#!/bin/sh

. config/options

#PKG_DIR=`find $PACKAGES -type d -name $1`

if [ "$OPENELEC_VERSION" = devel -o "$OPENELEC_VERSION" = debug ]; then
  BUILD_DATE=`date +%Y%m%d`
  GIT_BUILD=`git log --pretty=format:'' | wc -l`
  OPENELEC_VERSION=$OPENELEC_VERSION-$BUILD_DATE-r$GIT_BUILD
fi

TARGET_VERSION="$PROJECT.$TARGET_ARCH-$OPENELEC_VERSION"

case "$2" in
  system)

    $SCRIPTS/install squashfs
    $SCRIPTS/build fakeroot

    export INSTALL=$BUILD/$1/$2

    rm -rf $FAKEROOT_SCRIPT   # remove $FAKEROOT_SCRIPT if it exist
    touch $FAKEROOT_SCRIPT    # create an empty $FAKEROOT_SCRIPT
    chmod +x $FAKEROOT_SCRIPT # make $FAKEROOT_SCRIPT executable
    echo "chown -R 0:0 $INSTALL" >> $FAKEROOT_SCRIPT

    rm -rf $INSTALL
    mkdir -p $INSTALL

    mkdir -p $INSTALL/bin
    mkdir -p $INSTALL/etc
    mkdir -p $INSTALL/lib
    mkdir -p $INSTALL/sbin
    mkdir -p $INSTALL/dev
    mkdir -p $INSTALL/proc
    mkdir -p $INSTALL/sys
    mkdir -p $INSTALL/usr
    mkdir -p $INSTALL/var
    mkdir -p $INSTALL/flash
    mkdir -p $INSTALL/storage

    ln -sf /var $INSTALL/usr/var
    ln -sf /var/tmp $INSTALL/tmp
    ln -sf /var/media $INSTALL/media

    if [ $TARGET_ARCH = x86_64 -o $TARGET_ARCH = powerpc64 ]; then
      ln -s /lib $INSTALL/lib64
      ln -s lib $INSTALL/usr/lib64
    fi

# copy project related files to filesystem
   if [ -d $PROJECT_DIR/$PROJECT/filesystem ]; then
     cp -PR $PROJECT_DIR/$PROJECT/filesystem/* $INSTALL
   fi

# Basissystem...
    $SCRIPTS/install eglibc
    $SCRIPTS/install gcc-final
    $SCRIPTS/install linux $2
    $SCRIPTS/install busybox
    $SCRIPTS/install util-linux-ng

    echo "OpenELEC" > $INSTALL/etc/distribution
    echo "$PROJECT.$TARGET_ARCH" > $INSTALL/etc/arch
    echo "$OPENELEC_VERSION" > $INSTALL/etc/version
    echo "OpenELEC.tv - Date of build: $BUILD_DATE Build: $BZR_BUILD" > $INSTALL/etc/openelec-release

# Network support
    [ "$NETWORK" = yes ] && $SCRIPTS/install network

# Transmission BitTorrent support
    [ "$TRANSMISSION" = yes ] && $SCRIPTS/install transmission

# Graphic support
    [ ! "$DISPLAYSERVER" = no ] && $SCRIPTS/install $DISPLAYSERVER

# Multimedia support
    [ ! "$MEDIACENTER" = no ] && $SCRIPTS/install mediacenter

# Automounter support
    [ "$UDISKS" = yes ] && $SCRIPTS/install udisks

# Powermanagement support
    [ "$UPOWER" = yes ] && $SCRIPTS/install upower
    [ "$UPOWER" = yes ] && $SCRIPTS/install ConsoleKit

# Remote support
    [ "$REMOTE_SUPPORT" = yes ] && $SCRIPTS/install remote

# LCD support
    [ ! "$LCD_DRIVER" = none ] && $SCRIPTS/install lcdproc

# Sensors support
    [ "$SENSOR_SUPPORT" = yes ] && $SCRIPTS/install lm_sensors

# Update support
    [ "$UPDATE_SUPPORT" = yes ] && $SCRIPTS/install autoupdate

# Games support
    [ "$GAMES" = yes ] && $SCRIPTS/install games
    [ "$EMULATORS" = yes ] && $SCRIPTS/install emulators

# Devtools... (not for Release)
    [ "$TESTING" = yes ] && $SCRIPTS/install testing
    [ "$DEVTOOLS" = yes ] && $SCRIPTS/install debug

# Devtools for Coreboot... (not for Release)
    [ "$COREBOOT" = yes ] && $SCRIPTS/install superiotool
    [ "$COREBOOT" = yes ] && $SCRIPTS/install flashrom
    [ "$COREBOOT" = yes ] && $SCRIPTS/install getpir

# setting
    echo $TARGET_VERSION > $INSTALL/etc/release

# strip kernel modules
  for MOD in `find $INSTALL/lib/modules/ -name *.ko`; do
    $STRIP --strip-debug $MOD
  done

  mkdir -p $ROOT/target
    rm -rf $ROOT/target/OpenELEC-$TARGET_VERSION.kernel
    cp -PR $BUILD/linux-*/arch/x86/boot/bzImage $ROOT/target/OpenELEC-$TARGET_VERSION.kernel

    echo "rm -rf $ROOT/target/OpenELEC-$TARGET_VERSION.system" >> $FAKEROOT_SCRIPT
#    echo "$ROOT/$TOOLCHAIN/bin/mksquashfs $INSTALL $ROOT/target/OpenELEC-$TARGET_VERSION.system -noappend -comp lzma" >> $FAKEROOT_SCRIPT
    echo "$ROOT/$TOOLCHAIN/bin/mksquashfs $INSTALL $ROOT/target/OpenELEC-$TARGET_VERSION.system -noappend" >> $FAKEROOT_SCRIPT
    $ROOT/$TOOLCHAIN/bin/fakeroot -- $FAKEROOT_SCRIPT
    chmod 0644 $ROOT/target/OpenELEC-$TARGET_VERSION.system
    rm -rf $FAKEROOT_SCRIPT
  ;;

  release)

    $SCRIPTS/install image system

# cleanup
    rm -rf $BUILD/$2/OpenELEC-$TARGET_VERSION

    mkdir -p $BUILD/$2/OpenELEC-$TARGET_VERSION
      cp -R $CONFIG/release/* $BUILD/$2/OpenELEC-$TARGET_VERSION
      echo "$TARGET_VERSION" > $BUILD/$2/OpenELEC-$TARGET_VERSION/RELEASE
      cp $ROOT/CHANGELOG $BUILD/$2/OpenELEC-$TARGET_VERSION

    mkdir -p $BUILD/$2/OpenELEC-$TARGET_VERSION/licenses
      cp $ROOT/licenses/* $BUILD/$2/OpenELEC-$TARGET_VERSION/licenses

    mkdir -p $BUILD/$2/OpenELEC-$TARGET_VERSION/target
      cp $ROOT/target/OpenELEC-$TARGET_VERSION.system $BUILD/$2/OpenELEC-$TARGET_VERSION/target/SYSTEM
      cp $ROOT/target/OpenELEC-$TARGET_VERSION.kernel $BUILD/$2/OpenELEC-$TARGET_VERSION/target/KERNEL

# create release directory
    mkdir -p $ROOT/target

# remove an previous created release tarball
    rm -rf $ROOT/target/OpenELEC-$TARGET_VERSION.tar.bz2
    tar cjf $ROOT/target/OpenELEC-$TARGET_VERSION.tar.bz2 -C $BUILD/$2 OpenELEC-$TARGET_VERSION
  ;;

  qemu)

    $SCRIPTS/install image system

    mkdir -p $ROOT/target
      rm -rf $ROOT/target/OpenELEC-$TARGET_VERSION-qemu.flash
      qemu-img create -f raw $ROOT/target/OpenELEC-$TARGET_VERSION-qemu.flash 128M
      /sbin/mkfs.ext3 -L OpenELEC -F $ROOT/target/OpenELEC-$TARGET_VERSION-qemu.flash

    mkdir -p $ROOT/.tmp
      sudo mount -o loop $ROOT/target/OpenELEC-$TARGET_VERSION-qemu.flash $ROOT/.tmp
      cp -R $ROOT/target/OpenELEC-$TARGET_VERSION.system $ROOT/.tmp/openelec.system
      sudo umount $ROOT/.tmp

    rm -rf $ROOT/target/OpenELEC-$TARGET_VERSION-qemu.store
    qemu-img create -f raw $ROOT/target/OpenELEC-$TARGET_VERSION-qemu.store 40M
    /sbin/mkfs.ext3 -L OpenELEC -F $ROOT/target/OpenELEC-$TARGET_VERSION-qemu.store

  ;;

esac
