#!/bin/sh

set -e
#  emsandbox : emdebian rootfs installer
#
#  Copyright (C) 2007-2008  Neil Williams <codehelp@debian.org>
#
#  This package 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 3 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.

# simple wrapper to aid installation of the rootfs
# Provides a space for pre-installation custom commands as well as simplifying
# the command line.

TARGET=`pwd`

PROG=emsecondstage

usagehelp () {
    # print out help message
    cat <<EOF
$PROG - emdebian roots installer

Syntax: sudo $PROG
        sudo $PROG [COMMAND]

Commands:
-?|-h|--help|-version:      print this help message and exit

Although based on debootstrap, $PROG cannot support the full range of
debootstrap commands or options.

The standard Emdebian rootfs uses the 'busybox' package with 'dpkg' and
'apt'. Replacement scripts need to be full debootstrap suite shell
scripts that specify how to complete the first and second stage
installations. If the script uses 'busybox', the second-stage install
function must be compatible with the shell applet in busybox - avoid
bashisms!

Machine specific customisation hooks need to be shell scripts
(not bash) located in /debootstrap/machine/config.sh in the top level
directory of the rootfs. If you used 'emsandbox --machine ...' to
create the rootfs, the config.sh script for your machine and variant
has already been installed and will be executed by $PROG.

If the root filesystem used the 'unpack' method, $PROG only has to
apply the machine specific customisation hooks.

EOF
}

while [ -n "$1" ]; do
case "$1" in
	--help|-h|-?|--version)
		usagehelp
		exit;
	;;
	*)
		echo "Unrecognised command: $1"
	exit;
	;;
esac
done

# debootstrap directory removed if successful so avoid running twice.
if [ -d "$TARGET/debootstrap/" ]; then
	chmod 0755 $TARGET
	chroot $TARGET mount proc -t proc /proc || true
	if [ -f datestring ]; then
		TIME=`cat datestring`
		echo "Setting approximate time of $TIME"
		chroot $TARGET date -s $TIME || true
	fi
	echo "Running ldconfig in $TARGET ..."
	chroot $TARGET /sbin/ldconfig
	# in_target depmod # can only be run under the installed kernel, not the unpacking one.
	DEBIAN_FRONTEND=noninteractive
	DEBCONF_NONINTERACTIVE_SEEN=true
	export DEBIAN_FRONTEND DEBCONF_NONINTERACTIVE_SEEN
	# bug: See #451130
	if [ -f "$TARGET/usr/lib/cdebconf/debconf-loadtemplate" ]; then
		export DEBCONF_USE_CDEBCONF=true
		echo "Configuring cdebconf"
		chroot $TARGET /usr/lib/cdebconf/debconf-loadtemplate /usr/share/debconf/demo /usr/share/debconf/demo.templates
	fi
	echo "Configuring ..."
	chroot $TARGET dpkg --configure -a
	echo "Emdebian base system installed successfully in $TARGET."
fi

if [ -f $TARGET/machine/config.sh ]; then
	echo "  -> Running second stage config.sh script for this machine variant"
	sh $TARGET/machine/config.sh
	rm -r $TARGET/machine/
fi

if [ -d $TARGET/debootstrap ]; then
	rm -rf $TARGET/debootstrap
fi

if [ -d $TARGET/machine ]; then
	rm -rf $TARGET/machine
fi

if [ -f $TARGET/0x97BB3B58.txt ]; then
	rm $TARGET/0x97BB3B58.txt
fi

# If there is no time or date, this will need to remain.
#if [ -f $TARGET/emdebian-archive-keyring.gpg ]; then
#	rm $TARGET/emdebian-archive-keyring.gpg
#fi

if [ -d $TARGET/tmp/buildd ]; then
	rm -rf $TARGET/tmp/buildd
fi

# finally, remove self
if [ -f $TARGET/emsecondstage ]; then
	rm $TARGET/emsecondstage
fi

echo "Emdebian base system installed successfully."