#!/bin/sh
# Author: Didier Spaier didieratslintdotfr
# Dedicated to the public doman as stated in <http://unlicense.org/>
CWD=$(pwd)
SLINTISORESCUE=$(mktemp -d)
INSTALL=$(mktemp -d)
to_lower() {
	echo "$1"|tr '[:upper:]' '[:lower:]'
}
gettext -s "Do you want to make a rescue boot stick of an USB flash drive? "
while true; do
	gettext "Please type yes or no: "
	read ANSWER
	case $(to_lower $ANSWER) in
		yes) break;;
		no) exit;;
		*) :
	esac
done
while true; do
	BEFORE=$(lsblk -lno serial,name,type|grep disk|grep ' sd'|sort)
	echo "$BEFORE" >$INSTALL/before
	gettext "Plug in the USB boot stick, then press Enter."
	read
	sleep 3
	AFTER=$(lsblk -lno serial,name,type|grep disk|grep ' sd'|sort)
	echo "$AFTER" >$INSTALL/after
	NEWSERIAL=$(diff -u $INSTALL/before $INSTALL/after|grep "^+[^+]"|sed "s/.//;s/ .*//")
	NEWCOUNT=$(echo NEWSERIAL|wc -l)
	if [ ! "$NEWSERIAL" ]; then
	echo
		gettext -s "We didn't find a newly inserted USB flash drive."
		gettext -s "Maybe it was already inserted, remove it now."
		while true; do
			gettext "If you want to try again remove it then type yes, else type no: "
			read ANSWER
			case $(to_lower $ANSWER) in
				yes) continue 2;;
				no) exit;;
				*) :
			esac
		done
	fi
	if [ $NEWCOUNT -eq 1 ]; then
		lsblk -pPo name,size,vendor,model,serial,type|grep disk|grep $NEWSERIAL > $INSTALL/rescuebootstick
		. $INSTALL/rescuebootstick
		echo
		gettext -s "This drive will be used as rescue boot stick."
		echo "$NAME $VENDOR $MODEL ($SIZE)"
		gettext -s  "If you accept all data currently stored on it will be lost!"
		gettext -s "You will confirm that want to use this one, choose another one or give up"
		while true; do
			gettext "Type yes to use it, no to try another one or q to give up: "
			read ANSWER
				case $(to_lower $ANSWER) in
					yes) break 2;;
					no) gettext "Remove this USB flash drive, then press Enter: "; read; continue 2;;
					q) exit;;
					*) :
				esac
		done
	fi
	if [ $NEWCOUNT -gt 1 ]; then
		echo
		gettext -s "Several newly inserted drives have been detected!"
		gettext -s "No rescue boot disk can be made now."
		gettext -s "Press Enter to continue"
		read
		return
	fi
done
echo
gettext -s "Please wait while we prepare the booting equipment..."
RESCUEPATH=$(lsblk -po name,serial|grep "$NEWSERIAL"|cut -d' ' -f1)
# We are not going to install two generic kernels, so LINUX will resolve
# to only one. Otherwise we'd have needed a loop.
KERNELVERSION=$(uname -r)
KERNEL=$(sed "s@ .*@@;s@.*/@@" /proc/cmdline)
INITRD=$(echo $KERNEL|sed "s@vmlinuz@initrd@")
ROOTUUID=$(findmnt -lno UUID /)
mkdir -p $SLINTISORESCUE/boot/grub
cat <<-EOF > $SLINTISORESCUE/boot/grub/grub.cfg
	# This grub.cfg is for a rescue boot stick
	set menu_color_normal=white/black
	set menu_color_highlight=white/blue
	terminal_output console
	set timeout=10
	play 480 262 1 330 1 392 1 523 1
	search --set=root --label SLINTRESCUE
	set prefix=(\$root,1)/boot/grub
	
	menuentry 'Rescue Slint' {
		echo "Starting Slint with $KERNEL. Please wait..."
		insmod part_gpt
		insmod part_msdos
		insmod btrfs
		insmod f2fs
		insmod ext2
		insmod jfs
		insmod reiserfs
		insmod xfs
		insmod all_video
		linux /$KERNEL root=UUID=$ROOTUUID vga=normal ro
		initrd /$INITRD
	}

	menuentry "Detect and boot installed operating systems" {
	set sg2d_directory=/boot/grub
	export sg2d_directory
	configfile /boot/grub/main.cfg
	}
	EOF
( cd  /boot/grub/rescue
cp osdetect.cfg tools.cfg main.cfg \
$SLINTISORESCUE/boot/grub/
)
cp /boot/$KERNEL /boot/$INITRD $SLINTISORESCUE/ || exit 1
gettext -s "Writing the booting equipment on the stick..."
grub-mkrescue --locales='' \
--themes='' --compress=xz -V "SLINTRESCUE" -iso_mbr_part_type 0x83 \
-o /tmp/rescue.iso  -partition_offset 16 -J \
--xorriso=/usr/sbin/grub-mkrescue-sed.sh $SLINTISORESCUE 1>>$INSTALL/makerescueiso.log 2>>$INSTALL/makerescueiso.log
cp $SLINT/tmp/rescue.iso $RESCUEPATH && sync
gettext -s "Done. you may now remove the rescue boot stick."
chown -R didier: $SLINTISORESCUE $INSTALL
echo
