#! /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)
#	0.9: Dec. 25, 2022 (renewal)
#

# あらかじめ $DEBUG=YES だと、実際に書き込みはしなくなる
#export DEBUG=YES

# とりあえず日本語でなくていいし
export LANG=C

# カレントディレクトリのスクリプトを使うのでパス設定
export PATH=".:$PATH"

# 消去単位 (32Mバイト単位)
export CHUNKS=$(( 32 * 1024 * 1024 ))

# パラメータ設定
#disk=sda	# ubuntu, puppy
#disk=hda	# slax
export DEVICES='sd? hd? mmcblk?'
stuffs='10'

# OS依存
export DUMMYREADCMD=
# BSD判定
if sed -n /BSD/p /COPYRIGHT > /dev/null 2>&1 ; then
	export FLAVOR=BSD
	export DEVICES='da? ada? ad?'
	export DUMMYREADCMD='sh dummyread.sh'
fi
dummyreadcmd="$DUMMYREADCMD"

# デフォルトのディスクを探す
#disks=`ls -1 $DEVICES 2>/dev/null | sed 's/^.dev.//' | tr '\012' ' '`
#for d in $disks ; do
#	dkinfo=`sh diskinfo.sh $d`
#	case x"$dkinfo" in
#	    x)
#		;;
#	    x*)
#		break
#		;;
#	esac
#done
#disk="$d"

# stuff と check をコンパイル
cc -Wall -o stuff stuff.c
cc -Wall -o check check.c

stuffcmd=./stuff
if [ ! -x ./stuff ]; then
	stuffcmd='sh stuff.sh'
fi

export PATTERNFIL=/tmp/pattern.dat
$stuffcmd -g > $PATTERNFIL

(cd /dev; ls $DEVICES > /tmp/disks 2>/dev/null)
disk=`sed -n 1p /tmp/disks`

bytes=`sh disksize.sh -b $disk`
cbytes=`sh disksize.sh -c $disk`
cap=`sh disksize.sh -s $disk`


# ディストリビューションの違い
# 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

# 
resultfil=/tmp/result
dumpskip=0

while true ; do
	#dkinfo=`sh diskinfo.sh $disk 2>/dev/null`
	echo '*** Hard Disk wiping tool "wipe-out" ***'
	echo ''
	echo "Current Disk $disk ($cap, $cbytes bytes)"
	echo ''
	echo '    0: write "0" onto the disk'
	echo '    1: write "1" onto the disk'
	echo '    2: erase data with multiple writing'
	echo '    3: select multi-write method'
	echo '    4: verify disk (quick)'
	echo '    5: verify disk (normal)'
	echo '    6: write erase log onto the disk'
	echo '    7: select disk'
	echo '    8: dump sector'
	echo '    9: set sector number to dump'
	echo '    a: write random data onto the disk'
	echo '    b: run shell (sh)'
	echo '    c: reboot'
	echo '    d: shutdown'
	echo '    e: exit'
	echo ''
	echo -n 'enter number [0-8a-e]: '
	$dummyreadcmd
	read ans

	case x"${ans}" in
		x0)
			sh erase.sh -0 $disk
			;;
		x1)
			sh erase.sh -1 $disk
			;;
		x2)
			sh fine-erase.sh $stuffs $disk
			;;
		x3)
			. sel-fine.sh
			;;
		x4)
			sh verify.sh 90 $disk
			;;
		x5)
			sh verify.sh 0 $disk
			;;
		x6)
			echo "Sorry, not implemented"
			;;
		x7)
			. seldisk.sh
			;;
		x8)
			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
			;;
		x9)
			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`
			;;
		xa)
			sh erase.sh -r $disk
			;;
		xb)
			/bin/sh -i
			;;
		xc)
			$rebootcmd
			;;
		xd)
			$poweroffcmd
			;;
		xe)
			exit 0
			;;
	esac
	echo ''
	echo ''
done

exit 0

# EOF
