#!/bin/sh

. config/options

PKG_DIR=`find $PACKAGES -type d -name $1`
[ "$OPENELEC_VERSION" = devel ] && \
  OPENELEC_VERSION=$OPENELEC_VERSION-`date +%Y%m%d`-r`bzr version-info --custom --template={revno}`

case "$2" in
  system)

    $SCRIPTS/install squashfs

    export INSTALL=$BUILD/$1/$2

    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

    cp -PR $PKG_DIR/config/etc/* $INSTALL/etc

# Basissystem...
    $SCRIPTS/install uClibc
    $SCRIPTS/install gcc-final
    $SCRIPTS/install linux $2
    $SCRIPTS/install busybox $2
    $SCRIPTS/install automountd

    echo $TARGET_ARCH > $INSTALL/etc/arch
    echo "$GEEXBOX_VERSION" > $INSTALL/etc/version
    echo "OpenELEC" > $INSTALL/etc/openelec-release

# Audio Driver...
    $SCRIPTS/install alsa

# Xorg...
    $SCRIPTS/install Xorg

# Network...
    $SCRIPTS/install network

# Mediacenter...
    $SCRIPTS/install $MEDIACENTER

    $SCRIPTS/install gst-plugins-good
    $SCRIPTS/install gst-plugins-bad
    $SCRIPTS/install gst-plugins-ugly
#    $SCRIPTS/install gst-ffmpeg
#    $SCRIPTS/install gnome-dvb-daemon

#    if [ "$BOOTCHART" = yes ]; then
#      $SCRIPTS/install acct
#      cp $PACKAGES/$1/scripts/bootchartd $INSTALL/sbin
#      sed -i "s/EXIT_PROC=.*/EXIT_PROC=\"$GUI\"/" $INSTALL/sbin/bootchartd
#    fi

    [ "$EMULATORS" = yes ] && $SCRIPTS/install emulators
    [ "$GAMES" = yes ] && $SCRIPTS/install games
    #[ "$GAMES" = yes ] && $SCRIPTS/install pygame $1
    #[ "$LCD4LINUX" = yes ] && $SCRIPTS/install lcd4linux $1

# Devtools... (not for Release)
   [ "$DEVTOOLS" = yes ] && $SCRIPTS/install gdb
   [ "$DEVTOOLS" = yes ] && $SCRIPTS/install mrxvt
   [ "$DEVTOOLS" = yes ] && $SCRIPTS/install pciutils
   [ "$DEVTOOLS" = yes ] && $SCRIPTS/install driconf

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

    #[ "$EXTRACODECS" = yes ] && $SCRIPTS/install extra-codecs-nonfree $1
    #[ "$EXTRAFIRMWARES" = yes ] && $SCRIPTS/install extra-firmwares-nonfree $1

    mkdir -p $INSTALL/usr/config
    tar cf - -C $INSTALL etc | lzma -9 -v > $INSTALL/usr/config/etc.tar.lzma
    rm -rf $INSTALL/etc

    ln -sf /var/etc $INSTALL/etc
    ln -sf /var $INSTALL/usr/var
    ln -sf /var/tmp $INSTALL/tmp
    ln -sf /var/mnt $INSTALL/mnt

    mkdir -p $ROOT/target
      rm -rf $ROOT/target/OpenELEC-$PROJECT-$OPENELEC_VERSION.kernel
      cp -PR $BUILD/linux-*/arch/x86/boot/bzImage $ROOT/target/OpenELEC-$PROJECT-$OPENELEC_VERSION.kernel
    
      rm -rf $ROOT/target/OpenELEC-$PROJECT-$MEDIACENTER-$OPENELEC_VERSION.system
      $ROOT/$TOOLCHAIN/bin/mksquashfs $INSTALL $ROOT/target/OpenELEC-$PROJECT-$OPENELEC_VERSION.system -noappend -all-root
#     $ROOT/$TOOLCHAIN/bin/mkfs.axfs $INSTALL openelec.system
  ;;

  qemu)

    $SCRIPTS/install image system

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

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

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

esac
