#! /bin/sh
#
#   wipe-out.sh -- ハードディスク消去ツールメイン (Ubuntu版)
#
#	0.0: Oct. 30, 2001 by Dai ISHIJIMA (as startup.sh)
#	0.1: Oct. 22, 2002 (for v01)
#	0.2: May   1, 2004 (for v02)
#	0.3: Jun. 19, 2004 (for v03, as mainmenu.sh)
#	0.4: Jul. 11, 2004 (for v04)
#	0.5: Jul.  9, 2005 (for v06)
#	0.6: Feb. 17, 2005 (for v08)
#	0.7: Feb.  7, 2010 (for v10-ubuntu-gpxe)
#	0.8: Feb. 16, 2010 (check ubuntu, puppy, slax)
#

export PATH=".:$PATH"
export LANG=C

#export DEBUG=YES

resultfil=/tmp/result

#disk=sda	# ubuntu, puppy
#disk=hda	# slax
export DEVICES='/dev/sd? /dev/hd? /dev/mmcblk? /dev/ad? /dev/da?'
#disk=`ls -1 $DEVICES 2>/dev/null | sed 's/^.dev.//' | head -1`

disks=`ls -1 $DEVICES 2>/dev/null | sed 's/^.dev.//' | tr '\012' ' '`
for d in $disks ; do
	dkinfo=`fdisk -l "/dev/$d" 2>/dev/null | grep "^Disk /dev/"`
	case x"$dkinfo" in
	    x)
		;;
	    x*)
		break
		;;
	esac
done
disk="$d"

# hd for ubuntu
dumpcmd=hd
# od for slax
if [ ! -x /usr/bin/hd ]; then
	dumpcmd='od -cb'
fi

poweroffcmd='shutdown -h now'
rebootcmd='shutdown -r now'
# puppy does not have shutdown command
if [ ! -x /sbin/shutdown ]; then
	poweroffcmd='poweroff'
	rebootcmd='reboot'
fi

export ESCAPECHAR=`echo -n '\033'`
n=`echo -e '\033\c' | wc -c`
if [ $n -eq 1 ]; then
	export ESCAPECHAR=`echo -e '\033\c'`
fi

dumpskip=0

while true ; do
	dkinfo=`fdisk -l "/dev/$disk" 2>/dev/null | grep "^Disk /dev/"`

	echo '*** Hard Disk wiping tool "wipe-out" ***'
	echo ''
	case x"$dkinfo" in
	    x)
		echo "Current Disk /dev/$disk"
		;;
	    x*)
		echo "Current $dkinfo"
		;;
	esac
	echo ''
	echo '    0: write "0" onto the disk'
	echo '    1: select another disk'
	echo '    2: dump sector'
	echo '    3: set sector number to dump'
	echo '    4: run shell (sh)'
	echo '    5: reboot'
	echo '    6: shutdown'
	echo '    7: exit'
	echo ''
	echo -n 'enter number [0-7]: '
	#sh dummyread.sh
	read ans

	case x"${ans}" in
	    x0)
		sh erase.sh -z $disk
		;;
	    x1)
		. seldisk.sh
		;;
	    x2)
		echo ''
		echo "*** sector $dumpskip of disk $disk"
		dd if=/dev/$disk skip=$dumpskip count=1 2>/dev/null | $dumpcmd
		dumpskip=$(( $dumpskip + 1 ))
		echo -n "Press ENTER to return menu"
		read enterkey
		;;
	    x3)
		echo ''
		echo -n "***Enter sector number [$dumpskip] :"
		read sector
		echo "$sector" > /tmp/sector
		echo "$dumpskip" >> /tmp/sector
		dumpskip=`sed -n '/^[0-9]/p' /tmp/sector | sed -n 1p`
		;;
	    x4)
		/bin/sh -i
		;;
	    x5)
		$rebootcmd
		;;
	    x6)
		$poweroffcmd
		;;
	    x7)
		exit 0
		;;
	esac
	echo ''
	echo ''
done

exit 0

# EOF
