#!/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

PYTHON_LIBDIR="`ls -d $INSTALL/usr/lib/python*`"

mkdir -p $INSTALL/usr/bin
  cp $PKG_DIR/scripts/cputemp $INSTALL/usr/bin
  cp $PKG_DIR/scripts/gputemp $INSTALL/usr/bin
  cp $PKG_BUILD/tools/EventClients/Clients/XBMC\ Send/xbmc-send.py $INSTALL/usr/bin/xbmc-send

mkdir -p $INSTALL/usr/lib/xbmc
  cp $PKG_BUILD/xbmc.bin $INSTALL/usr/lib/xbmc
  cp $PKG_BUILD/xbmc-xrandr $INSTALL/usr/lib/xbmc

cd $PKG_BUILD
  find system addons \
    -regextype posix-extended -type f \
    -not -iregex ".*svn.*|.*win32(dx)?\.vis|.*osx\.vis" \
    -iregex ".*-linux.*|.*\.vis|.*\.xbs" \
    -exec install -D "{}" $ROOT/$INSTALL/usr/lib/xbmc/"{}" ";"

  find addons language media sounds userdata system \
    -regextype posix-extended -type f \
    -not -iregex ".*-linux.*|.*\.vis|.*\.xbs|.*svn.*|.*\.orig|.*\.so|.*\.dll|.*\.pyd|.*python/.*\.zlib" \
    -exec install -D -m 0644 "{}" $ROOT/$INSTALL/usr/share/xbmc/"{}" ";"
cd -

if [ ! "$XBMC_SCR_RSXS" = yes ]; then
  rm -rf $ROOT/$INSTALL/usr/share/xbmc/addons/screensaver.rsxs.*
fi

if [ ! "$XBMC_VIS_PROJECTM" = yes ]; then
  rm -rf $ROOT/$INSTALL/usr/share/xbmc/addons/visualization.projectm
fi

rm -rf $ROOT/$INSTALL/usr/share/xbmc/addons/visualization.dxspectrum
rm -rf $ROOT/$INSTALL/usr/share/xbmc/addons/visualization.itunes
rm -rf $ROOT/$INSTALL/usr/share/xbmc/addons/visualization.milkdrop

# overriding Splash image
mkdir -p $INSTALL/usr/share/xbmc/media
  rm -rf $INSTALL/usr/share/xbmc/media/Splash.png
#   if [ -f $PROJECT_DIR/$PROJECT/splash/splash.png ]; then
#     cp $PROJECT_DIR/$PROJECT/splash/splash.png $INSTALL/usr/share/xbmc/media/Splash.png
#   else
#     cp $PKG_DIR/splash/splash.png $INSTALL/usr/share/xbmc/media/Splash.png
#   fi

# cleanup and python addon fixes
  mkdir -p $INSTALL/usr/share/xbmc/addons/script.module.pil/lib
  ln -s `ls -d $INSTALL/usr/lib/python*/site-packages | sed -e "s,$INSTALL,,"`/PIL \
         $INSTALL/usr/share/xbmc/addons/script.module.pil/lib/PIL
  rm -rf $INSTALL/usr/share/xbmc/addons/script.module.pysqlite

mkdir -p $INSTALL/usr/share/xbmc/addons
  cp -R $PKG_DIR/config/os.openelec.tv $INSTALL/usr/share/xbmc/addons
  $SED "s|@ADDON_OS_VERSION@|$OS_VERSION|g" -i $INSTALL/usr/share/xbmc/addons/os.openelec.tv/addon.xml
  cp -R $PKG_DIR/config/repository.openelec.tv $INSTALL/usr/share/xbmc/addons
  $SED "s|@ADDON_URL@|$ADDON_URL|g" -i $INSTALL/usr/share/xbmc/addons/repository.openelec.tv/addon.xml

mkdir -p $PYTHON_LIBDIR/site-packages/xbmc
  cp -R $PKG_BUILD/tools/EventClients/lib/python/* $PYTHON_LIBDIR/site-packages/xbmc

# install powermanagement hooks
  mkdir -p $INSTALL/etc/pm/sleep.d
    cp $PKG_DIR/sleep.d/* $INSTALL/etc/pm/sleep.d

# install project specific configs
  if [ -f $PROJECT_DIR/$PROJECT/xbmc/*.xml ]; then
    mkdir -p $INSTALL/usr/share/xbmc/config
      cp -R $PROJECT_DIR/$PROJECT/xbmc/* $INSTALL/usr/share/xbmc/config
  fi

if [ "$XBMC_EXTRA_FONTS" = yes ]; then
  mkdir -p $INSTALL/usr/share/xbmc/media/Fonts
    cp $PKG_DIR/fonts/*.ttf $INSTALL/usr/share/xbmc/media/Fonts
fi

if [ "$WIIMOTE_SUPPORT" = yes ]; then
  mkdir -p $INSTALL/usr/bin
    cp $PKG_BUILD/tools/EventClients/Clients/WiiRemote/WiiUse_WiiRemote $INSTALL/usr/bin/xbmc-wiiremote
  mkdir -p $INSTALL/lib/udev
    cp $PKG_DIR/scripts/wiiremote_helper $INSTALL/lib/udev
fi

if [ "$PS3REMOTE_SUPPORT" = yes ]; then
  mkdir -p $INSTALL/usr/bin
    cp $PKG_BUILD/tools/EventClients/Clients/PS3\ BD\ Remote/ps3_remote.py $INSTALL/usr/bin/xbmc-ps3remote
  mkdir -p $INSTALL/usr/share/pixmaps/xbmc
    cp $PKG_BUILD/tools/EventClients/icons/bluetooth.png $INSTALL/usr/share/pixmaps/xbmc
  mkdir -p $PYTHON_LIBDIR/site-packages/xbmc
   echo 'ICON_PATH="/usr/share/pixmaps/xbmc/"' > $PYTHON_LIBDIR/site-packages/xbmc/defs.py
  mkdir -p $INSTALL/lib/udev
    cp $PKG_DIR/scripts/ps3remote_helper $INSTALL/lib/udev
fi
