#!/bin/sh

. config/options $1

$SCRIPTS/checkdeps build
$SCRIPTS/build toolchain

export INSTALL=$BUILD/image/system

get_version

rm -rf $INSTALL
rm -rf $STAMPS_INSTALL

mkdir -p $INSTALL

# setup fakeroot
  echo "chown -R 0:0 $INSTALL" >> $FAKEROOT_SCRIPT

# create baselayout
  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

  echo "$DISTRONAME" > $INSTALL/etc/distribution
  echo "$PROJECT.$TARGET_ARCH" > $INSTALL/etc/arch
  echo "$OPENELEC_VERSION" > $INSTALL/etc/version
  echo "$TARGET_VERSION" > $INSTALL/etc/release
  echo "$DISTRONAME - Version: $OPENELEC_VERSION" > $INSTALL/etc/openelec-release

# create /etc/issue
  echo $GREATING0 >  $INSTALL/etc/issue
  echo $GREATING1 >> $INSTALL/etc/issue
  echo $GREATING2 >> $INSTALL/etc/issue
  echo $GREATING3 >> $INSTALL/etc/issue
  echo $GREATING4 >> $INSTALL/etc/issue
  echo "$DISTRONAME Version: $OPENELEC_VERSION" >> $INSTALL/etc/issue

# 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 system
  $SCRIPTS/install busybox
  $SCRIPTS/install util-linux-ng

# 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
  [ "$TVHEADEND" = "yes" ] && $SCRIPTS/install hts-tvheadend

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

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

# NTFS 3G support
  [ "$NTFS3G" = "yes" ] && $SCRIPTS/install ntfs-3g

# 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

# Update support
  [ "$INSTALLER_SUPPORT" = "yes" ] && $SCRIPTS/install installer

# Games and Emulators support (SDK's)
  [ "$GAMES" = "yes" ] && $SCRIPTS/install games-sdk
  [ "$EMULATORS" = "yes" ] && $SCRIPTS/install emulators-sdk

# some system tools
  [ "$TOOLS" = "yes" ] && $SCRIPTS/install tools

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

# OEM packages
  [ "$OEM_SUPPORT" = "yes" ] && $SCRIPTS/install oem

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