#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2011 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, 675 Mass Ave, Cambridge, MA 02139, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

. config/options $1

$SCRIPTS/build busybox-hosttools

VER=`ls $BUILD/linux*/modules/lib/modules`

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

# ATI kernel driver
mkdir -p $INSTALL/lib/modules/$VER/ati
  cp $PKG_BUILD/common/lib/modules/fglrx/build_mod/2.6.x/fglrx.ko $INSTALL/lib/modules/$VER/ati

$ROOT/$TOOLCHAIN/sbin/depmod -b $INSTALL $VER > /dev/null
for i in `ls $INSTALL/lib/modules/*/modules.* | grep -v modules.dep | grep -v modules.alias | grep -v modules.symbols`; do
  rm -f $i
done

# X.Org modules
mkdir -p $INSTALL/$XORG_PATH_MODULES
  cp -P $PKG_BUILD/$BASEDIR/usr/X11R6/$LIBDIR/modules/*.so $INSTALL/$XORG_PATH_MODULES

mkdir -p $INSTALL/$XORG_PATH_MODULES/drivers
  cp -P $PKG_BUILD/$BASEDIR/usr/X11R6/$LIBDIR/modules/drivers/*_drv.so $INSTALL/$XORG_PATH_MODULES/drivers

mkdir -p $INSTALL/$XORG_PATH_MODULES/extensions/fglrx
  # rename to not conflicting with Mesa libGL.so
  cp -P $PKG_BUILD/$BASEDIR/usr/X11R6/$LIBDIR/modules/extensions/fglrx/fglrx-libglx.so $INSTALL/$XORG_PATH_MODULES/extensions/libglx_fglrx.so

mkdir -p $INSTALL/$XORG_PATH_MODULES/linux
  cp -P $PKG_BUILD/$BASEDIR/usr/X11R6/$LIBDIR/modules/linux/*.so $INSTALL/$XORG_PATH_MODULES/linux

mkdir -p $INSTALL/etc/X11
  cp $PKG_DIR/config/*.conf $INSTALL/etc/X11

mkdir -p $INSTALL/etc/ati
  cp $PKG_BUILD/common/etc/ati/amdpcsdb.default $INSTALL/etc/ati
  cp $PKG_DIR/config/amdpcsdb $INSTALL/usr/config/fglrx.conf
    ln -sf /storage/.config/fglrx.conf $INSTALL/etc/ati/amdpcsdb
  cp $PKG_BUILD/common/etc/ati/control $INSTALL/etc/ati
  cp $PKG_BUILD/common/etc/ati/signature $INSTALL/etc/ati

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

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

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

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

if [ "$VAAPI" = yes ]; then
  $SCRIPTS/install xvba-video

  cp $PKG_BUILD/arch/$FGLRX_ARCH/usr/X11R6/$LIBDIR/libAMDXvBA.cap $INSTALL/usr/lib
  cp $PKG_BUILD/arch/$FGLRX_ARCH/usr/X11R6/$LIBDIR/libAMDXvBA.so* $INSTALL/usr/lib/libAMDXvBA.so.1
  cp $PKG_BUILD/arch/$FGLRX_ARCH/usr/X11R6/$LIBDIR/libXvBAW.so* $INSTALL/usr/lib/libXvBAW.so.1
fi
