#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

. config/options $1

if [ "$TARGET_ARCH" = i386 ]; then
  FGLRX_ARCH=x86
  LIBDIR=lib
  BASEDIR=xpic
elif [ "$TARGET_ARCH" = x86_64 ]; then
  FGLRX_ARCH=x86_64
  LIBDIR=lib64
  BASEDIR=xpic_64a
fi

INSTALL_DIR=".install/usr/lib/fglrx-legacy/"

cd $PKG_BUILD/common/lib/modules/fglrx/build_mod
  ln -sf $ROOT/$PKG_BUILD/arch/$FGLRX_ARCH/lib/modules/fglrx/build_mod/libfglrx_ip.a .

  cd 2.6.x
  sed -i -e 's/  -DPAGE_ATTR_FIX=/-DCOMPAT_ALLOC_USER_SPACE=arch_compat_alloc_user_space -DPAGE_ATTR_FIX=/' Makefile
  LDFLAGS="" make CC=${CC} KDIR=$(kernel_path)

cd $ROOT/$PKG_BUILD

# config files
  mkdir -p $INSTALL_DIR/etc/ati
    cp common/etc/ati/amdpcsdb.default $INSTALL_DIR/etc/ati
    cp common/etc/ati/control $INSTALL_DIR/etc/ati
    cp common/etc/ati/signature $INSTALL_DIR/etc/ati
    ln -sf /storage/.config/fglrx-legacy.conf $INSTALL_DIR/etc/ati/amdpcsdb

    # hack around a broken driver
    cp $ROOT/$PKG_DIR/config/control $INSTALL_DIR/etc/ati

(
  cd $INSTALL_DIR/
  for etc in `find etc -type f`; do
    mkdir -p $ROOT/$PKG_BUILD/.install/`dirname $etc`
      ln -sf /var/run/fglrx/$etc $ROOT/$PKG_BUILD/.install/$etc
  done
  ln -sf /var/run/fglrx/etc/ati/amdpcsdb $ROOT/$PKG_BUILD/.install/etc/ati/amdpcsdb
)

# ATI config
  mkdir -p $INSTALL_DIR/bin
    cp -P arch/$FGLRX_ARCH/usr/X11R6/bin/aticonfig $INSTALL_DIR/bin

(
  cd $INSTALL_DIR/
  for bin in `find bin -type f`; do
    mkdir -p $ROOT/$PKG_BUILD/.install/usr/`dirname $bin`
      ln -sf /var/run/fglrx/$bin $ROOT/$PKG_BUILD/.install/usr/$bin
  done
)

# OpenGL libs
  mkdir -p $INSTALL_DIR/lib
    # rename to not conflicting with Mesa libGL.so
    cp -P arch/$FGLRX_ARCH/usr/X11R6/$LIBDIR/fglrx/fglrx-libGL.so* $INSTALL_DIR/lib/libGL_fglrx.so.1

# System Libs
  mkdir -p $INSTALL_DIR/lib
    cp arch/$FGLRX_ARCH/usr/$LIBDIR/libati*.so $INSTALL_DIR/lib
    cp arch/$FGLRX_ARCH/usr/$LIBDIR/libatiuki.so* $INSTALL_DIR/lib/libatiuki.so.1
    cp arch/$FGLRX_ARCH/usr/X11R6/$LIBDIR/libati*.so* $INSTALL_DIR/lib
    cp arch/$FGLRX_ARCH/usr/X11R6/$LIBDIR/libfglrx*.so* $INSTALL_DIR/lib

# DRI driver
  mkdir -p $INSTALL_DIR/lib/dri
    cp arch/$FGLRX_ARCH/usr/X11R6/$LIBDIR/modules/dri/fglrx_dri.so* $INSTALL_DIR/lib/dri

# X.Org modules
  mkdir -p $INSTALL_DIR/lib/xorg/modules
    cp -P $BASEDIR/usr/X11R6/$LIBDIR/modules/*.so $INSTALL_DIR/lib/xorg/modules

  mkdir -p $INSTALL_DIR/lib/xorg/modules/drivers
    cp -P $BASEDIR/usr/X11R6/$LIBDIR/modules/drivers/*_drv.so $INSTALL_DIR/lib/xorg/modules/drivers

  mkdir -p $INSTALL_DIR/lib/xorg/modules/extensions
    cp -P $BASEDIR/usr/X11R6/$LIBDIR/modules/extensions/fglrx/fglrx-libglx.so $INSTALL_DIR/lib/xorg/modules/extensions/libglx_fglrx.so

  mkdir -p $INSTALL_DIR/lib/xorg/modules/linux
    cp -P $BASEDIR/usr/X11R6/$LIBDIR/modules/linux/*.so $INSTALL_DIR/lib/xorg/modules/linux

if [ "$XVBA" = yes ]; then
  cp arch/$FGLRX_ARCH/usr/X11R6/$LIBDIR/libAMDXvBA.cap $INSTALL_DIR/lib
  cp arch/$FGLRX_ARCH/usr/X11R6/$LIBDIR/libAMDXvBA.so* $INSTALL_DIR/lib/libAMDXvBA.so.1
  cp arch/$FGLRX_ARCH/usr/X11R6/$LIBDIR/libXvBAW.so* $INSTALL_DIR/lib/libXvBAW.so.1

# install needed files for toolchain
  mkdir -p $SYSROOT_PREFIX/usr/include/amd
    cp $ROOT/$PKG_DIR/source/amdxvba.h $SYSROOT_PREFIX/usr/include/amd

  mkdir -p $SYSROOT_PREFIX/usr/lib
    cp arch/$FGLRX_ARCH/usr/X11R6/*/libXvBAW.so* $SYSROOT_PREFIX/usr/lib
    ln -sf libXvBAW.so.1.0 $SYSROOT_PREFIX/usr/lib/libXvBAW.so.1
    ln -sf libXvBAW.so.1 $SYSROOT_PREFIX/usr/lib/libXvBAW.so
fi

(
  cd $INSTALL_DIR/
  for lib in `find lib -type f`; do
    mkdir -p $ROOT/$PKG_BUILD/.install/usr/`dirname $lib`
      ln -sf /var/run/fglrx/$lib $ROOT/$PKG_BUILD/.install/usr/$lib
  done
)
