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

show_config

$SCRIPTS/checkdeps build
$SCRIPTS/build toolchain
$SCRIPTS/build squashfs
$SCRIPTS/build fakeroot
$SCRIPTS/build module-init-tools

export INSTALL=$BUILD/image/system

BUILD_DATE=`date +%Y%m%d%H%M%S`

if [ "$OPENELEC_VERSION" = devel -o "$OPENELEC_VERSION" = debug ]; then
  GIT_BUILD=`git rev-list HEAD | wc -l`
  GIT_HASH=`git rev-parse HEAD`
  OPENELEC_VERSION=$OPENELEC_VERSION-$BUILD_DATE-r$GIT_BUILD
fi

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

# setup fakeroot
  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

# clean old install dirs
  rm -rf $INSTALL
  rm -rf $STAMPS_INSTALL
  mkdir -p $INSTALL

# 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/run
  ln -sf /var/run $INSTALL/run
  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
  if [ -n "$GIT_HASH" ]; then
    echo "$GIT_HASH" >> $INSTALL/etc/gitrev
  fi

# create /etc/issue
  echo $GREETING0 >  $INSTALL/etc/issue
  echo $GREETING1 >> $INSTALL/etc/issue
  echo $GREETING2 >> $INSTALL/etc/issue
  echo $GREETING3 >> $INSTALL/etc/issue
  echo $GREETING4 >> $INSTALL/etc/issue
  echo "$DISTRONAME Version: $OPENELEC_VERSION" >> $INSTALL/etc/issue
  if [ -n "$GIT_HASH" ]; then
    echo "$DISTRONAME git: $GIT_HASH" >> $INSTALL/etc/issue
  fi

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

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

# 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

# NTFS 3G support
  [ "$EXFAT" = "yes" ] && $SCRIPTS/install fuse-exfat

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

# 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

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

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

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

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

# run depmod
  KVER=`ls $BUILD/linux-*/modules/lib/modules`
  $ROOT/$TOOLCHAIN/bin/depmod -b $INSTALL $KVER > /dev/null

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

# make target dir
  mkdir -p $TARGET_IMG
    rm -rf $TARGET_IMG/$IMAGE_NAME.kernel
    rm -rf $TARGET_IMG/$IMAGE_NAME.mach_kernel

# copy kernel to target dir
  if [ "$BOOTLOADER" = "u-boot" ]; then
    KERNEL_IMAGE="uImage"
  elif [ "$BOOTLOADER" = "bcm2835-bootloader" ]; then
    KERNEL_IMAGE="zImage"
  else
    KERNEL_IMAGE="bzImage"
  fi

  if [ "$TARGET_ARCH" = i386 -o "$TARGET_ARCH" = x86_64 ]; then
    KERNEL_ARCH="x86"
  elif [ "$TARGET_ARCH" = arm ]; then
    KERNEL_ARCH="arm"
  fi

  cp -PR $BUILD/linux-*/arch/$KERNEL_ARCH/boot/$KERNEL_IMAGE $TARGET_IMG/$IMAGE_NAME.kernel

  if [ "$BOOTLOADER" = "atv-bootloader" ]; then
    cp -PR $BUILD/atv-bootloader-*/mach_kernel $TARGET_IMG/$IMAGE_NAME.mach_kernel
    chmod 0644 $TARGET_IMG/$IMAGE_NAME.mach_kernel
  fi

# create squashfs file
  if [ -z "$SQUASHFS_COMPRESSION" ]; then
    SQUASHFS_COMPRESSION="gzip"
  fi

  echo "rm -rf $TARGET_IMG/$IMAGE_NAME.system" >> $FAKEROOT_SCRIPT
  echo "$ROOT/$TOOLCHAIN/bin/mksquashfs $BUILD/image/system $TARGET_IMG/$IMAGE_NAME.system -noappend -comp $SQUASHFS_COMPRESSION" >> $FAKEROOT_SCRIPT

# run fakeroot
  $ROOT/$TOOLCHAIN/bin/fakeroot -- $FAKEROOT_SCRIPT
  rm -rf $FAKEROOT_SCRIPT

# set permissions
  chmod 0644 $TARGET_IMG/$IMAGE_NAME.system
  chmod 0644 $TARGET_IMG/$IMAGE_NAME.kernel

  if [ "$1" = "release" ]; then

    RELEASE_DIR="target/$IMAGE_NAME"

    # cleanup
      rm -rf $RELEASE_DIR

    # create release dir
      mkdir -p $RELEASE_DIR
        if [ -n "$BOOTLOADER" ]; then
          BOOTLOADER_DIR=`find $PACKAGES -type d -name $BOOTLOADER 2>/dev/null`
          if [ -d "$BOOTLOADER_DIR"/files ]; then
            cp -R $BOOTLOADER_DIR/files/* $RELEASE_DIR
          fi
          if [ -f "$BOOTLOADER_DIR"/release ]; then
            . $BOOTLOADER_DIR/release
          fi
        fi
        cp $ROOT/README* $RELEASE_DIR
        cp $ROOT/CHANGELOG* $RELEASE_DIR
        cp -R $CONFIG/release/openelec.ico $RELEASE_DIR
        echo "$TARGET_VERSION" > $RELEASE_DIR/RELEASE

        if [ -n "$MEDIACENTER" ] ; then
          echo "XBMC commit: `scripts/git_version $MEDIACENTER`" >> $RELEASE_DIR/RELEASE
        fi

      mkdir -p $RELEASE_DIR/licenses
        cp $ROOT/licenses/* $RELEASE_DIR/licenses

      mkdir -p $RELEASE_DIR/target
        cp $TARGET_IMG/$IMAGE_NAME.system $RELEASE_DIR/target/SYSTEM
        cp $TARGET_IMG/$IMAGE_NAME.kernel $RELEASE_DIR/target/KERNEL

        if [ -f $TARGET_IMG/$IMAGE_NAME.mach_kernel ]; then
          cp $TARGET_IMG/$IMAGE_NAME.mach_kernel $RELEASE_DIR/target/MACH_KERNEL
        fi

    # create md5sum's
      ( cd $RELEASE_DIR;
        md5sum -t target/SYSTEM > target/SYSTEM.md5;
        md5sum -t target/KERNEL > target/KERNEL.md5;
        if [ -f target/MACH_KERNEL ]; then
          md5sum -t target/MACH_KERNEL > target/MACH_KERNEL.md5;
        fi
      )

    # create target directory
      mkdir -p $TARGET_IMG

    # remove an previous created release tarball
      rm -rf $TARGET_IMG/$IMAGE_NAME.tar.bz2

    # create release tarball
      tar cjf $TARGET_IMG/$IMAGE_NAME.tar.bz2 -C target $IMAGE_NAME

    # cleanup release dir
      rm -rf $RELEASE_DIR
  fi
