#!/bin/sh -e

[ -n "$spt_dir" ] || spt_dir=/usr/share/spt3
. $spt_dir/init

mnt_dir=$work_dir/mnt

[ -n "$ROOT_MOUNT" ] || Fatal 'Unable to find root mount point to deploy system to'

pushd $ROOT_MOUNT 2>/dev/null || Fatal "Unable to chdir to $ROOT_MOUNT"

if [ -n "$TARBALL" ]; then
	Verbose "Deploying system from tarball $TARBALL"
	echo "$TARBALL" | grep -q '\.gz$' && COMPRESSION=-z
	echo "$TARBALL" | grep -q '\.bz2$' && COMPRESSION=-j
	tar -x $COMPRESSION $verbose -f $TARBALL
fi

mount -t proc proc proc
mount -t sysfs sys sys

for kver in lib/modules/*; do
	Verbose "Processing kernel: $kver"
	chroot $ROOT_MOUNT mkinitrd -f $verbose /boot/initrd-$kver.img $kver || Fatal "Unable to create initrd for $kver"
done

chroot $ROOT_MOUNT /sbin/lilo || Fatal 'Unable to set up LILO'

popd 2>/dev/null
