#!/bin/sh

#========================================
#
# usbfadm - USB Flash ADMinistration tool
# KAWAMATA, Yoshihiro / kaw@on.rim.or.jp
#
#========================================


echo 'Welcome to USB flash maintenance tool.'
echo ''

#-------------------
# initialization - read status files
#
if [ -r /boottmp/boot_mode ]; then
    mode=`cat /boottmp/boot_mode`
    echo "     Boot mode: $mode"
fi

if [ -r /boottmp/boot_restore_devname ]; then
    devname=`cat /boottmp/boot_restore_devname`
    echo "Data stored in: $devname"
fi

if [ -r /boottmp/boot_user_config ]; then
    uconf=`cat /boottmp/boot_user_config`
    echo " Data Set Name: $uconf"
fi
echo ''
echo 'Type ? for help.'

#-------------------
# command loop
#
while :; do
    #-------------------
    # setup prompt string
    #
    d="$devname" ; [ -z "$devname" ] && d='?'
    u="$uconf"   ; [ -z "$uconf" ]   && u='?'
    echo ''
    echo -n "$d:$u -> "; read cmd
    echo ''

    set X $cmd

    #-------------------
    # process every command
    #
    case X"$2" in
        #-------------------
        # finish all
        #
        Xquit|Xbye|Xexit)
	break;
	;;

        #-------------------
        # write back memory file system
        # to USB flash disk
        #
	Xsync)
	echo -n "Sync current mfs as $uconf, OK? -> "; read yn
	if [ 1 = `expr "$yn" : '[Yy]$'` ]; then
	    if mount -o softdep $devname /mnt; then
		echo ''
		rsync -av --delete /mfs/. /mnt/livecd-config/$uconf
		# find /mnt \! -type d \! -type f \! -type l -print | xargs rm -f
		rm -rf /mnt/livecd-config/$uconf/tmp/{.??*,*}
		umount $devname
	    fi
	fi
	;;

        #-------------------
        # show status of USB memory
        #
	Xinfo)
	if mount -r $devname /mnt; then
	    (cd /mnt/livecd-config && echo ''; df -h /mnt; echo "\nscanning...\n"; du -sh *)
	    umount $devname
	fi
	;;

        #-------------------
        # set data set name
        # default is our FQDN.
        #
        # strip invalid chars
        #
        Xdsn)
	if [ X"$3" = X ]; then
	    echo -n "Enter data set name [`hostname`] -> "; read uconf
	    [ -z "$uconf" ] && uconf=`hostname`
	else
            uconf="$3"
	fi
	uconf=`echo $uconf | tr -cd 0-9A-Za-z.,_-+=:%@`
        echo ''
	echo "Now data set name is set to \`\`$uconf''."
        echo "$uconf" > /boottmp/boot_user_config
        ;;

        #-------------------
        # search and set USB flash
        # disk device from dmesg strings
        #
        Xrescan)
	echo 'Rescanning USB flash'
	echo 'Please make sure the device inserted.'
	echo -n 'Then press ENTER -->'; read dummy

	set X `dmesg | grep '^scsibus[0-9] at umass[0-9]'`
	if [ $# -gt 2 ]; then
		usb_scsi_umass=${2%:}
		usb_umass=$4

		set X `dmesg | grep '^sd[0-9] at scsibus[0-9]'`
		if [ $# -gt 2 ]; then
			usb_sd=$2
			usb_sd_scsi=$4
			if [ "$usb_scsi_umass" = "$usb_sd_scsi" ]; then
			    echo ''
			    echo "USB flash memory found as $usb_sd"
			    devname=/dev/${usb_sd}a
			else
			    echo "SCSI bus between sd($usb_sd at $usb_sd_scsi) and umass($usb_scsi_umass at $usb_umass) not match"
			fi
		else
		    echo 'SCSI-emulated USB disk storage not found'
		fi
	else
	    echo 'USB mass storage device not found'
	fi
        ;;

        #-------------------
        # null command
        # ... only RET or EOF
        X)
        : # do nothing
	;;

        #-------------------
        # other strings are invalid
        # then for help message
        #
	*)
        echo "Commands are;
    sync    -  sync current mfs to USB flash
    info    -  Show info on USB flash
    rescan  -  search USB flash device
    dsn     -  set data set name
    bye, exit, quit
            - end of this utility"
        ;;
    esac
done
