# !/bin/sh
#
# Copyright Matthias Hentges (c) 2005
#
# License: GPL (see http://www.gnu.org/licenses/gpl.txt for a copy of the GPL)


M_TITLE="USB Attached Storage"

test "$USB_HOST_AVAILABLE" = "yes" || exit 0
       
# This function is activated by init.altboot by calling this script with the "run" option
run_module() {
	
	test -e /etc/altboot.func && . /etc/altboot.func || die "ERROR: /etc/altboot.func not found. Check your installation!"
	
	# Mount /proc, etc
	init_rootfs

	echo -e "\nStarting USB..."
	
	for module in $USB_STORAGE_MODULES
	do
		echo -en "\t - $module: "
		modprobe "$module" >/dev/null 2>&1 && echo ok || die "Failed to modprobe [$module]"
	done

	echo ""
	scan_devices
	usb_show_menu 		
}

scan_devices() {
	reset_pref "available_devices"
	reset_pref "available_devices_short"
	reset_pref "available_devices_type"
	reset_pref "available_partitions"

	cnt=1	
	
	for scsi_disk in `ls -1 /sys/block/ | grep ^sd`
	do
		scsi_disk_name="`echo "$scsi_disk" | awk '{printf("%s\n",toupper($0))}'`"
		test -e /sys/block/$scsi_disk/device/vendor && HDD_VENDOR="`cat /sys/block/$scsi_disk/device/vendor | sed "s/\ $//"`" 
		test -z "$HDD_VENDOR" && HDD_VENDOR="Unknown Vendor"

		test -e /sys/block/$scsi_disk/device/model && HDD_MODEL="`cat /sys/block/$scsi_disk/device/model`" 
		test -z "$HDD_MODEL" && HDD_MODEL="Unknown Model"

		scsi_disk_partition_cnt="`ls -1 /sys/block/$scsi_disk | grep ^$scsi_disk | wc -l | tr -d " "`"
		test "$scsi_disk_partition_cnt" -gt 1 && scsi_disk_partition_cnt="$scsi_disk_partition_cnt partitions" || scsi_disk_partition_cnt="$scsi_disk_partition_cnt partition"
		
		set_pref "available_devices" "$cnt" "$scsi_disk_name ( $HDD_VENDOR $HDD_MODEL with $scsi_disk_partition_cnt)"
		set_pref "available_devices_short" "$cnt" "$scsi_disk"	
		set_pref "available_devices_type" "$cnt" "usb"	
		
		let cnt=$cnt+1
	done
	
}

usb_show_menu() {		

	cnt2=0 ; let cnt=$cnt-1	
	while test "$cnt" != "$cnt2"
	do
		let cnt2=$cnt2+1
		get_pref "available_devices" "$cnt2" dev
		
		echo -e "\t[$cnt2] - $dev"			
	done
	
	selected_dev=""
	while test -z "$selected_dev"
	do	
		echo -en "\nYour choice: "
		read junk
		get_pref "available_devices" "$junk" selected_dev
		get_pref "available_devices_type" "$junk" part_mode		
	done
	
	get_pref "available_devices_short" "$junk" selected_dev
#		debug_echo "show_menu(): selected_devices_short: [$selected_dev]"

	partitions="`ls -1 /sys/block/$selected_dev|grep ^$selected_dev`"
	test -z "$partitions" && die "No partitions found on /dev/$selected_dev!"


	echo -e "\nPlease select a partition on $selected_dev to boot from:\n"

	cnt=1
	for partition in $partitions
	do			
		# We assume that partitions with a "size" < 10 are extended partitions
		# and should not be listed. TYPE=swap is blacklisted as well.
		part_size="`cat /sys/block/$selected_dev/$partition/size`"
		part_type="`blkid -s TYPE -o value /dev/$partition`"		

		if test "$part_size" -gt 10 -a "$part_type" != "swap"
		then
			let part_size="($part_size/2)/1000"
			echo -e "\t[$cnt] $partition (~ ${part_size}Mb, $part_type)"	
			set_pref "available_partitions" "$cnt" "$partition"

			let cnt=$cnt+1			
		fi
	done

	selected_partition=""
	while test -z "$selected_partition"
	do
		echo -en "\nYour choice: "	
		read junk	
		get_pref "available_partitions" "$junk" selected_partition						
	done

	part_uuid="`blkid -c /dev/null -s UUID -o value /dev/$selected_partition`"

	select_boot_mode "$part_mode" "$selected_partition" "$part_uuid"		

	remember_last_setting "$MENU_POSITION $part_name $part_uuid $pivot_mode $pivot_target"				
}	

boot_from() {
#	debug_echo "boot_from() [$*]"
	x=y
}

copy_rootfs() {
	debug_echo "copy_rootfs() [$*]"
}

exec_module() {
	debug_echo "exec_module() [$*]"
	if test -n "$1"
	then
		# Only required when launched via auto-boot

		part_name="`echo "$1" | awk '{print $1}'`"
		part_uuid="`echo "$1" | awk '{print $2}'`"
		pivot_mode="`echo "$1" | awk '{print $3}'`"
		pivot_target="`echo "$1" | awk '{print $4}'`"

		init_rootfs

		for module in $USB_STORAGE_MODULES
		do
			echo -en "\t - $module: "
			modprobe "$module" >/dev/null 2>&1 && echo ok || die "Failed to modprobe [$module]"
		done

	fi	

	debug_echo "name: [$part_name] uuid: [$part_uuid] mode: [$pivot_mode] target: [$pivot_target]"

	sleep "$USB_STORAGE_WAIT"

	if test -n "$part_uuid" 
	then
		if ! ( safe_uuid_mount "$part_uuid" /media/usb-disk "-t auto -o defaults,noatime" )
		then
			echo "WARNING: UUID mount failed"
			safe_mount "$part_name" /media/usb-disk "-t auto -o defaults,noatime" || mdie "safe_mount failed!"
		fi
	else
		echo "WARNING: Partition has no UUID? Using [$part_name] instead"
		safe_mount "$part_name" /media/usb-disk "-t auto -o defaults,noatime" || mdie "safe_mount failed!"
	fi				

	test -e /etc/altboot.func && . /etc/altboot.func || die "ERROR: /etc/altboot.func not found. Check your installation!"

	# Check for a real fs and loop-images.
	# check_target "/media/usb-disk" >$OUT_TTY				

	fire_boot /media/usb-disk "$pivot_mode" "$pivot_target"

}

case "$1" in
title)		echo "$M_TITLE";;
run)		run_module "$2"
		test "$OFFLINE_CONFIG" != true && exec_module ;;
autorun)	exec_module "$3" ;;
flags)		echo "";;
esac