# !/bin/sh
M_TITLE="Configure kexec CMDLINE"

# Deprecated due to altbootctl
exit 0


# Only kernel 2.6 offers kexec support
uname -r | grep -q "^2.6" || exit 0

run_module() {
	
	test -e /etc/altboot.func && . /etc/altboot.func || die "ERROR: /etc/altboot.func not found. Check your installation!"
	
	test -z "$KEXEC_KERNEL_DIR" && KEXEC_KERNEL_DIR="/boot"	
	test -x "$KEXEC_BIN" || mdie "kexec-tools not found [$KEXEC_BIN]"
	
	# Mount /proc, etc
	init_rootfs
	
	if test `ls -1 $KEXEC_KERNEL_DIR | grep -v "kexec.cfg" | wc -l | tr -d " "` -gt 1
	then
		echo -e "\nPlease select the kernel you want to configure:\n"	
		
		cnt=1 ; reset_pref "kexec_klist"
		for k in `ls -1 $KEXEC_KERNEL_DIR/zImage* | grep -v "kexec.cfg" `
		do
			echo -e "\t[$cnt] $k"
			set_pref "kexec_klist" "$cnt" "$k"
						
			let cnt=$cnt+1
		done 
		
		echo ""
		
		while true
		do
			echo -n "Select a kernel: "
			read junk
			
			get_pref "kexec_klist" "$junk" KEXEC_KERNEL && break

		done				
	else
		KEXEC_KERNEL="`ls -1 $KEXEC_KERNEL_DIR/zImage* | grep -v "kexec.cfg" `"
	fi
	
	echo -e "\nUsing [$KEXEC_KERNEL]"
	
	configure_rootdev
	configure_roottype
	configure_verbose
	configure_custom

	CMDLINE="console=ttyS0,115200n8 console=tty1 dyntick=enable fbcon=rotate:1 noinitrd root=$KERNEL_ROOTDEV rootfstype=$KERNEL_ROOTFSTYPE $KERNEL_VERBOSE $KERNEL_CUSTOM"
	
	echo -en "\nWriting CMDLINE to $KEXEC_KERNEL.kexec.cfg..."
	echo "$CMDLINE" > $KEXEC_KERNEL.kexec.cfg && echo ok || mdie FAILED
	
	show_menu
}

configure_rootdev() {

	echo -e "\nPlease choose a root device (root=*):\n"
	
	cnt=1 ; reset_pref "kexec_rootdev"
	for root in /dev/mtdblock2 /dev/hda1 "Manual Entry"
	do
		echo -e "\t[$cnt] $root"
		set_pref kexec_rootdev "$cnt" "$root"
		let cnt=cnt+1
	done
	
	echo ""
	
	while true
	do
		echo -n "Select a root device: "
		read junk
		
		get_pref kexec_rootdev "$junk" KERNEL_ROOTDEV && break
	done
	
	if test "$KERNEL_ROOTDEV" = "Manual Entry"
	then
		echo ""
		
		KERNEL_ROOTDEV=""
		
		while test -z "$KERNEL_ROOTDEV"
		do
			while true
			do
				echo -n "Enter the root device: "
				read junk
				test -n "$junk" && break
			done

			echo -en "\nIs [$junk] correct? [Y|n] "
			read junk2
			while true
			do
				if test "$junk2" = y -o "$junk2" = Y -o -z "$junk2"
				then
					KERNEL_ROOTDEV="$junk"
					break
				fi
				
				break
			done
		done
	fi 
	
	echo "Using [$KERNEL_ROOTDEV] as root device"
}

configure_roottype() {

	echo -e "\nPlease choose a rootfs type (rootfs=*):\n"
	
	cnt=1 ; reset_pref "kexec_rootfstype"
	for root in jffs2 ext2 ext3 "Manual Entry"
	do
		echo -e "\t[$cnt] $root"
		set_pref kexec_rootfstype "$cnt" "$root"
		let cnt=cnt+1
	done
	
	echo ""
	
	while true
	do
		echo -n "Select a rootfs type: "
		read junk
		
		get_pref kexec_rootfstype "$junk" KERNEL_ROOTFSTYPE && break
	done
	
	if test "$KERNEL_ROOTFSTYPE" = "Manual Entry"
	then
		echo ""
		
		KERNEL_ROOTFSTYPE=""
		
		while test -z "$KERNEL_ROOTFSTYPE"
		do
			while true
			do
				echo -n "Enter the rootfs type: "
				read junk
				test -n "$junk" && break
			done

			echo -en "\nIs [$junk] correct? [Y|n] "
			read junk2
			while true
			do
				if test "$junk2" = y -o "$junk2" = Y -o -z "$junk2"
				then
					KERNEL_ROOTFSTYPE="$junk"
					break
				fi
				
				break
			done
		done
	fi 
	
	echo "Using [$KERNEL_ROOTFSTYPE] as rootfs type"
}

configure_verbose() {
	echo -e "\nDo you want to see kernel messages? ([quiet | debug]):\n"
	
	echo -e "\t[1] Yes"
	echo -e "\t[2] No"
		
	echo ""
	
	while true
	do
		echo -n "Select one of the above: "
		read junk
		
		test "$junk" = 1 -o "$junk" = 2 && break
	done
	
	test "$junk" = 1 && KERNEL_VERBOSE="debug" || KERNEL_VERBOSE="quiet"
	
}

configure_custom() {
	echo -e "\nDo you want to set custom kernel options?\n"

	echo -e "\t[1] Yes"
	echo -e "\t[2] No"
		
	echo ""
	
	while true
	do
		echo -n "Select one of the above: "
		read junk
		
		test "$junk" = 1 -o "$junk" = 2 && break
	done
	
	if test "$junk" = 1
	then
		
		echo ""
		
		KERNEL_CUSTOM=""
		
		while test -z "$KERNEL_CUSTOM"
		do
			while true
			do
				echo -n "Enter kernel options: "
				read junk
				test -n "$junk" && break
			done

			echo -en "\nIs [$junk] correct? [Y|n] "
			read junk2
			while true
			do
				if test "$junk2" = y -o "$junk2" = Y -o -z "$junk2"
				then
					KERNEL_CUSTOM="$junk"
					break
				fi
				
				break
			done
		done
	fi 
	
	echo "Using [$KERNEL_CUSTOM] as custom kernel options"
	
}

case "$1" in
title)	echo "$M_TITLE";;
run)	run_module;;
esac