#!/bin/sh -e

# generate a new fstab
cat <<EOF > $TARGET/etc/fstab
/dev/ubda / 	$fstype defaults 			0 1
proc 	/proc 	proc 	defaults	 		0 0
tmpfs  	/tmp    tmpfs   defaults,size=768M 		0 0
EOF

# setup the timezone, the same as the host
[ ! -f $HOST/etc/timezone ] || TARGET_TIMEZONE="$(cat $HOST/etc/timezone)"

if [ -f "$TARGET/usr/share/zoneinfo/$TARGET_TIMEZONE" ] ; then
	chroot $TARGET cp /usr/share/zoneinfo/$TARGET_TIMEZONE /etc/localtime
	echo "$TARGET_TIMEZONE" > $TARGET/etc/timezone
elif [ -f "$HOST/etc/localtime" ] ; then
	cp $HOST/etc/localtime $TARGET/etc/localtime
fi 

# kernel modules
mkdir -p $TARGET/lib/modules

[ -n "$kernel_modules" ] || kernel_modules=copy
[ -n "$kernel_modules_dir" ] || kernel_modules_dir=/usr/lib/uml/modules

if ! [ -d "$kernel_modules_dir" ] ; then
	echo "ERROR: \"$kernel_modules_dir\" not found, skipping kernel modules."

else
	case "$kernel_modules" in
		"copy")
		cp -R $kernel_modules_dir/* $TARGET/lib/modules
		;;

		"hostfs")
		echo "hostfs /lib/modules  hostfs  defaults,ro,$kernel_modules_dir 0 0" >> $TARGET/etc/fstab
		;;

		"none")
		;;

		*)
		echo "ERROR: Unknown action for 'kernel_modules', skipping kernel modules."
		;;
	esac
fi
