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

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_DIR/scripts/wait_on_xbmc_exit $INSTALL/usr/bin
  cp $PKG_BUILD/tools/EventClients/Clients/XBMC\ Send/xbmc-send.py $INSTALL/usr/bin/xbmc-send
  cp $PKG_DIR/scripts/setwakeup.sh $INSTALL/usr/bin

mkdir -p $INSTALL/usr/lib/xbmc
  cp $PKG_BUILD/xbmc.bin $INSTALL/usr/lib/xbmc
  if [ "$DISPLAYSERVER" = "xorg-server" ]; then
    cp $PKG_BUILD/xbmc-xrandr $INSTALL/usr/lib/xbmc
  fi

cd $PKG_BUILD
  find system addons \
    -regextype posix-extended -type f \
    -not -iregex ".*svn.*|.*win32(dx)?\.vis|.*osx\.vis" \
    -iregex ".*-linux.*|.*-arm.*|.*\.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.*|.*-arm.*|.*\.vis|.*\.xbs|.*svn.*|.*\.orig|.*\.so|.*\.dll|.*\.pyd|.*python|.*\.zlib|.*\.conf" \
    -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
  rm -rf $INSTALL/usr/share/xbmc/addons/script.module.pysqlite
  rm -rf $INSTALL/usr/share/xbmc/addons/script.module.simplejson

mkdir -p $INSTALL/usr/share/xbmc/addons
  cp -R $PKG_DIR/config/os.openelec.tv $INSTALL/usr/share/xbmc/addons
  $SED "s|@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
  mkdir -p $INSTALL/usr/share/xbmc/config
    if [ -f $PROJECT_DIR/$PROJECT/xbmc/guisettings.xml ]; then
      cp -R $PROJECT_DIR/$PROJECT/xbmc/guisettings.xml $INSTALL/usr/share/xbmc/config
    fi

    if [ -f $PROJECT_DIR/$PROJECT/xbmc/sources.xml ]; then
      cp -R $PROJECT_DIR/$PROJECT/xbmc/sources.xml $INSTALL/usr/share/xbmc/config
    fi

  mkdir -p $INSTALL/usr/share/xbmc/system/
    if [ -f $PROJECT_DIR/$PROJECT/xbmc/advancedsettings.xml ]; then
      cp $PROJECT_DIR/$PROJECT/xbmc/advancedsettings.xml $INSTALL/usr/share/xbmc/system/
    else
      cp $PKG_DIR/config/advancedsettings.xml $INSTALL/usr/share/xbmc/system/
    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
