#!/bin/dash
# (C) James Budiono 2013
# License: GPL Version 3 or later
#
# $1-iso $-save path

### source dirs
ISOPATH=${1:-/mnt/sda5/holding/misc-iso/slacko-5.5.70-SCSI.iso}
SAVEDIR=${2:-/root/slacko}

### work dirs
SFS_RAMDISK=/mnt/ramdisk # where to create the ramdisk
MOUNT_ROOT=/mnt/slacko # all other mount points will be created here

### mountpoints
AUFS_ROOT=$MOUNT_ROOT/root
SFS_MOUNT=$MOUNT_ROOT/sfs
ADRIVE_MOUNT=$MOUNT_ROOT/adrive
ZDRIVE_MOUNT=$MOUNT_ROOT/zdrive
DEVX_MOUNT=$MOUNT_ROOT/devx

# clean-up when killed
trap 'cleanup; exit' INT QUIT TERM 0

### clean-up everything
cleanup() {
	echo cleaning up ...
	for p in $AUFS_ROOT/dev/shm $AUFS_ROOT/dev/pts $AUFS_ROOT/dev \
	         $AUFS_ROOT/proc $AUFS_ROOT/sys $AUFS_ROOT/sys $AUFS_ROOT/tmp $AUFS_ROOT \
	         $SFS_MOUNT $ADRIVE_MOUNT $ZDRIVE_MOUNT $DEVX_MOUNT $SFS_RAMDISK; do
	    if mountpoint -q $p; then
			fuser -M -k $p 
			umount -l $p
	    fi
	done
}


### mount proc dev sys tmp 
system_mounts() {
	echo mounting system mounts  ...
	mkdir -p $AUFS_ROOT/dev $AUFS_ROOT/proc $AUFS_ROOT/sys $AUFS_ROOT/tmp $AUFS_ROOT/etc
	mount --rbind /dev $AUFS_ROOT/dev
	for p in proc sys tmp; do
		mount --bind /$p $AUFS_ROOT/$p
	done
	cp /root/.Xauthority $AUFS_ROOT/root
	ln -sf /proc/mounts $AUFS_ROOT/etc/mtab
}

### mount sfs-es
sfs_mounts() {
	echo mounting sfs ...
	mkdir -p $SFS_MOUNT $ADRIVE_MOUNT $ZDRIVE_MOUNT $DEVX_MOUNT
	mount -o ro,loop $SFS_RAMDISK/puppy_slacko*.sfs $SFS_MOUNT
	[ -e $SFS_RAMDISK/adrive.sfs ] && mount -o ro,loop $SFS_RAMDISK/adrive.sfs $ADRIVE_MOUNT
	[ -e $SFS_RAMDISK/zdrive.sfs ] && mount -o ro,loop $SFS_RAMDISK/zdrive.sfs $ZDRIVE_MOUNT
	[ -e $SFS_RAMDISK/devx.sfs ] && mount -o ro,loop $SFS_RAMDISK/devx.sfs $DEVX_MOUNT
}

### make aufs union
prepare_union() {
	echo preparing union ...
	local branches
	mkdir -p $SAVEDIR $AUFS_ROOT
	
	branches=br:$SAVEDIR
	for p in $SFS_MOUNT $ADRIVE_MOUNT $ZDRIVE_MOUNT $DEVX_MOUNT; do
		mountpoint -q $p && branches=$branches:$p
	done
	if ! mount -t aufs aufs -o $branches $AUFS_ROOT 2>/dev/null; then
		# savedir may be under aufs already, try creating a tmpfs there and mount again
		mount -t tmpfs tmpfs $SAVEDIR
		mount -t aufs aufs -o $branches $AUFS_ROOT
	fi
}

### copy SFS from ramdisk
prepare_ramdisk() {
	echo preparing ramdisk ...
	mkdir /tmp/xxx.$$	
	if mount -o ro,loop $ISOPATH /tmp/xxx.$$; then
		mkdir -p $SFS_RAMDISK
		mount -t tmpfs tmpfs $SFS_RAMDISK
		cp /tmp/xxx.$$/*.sfs $SFS_RAMDISK
		umount /tmp/xxx.$$
		rmdir /tmp/xxx.$$
		return 0
	else
		rmdir /tmp/xxx.$$
		return 1
	fi
}

### patch .profile to do first-run activities
patch_profile() {
	if ! grep -q gdk-pixbuf-query-loaders $AUFS_ROOT/root/.profile; then
		echo patching .profile
		mv $AUFS_ROOT/root/.profile $AUFS_ROOT/root/.profile-old
		{ 
			echo "echo Entering slacko chroot ..."
			echo "PS1='slacko# '"
			echo "cd /root"
			echo "! [ -e /root/.gtkdone ] && gdk-pixbuf-query-loaders > /etc/gtk-2.0/gdk-pixbuf.loaders"
			echo "touch /root/.gtkdone"			
			echo "export DISPLAY=:0"
			cat $AUFS_ROOT/root/.profile-old
		} > $AUFS_ROOT/root/.profile
	fi
}


##################################
############# main ###############
! prepare_ramdisk && echo "Cant load base sfs" && exit
sfs_mounts
! prepare_union && echo "Can't make union" && exit
system_mounts
patch_profile
linux32 chroot $AUFS_ROOT /bin/sh --login
#cleanup # activated by trap

