# $NetBSD: livecd,v 1.9 2012/10/27 21:42:36 zafer Exp $ 
#
# Mount ramfs directories and unpack the required files
# before anything.

# PROVIDE: mklivecd
# BEFORE: DISKS

$_rc_subr_loaded . /etc/rc.subr

name="livecd"
rcvar=$name

start_cmd="livecd_start"
stop_cmd="livecd_stop"

livecd_start()
{
	if [ -f /stand/usr.zfs ]; then
		/rescue/vnconfig -z vnd0 /stand/usr.zfs
		/rescue/mount -r /dev/vnd0a /usr
        fi

	echo -n "=> Mounting memory filesystem directories: "

	echo "dev, "
	/rescue/@MNT_RAMFS_CMD@ @MNT_RAMFS_ARGS@ /dev

	/rescue/@MNT_RAMFS_CMD@ @MNT_RAMFS_ARGS@ /ramfs

	for f in etc root tmp var
	do
		/rescue/mkdir /ramfs/$f
		[ "$f" = "tmp" ] && /rescue/chmod 01777 /ramfs/$f
		/rescue/mount_null /ramfs/$f /$f
		if [ "$f" = "var" ]; then
			echo "/$f."
		else
			echo -n "/$f, "
		fi
	done

	@HOME_MKDIR@
	@HOME_MOUNT@
	@HOME_UNPACK@
	@USRPKGETC_MKDIR@
	@USRPKGETC_MOUNT@
	@USRPKGETC_UNPACK@

	for f in dev etc var root
	do
	    /bin/tar xfzp /stand/ramfs_$f.tgz -C / > /dev/null 2>&1
            if [ "$?" -eq 0 ]; then
                echo "=> $f unpacked successfully."
            else
                echo "=> $f not unpacked correctly!"
            fi
	done

	if [ -f /stand/var_db_pkg.zfs ]; then
		/rescue/vnconfig -z vnd1 /stand/var_db_pkg.zfs
		/rescue/mount -r /dev/vnd1a /var/db/pkg
	fi
}

livecd_stop()
{
	echo "Please don't do that :-)"
}

load_rc_config $name
run_rc_command "$1"
