#!/bin/ash
LIVELABEL="LiveSystem" #edit in build-live-system.sh script too
if [ -z "$livelabel" ]; then livelabel=$LIVELABEL; fi #undocumented

source /init.inc
echo ""
echo "*** Live system initialization ***"
initialization
mkdir /live
mount -t tmpfs tmpfs /live

#mount live media
mkdir /live/media
mediadetected="none"
if [ ! -z "$nfsroot" ]; then
	mediadetected="nfs"
	address=`echo $ip | cut -f1 -d:`
	netmask=`echo $ip | cut -f4 -d:`
	gateway=`echo $ip | cut -f3 -d:`
	ifconfig eth0 $address netmask $netmask
	route add default gw $gateway
	mount -t nfs -o nolock $nfsroot /live/media
	echo "$livelabel found on $nfsroot"
else
	sleeptime=0
	while [ "$mediadetected" = "none" ] && [ "$sleeptime" != "10" ]; do #try each seconds, but don't wait (USB) more than 10 seconds
		if blkid | grep -q "LABEL=\"$livelabel\""; then
			livedevice=`blkid | grep "LABEL=\"$livelabel\"" | sed -n 1p | cut -f1 -d:`
			echo "$livelabel found on $livedevice"
			mount -o ro $livedevice /live/media
			if blkid | grep "LABEL=\"$livelabel\"" | sed -n 1p | grep -q "TYPE=\"iso9660\""
			then mediadetected="cd"
			else mediadetected="sd"
			fi
		else
			sleep 1
			let sleeptime+=1
		fi
	done
fi
if [ "$mediadetected" = "none" ]; then 
	echo -e "\nError: $livelabel not found"
	exec sh
fi

#detect modules and mount squashfs ones in /live/modules/*
mkdir /live/modules
modulescount=0
if [ `ls /live/media/boot/modules/ | wc -l` != 0 ]; then
	for module in /live/media/boot/modules/*; do
		modulename=`basename $module`
		if ! echo $exclude | sed 's/:/\n/g' | grep -q "^$modulename$"; then
			if [ -d $module ]; then
				modulesbranches="$module:$modulesbranches"
			else	
				mkdir /live/modules/$modulename
				mount -o loop -t squashfs $module /live/modules/$modulename
				modulesbranches="/live/modules/$modulename:$modulesbranches"
			fi
			echo "Loading SquashFS module $modulename"
			modulescount=$(($modulescount+1))
		fi
	done
fi
modulesbranches=`echo $modulesbranches | sed 's/:$//'`
if [ $modulescount = 0 ]; then 
	echo -e "\nError: no modules has been loaded"
	exec sh
fi

#mount (union) all modules in /live/system (ro)
mkdir /live/system
if [ $modulescount = 1 ]
then mount --bind $modulesbranches /live/system
else mount -t overlay -o ro,lowerdir=$modulesbranches overlay /live/system
fi

#mount (union) all modules (ro) and /live/changes (rw) in /live/union (rw)
mkdir /live/.workdir
mkdir /live/changes
mkdir -p /mnt
mount -t overlay -o workdir=/live/.workdir,upperdir=/live/changes,lowerdir=$modulesbranches overlay /mnt

#setup system tree
mkdir -p /mnt/tmp
mkdir -p /mnt/sys
mkdir -p /mnt/proc
mkdir -p /mnt/dev
mkdir -p /mnt/live
cat > /mnt/etc/fstab << END
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
none / tmpfs defaults 0 0
END

tweak_system

#setup persistent homedir
if [ ! -z "$home" ] && [ "$mediadetected" != "cd" ]; then
	if [ "$mediadetected" = "nfs" ]; then
		if mount -t nfs -o nolock $home /mnt/home; then
			echo "$home /home nfs nolock 0 0" >> /mnt/etc/fstab
		fi
	else
		home=`blkid | grep "\"$home\"" | cut -f1 -d:` #label -> device
		if [ ! -z "$home" ]; then #if found
			if mount $home /mnt/home; then
				fstype=`cat /etc/fstab | grep $home | cut -f3 -d' '`
				echo "$home /home $fstype defaults 0 0" >> /mnt/etc/fstab
			fi
		fi
	fi
	if mount | grep -q /mnt/home; then
		echo "Setting up persistent home directory ($home)"
		for entry in `cat /mnt/etc/passwd | grep /home | sed /^ftp/d`; do
			uid=`echo $entry | cut -f3 -d:`
			gid=`echo $entry | cut -f4 -d:`
			homedir=`echo $entry | cut -f6 -d:`
			if [ ! -d /mnt/$homedir ]; then
				mkdir /mnt/$homedir
				cp -dpR /mnt/etc/skel/.??* /mnt/$homedir
				cp -dpR /mnt/etc/skel/* /mnt/$homedir
				chown -R $uid:$gid /mnt/$homedir
			fi
		done
		umount /mnt/home
	fi
fi

#mtab + bind mounts
cat /etc/mtab | sed 's@overlay /mnt@overlay /@' > /mnt/etc/mtab
mount -o rbind /live /mnt/live

#system i18n setup
setup_locale
setup_time
setup_console_keymap
if [ ! -z "$keymap" ]; then
	if [ -d /mnt/etc/X11/xorg.conf.d ]; then
		xkblayout=`echo $keymap | cut -c1-2`
		echo "Setting up Xorg keymap layout ($xkblayout)"
		cat > /mnt/etc/X11/xorg.conf.d/00-keyboard.conf << END
Section "InputClass"
	Identifier "keyboard-all"
	Option "XkbLayout" "$xkblayout"
	MatchIsKeyboard "on"
EndSection
END
	fi
fi

finalization
echo "*** Live system ready ***"
echo ""

#chroot to live system
exec switch_root /mnt /sbin/init || exec sh
