#!/bin/sh
#=============================================================================#
#                         Main nanospt script                                 #
#=============================================================================#
# (C) Denis Smirnov <mithraen@freesource.info>                                            #
#=============================================================================#
set +e
if [ -f profile.config ]; then
	. profile.config
else
	echo "Must be runned from profile directory"
	exit -1
fi

mkdir -p $DISTRTMP
T=$DISTRTMP
TI=$DISTRTMP/images

mkdir -p $TI

install_kernel()
{
	pkglst2aptbox $TI/image.kernel	profile/list.kernel
	cp -a `which cpio` $TI/image.kernel/chroot/.host/
	aptbox-run-script $TI/image.kernel /usr/share/mithraen-nanospt/kernel > $T/kernel_modules.cpio
	hsh-run $TI/image.kernel --rooter -- cat /boot/vmlinuz > $T/vmlinuz
	hsh --cleanup $TI/image.kernel
}

image_installer()
{
	rm -f $T/log.installer
	pkglst2aptbox $TI/image.installer profile/list.installer 2>> $T/log.installer >> $T/log.installer
	echo "Unpacking kernel modules..."
	cat $T/kernel_modules.cpio | aptbox-cpio $TI/image.installer -i -d -c --absolute-filenames --owner=root:root
	echo "Creating cpio..."
	aptbox2cpio $TI/image.installer $DISTRTMP/installer.cpio
	echo "Cleanup..."
	hsh --cleanup $TI/image.installer
	echo "Compressing cpio..."
	rm -f $DISTRTMP/installer.cpio.gz
	gzip -9 $DISTRTMP/installer.cpio
}
#----------------------------------------------------------------------------#
create_image()
{
	rm -f $DISTRTMP/log.$1
	echo "Creating aptbox with $1 component" | csed '/.*/ p green'
#        if [ -d profile/$1/preinstall ]; then
#			for s in profile/$1/preinstall/*; do
#				echo "Running script "`basename $s`
#				if [ -x "$s" ]; then
#					./aptbox-run-script $TI/image.$1 "$s"
#				fi
#			done
#		fi
	pkglst2aptbox $TI/image.$1 profile/$1/lists 2>> $DISTRTMP/log.$1 >> $DISTRTMP/log.$1
        if [ -d profile/$1/postinstall ]; then
			for s in profile/$1/postinstall/*; do
				echo "Running script "`basename $s`
				if [ -x "$s" ]; then
					aptbox-run-script $TI/image.$1 "$s"
				fi
			done
		fi
	echo "Creating cpio..." | csed '/.*/ p green'
	aptbox2cpio $TI/image.$1 $DISTRTMP/$1.cpio
	echo "Cleanup aptbox.." | csed '/.*/ p green'
	hsh --cleanup $TI/image.$1
	rmdir $TI/image.$1
	echo "Compress cpio.." | csed '/.*/ p green'
	rm -f $DISTRTMP/$1.cpio.gz
	gzip -9 $DISTRTMP/$1.cpio
}
#----------------------------------------------------------------------------#
install_kernel
image_installer

for s in $PROFILES; do
	create_image "$s"
done

#----------------------------------------------------------------------------#

cp_image()
{
    rsync $DISTRTMP/$1.cpio.gz	$T/iso/.images/
    ./compile.partitions	profile/$1/fs > $T/iso/.images/$1.fs
}

echo "Creating ISO..." | csed '/.*/ p green'

for s in $PROFILES; do
	cp_image "$s"
done

mkdir -p $T/iso/isolinux $T/iso/.images
cp -f /usr/lib/syslinux/isolinux.bin	$T/iso/isolinux
cp -f profile/isolinux.cfg				$T/iso/isolinux
cp -f profile/prompt.msg				$T/iso/isolinux
rsync $T/vmlinuz $T/iso/isolinux
rsync $T/installer.cpio.gz $T/iso/isolinux/full.cz
rsync -a --exclude .svn iso/ $DISTRTMP/iso/

gen_iso $ISO $DISTRTMP/iso/ profile/mkisofs.sort

