#!/bin/sh

. config/options $1

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

mkdir -p $INSTALL/usr/bin
  cp $PKG_DIR/scripts/* $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 scripts 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 -

# 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

if [ "$WEBSERVER" = yes ]; then
  mkdir -p $INSTALL/usr/share/xbmc/web
    cp -R $PKG_BUILD/web/poc_jsonrpc/* $INSTALL/usr/share/xbmc/web
fi

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

if [ "$WIIMOTE_SUPPORT" = yes ]; then
  mkdir -p $INSTALL/usr/bin
    cp $PKG_BUILD/tools/EventClients/Clients/WiiRemote/WiiUse_WiiRemote $INSTALL/usr/bin/xbmc-wiiremote
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
fi
