#!/bin/sh
set -e
#  emsandbox : emdebian roots installer
#
#  Copyright (C) 2007  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.

EOF
}

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

if [ -d "$TARGET/debootstrap/" ]; then
	# need to read in the values passed to --include at first stage - debootstrap needs them repeated here.
	if [ -f $TARGET/debootstrap/machineincludes ]; then
		INC=`cat $TARGET/debootstrap/machineincludes`
	else
		INC=
	fi
	DEBOOTSTRAP_DIR="$TARGET/debootstrap/" ./debootstrap/debootstrap ${INC} --second-stage --second-stage-target $TARGET
else
	echo "Unable to find ./debootstrap/ directory!"
	echo "emsecondstage needs to be run from the top level directory"
	echo "where the .tgz was decompressed - the / of the final chroot."
fi

# debootstrap puts a default sources list in place without regard for ours.
# emdebootstrap stores the real one in /etc/apt/sources.list.d/emdebian.sources.list
# so remove the debootstrap broken one and leave a note for users
# not expecting to look in /etc/apt/sources.list.d/
echo "# see sources lists created in /etc/apt/sources.list.d/" > $TARGET/etc/apt/sources.list

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 $TARGET/emsecondstage
	rm $TARGET/emdebian-*.tgz
fi
