#!/bin/sh 

uninstall=false
if [ "$1" = "-u" ] ; then
	uninstall=true
	shift
fi

mnt=$1
dev=$2
fstype=$3

if [ -n "$4" ] ; then
	nblocks=$4
else
	nblocks=1440
fi

if [ "$mnt" = "" -o "$dev" = "" ] ; then
	echo "usage: $0 [-u] <dir> <device> [<fstype> [<blocks>]]"
	exit 1
fi

if [ ! -d $mnt ] ; then
	echo "error: $mnt is not a mount point"
	exit 1
fi

if [ ! -b $dev ] ; then
	echo "error: $dev is not a block device"
	exit 1
fi

if [ ! -w $dev ] ; then
	echo "error: cannot write to $dev"
	exit 1
fi

set -e

cp="/bin/cp -f -p"	# preserve dates and other info while copying
sync=/bin/sync
mount=/bin/mount
umount=/bin/umount
mformat=/usr/bin/mformat
mkfs=/sbin/mkfs
strip=/usr/bin/strip
mkdir=/bin/mkdir

if [ "$mnt" != "/" ] ; then
	if [ -n "$fstype" ] ; then
		echo -n "Unmounting $dev: "
		($umount $dev)
		if [ $? = 0 ] ; then
			echo "Ok"
		fi
		if [ $fstype = "msdos" -a $dev = "/dev/fd0" ] ; then
			$mformat a:
		elif [ $fstype = "msdos" -a $dev = "/dev/fd1" ] ; then
			$mformat b:
		else
			$mkfs -t $fstype $dev $nblocks
		fi
	fi

	mounted=false
	(grep $dev /etc/mtab) > /dev/null
	if [ $? = 0 ] ; then
		(grep "$dev[ ]\+$mnt" /etc/mtab) > /dev/null
		if [ $? = 0 ] ; then
			echo "$dev already mounted on $mnt"
			mounted=true
		else
			echo "error: $dev already mounted elsewhere"
			exit 1
		fi 
	fi
	if [ $mounted = false ] ; then
		(grep $mnt /etc/fstab) | (grep $dev) > /dev/null
		if [ $? = 0 ] ; then
			echo -n "Trying to mount $dev: "
			($mount $mnt)
		else
			echo -n "Trying to mount $dev as minix: "
			($mount $dev $mnt -t minix)
			if [ $? != 0 ] ; then
				echo -n "Trying to mount $dev as msdos: "
				($mount $dev $mnt -t msdos)
				if [ $? != 0 ] ; then
					echo -n "Trying to mount $dev as ext2: "
					($mount $dev $mnt -t ext2)
				fi
			fi
		fi
		if [ $? != 0 ]; then
			echo "error: cannot mount $dev"
			exit 1
		else
			echo "Ok"
		fi
	fi
fi

if [ $uninstall = true ] ; then
	./mksolo -u -d -b $dev -r $mnt sys/solo.cnf
	if [ $mnt != "/" ] ; then
		($umount $mnt)
		if [ $? != 0 ] ; then
			echo "error: cannot unmount $dev"
			exit 1
		fi
	fi
	exit 0
fi


BOOTBLOCK=`grep "bootblock=" solo.cnf | sed "s/bootblock=//" \
		| sed "s/#.*$//"` 
SAVEBOOT=`grep "saveboot=" solo.cnf | sed "s/saveboot=//" \
		| sed "s/#.*$//"` 
MAP=`grep "map=" solo.cnf | sed "s/map=//" \
		| sed "s/#.*$//"` 
LOADER=`grep "loader=" solo.cnf | sed "s/loader=//" \
		| sed "s/#.*$//"` 
SYSFILES=`grep "image=" solo.cnf | sed "s/image=//" \
		| sed "s/#.*$//"` 

if [ ! -d $mnt/sys ]; then $mkdir $mnt/sys; fi
if [ ! -d $mnt/drv ]; then $mkdir $mnt/drv; fi

remove=

base=`basename $BOOTBLOCK`
if [ -r $base ] ; then
	($cp $base $mnt/$BOOTBLOCK)
	if [ $? != 0 ] ; then
		cp="/bin/cp -f"
		echo "warning: unable to preserve file dates/permissions"
	fi
else
	echo "error: missing '$BOOTBLOCK'"
	exit 1
fi

for file in $BOOTBLOCK $LOADER $SYSFILES; do
	base=`basename $file`
	if [ $file = $SAVEBOOT -o $file = $MAP ] ; then
		continue
	elif [ -r $base.x ] ; then
		$cp $base.x $mnt/$file
		$strip $mnt/$file
	elif [ -r $base ] ; then
		$cp $base $mnt/$file
	elif [ $base = "vmlinuz" -a -r /vmlinuz -a $mnt != / ] ; then
		$cp /vmlinuz $mnt/$file
	elif [ $base = "mkern" -a -r ../mkern/mkern.x ] ; then
		$cp ../mkern/mkern.x $mnt/$file
		$strip $mnt/$file
	elif [ -r $mnt/$file ] ; then
		echo "warning: using old '$file'"
	else
		echo "warning: missing '$file'"
		remove="$remove $file"
	fi
done

if [ "$remove" != "" ] ; then
	echo "removing missing files from '$mnt/sys/solo.cnf'..."
	remove=`echo $remove | sed "s/\//\\\\\\\\\//g"`
	for file in $remove; do
		cat $mnt/sys/solo.cnf | sed "s/image=$file/#image=$file/" \
			> $mnt/sys/solocnf.tmp
		mv $mnt/sys/solocnf.tmp $mnt/sys/solo.cnf
	done
fi

$sync
cmd="./mksolo -d -b $dev -r $mnt sys/solo.cnf"
($cmd)
exitcode=$?
if [ $exitcode != 0 ] ; then
	echo "failed with code $exitcode: $cmd"
fi

./readmap $mnt/sys/solo.map > solomap.out

if [ $mnt != "/" ] ; then
	($umount $mnt)
	if [ $? != 0 ] ; then
		echo "error: cannot unmount $dev"
		exit 1
	fi
fi

exit $exitcode
