#! /bin/sh
#
#   fine-erase.sh -- 複数回書き込みで念入りにディスクのデータを消去する
#
#	0.0: Jul. 12, 2004 by Dai ISHIJIMA (for wpout04)
#	0.1: Feb. 17, 2007 (for wpout08)
#

stuffs="${1:-fz}"
disk="${2:-ad0}"

dmesg=/var/run/dmesg.boot
dname=`sed -n -E "/^${disk}:.*<.*>/p" $dmesg | sed 's/.*\(<.*>\).*/\1/'`
bytes=`diskinfo -B /dev/r$disk`
cap=`diskinfo -p /dev/r$disk`

nstuff=0
msg=''
for stuff in `echo $stuffs | sed 's/\(.\)/\1 /g'` ; do
	case x"$stuff" in
		x[0z])	msg="${msg}0," ;;
		x[1f])	msg="${msg}1," ;;
		x[Rr])	msg="${msg}R," ;;
		x[Pp])	msg="${msg}P," ;;
		x[Qq])	msg="${msg}Q," ;;
	esac
	nstuff=$(( $nstuff + 1 ))
done
msg=`echo $msg | sed 's/,$//'`

echo ''; echo ''; echo ''
echo "*** erase data on ${disk} ${dname}, ${bytes} bytes"
echo "    with writing $nstuff times ($msg)  "

echo -n "Ok? [y/n]: "
read ans
case x"$ans" in
	xy)	;;
	*)	echo 'canceled'; exit 1;;
esac

echo -n "Really? [yes/no]: "
read ans
case x"$ans" in
	xyes)	;;
	*)	echo 'canceled'; exit 1;;
esac


# 消去のパラメータ
bytes=`diskinfo -b /dev/r$disk`
obs=$(( 1024 * 1024 ))
ibs=1024
icnt=32768
ocnt=32
max=$(( ( $bytes + $obs - 1 ) / $obs ))

#
echo ''

spc='                                                                        '
n=0
while [ $n -lt 18 ] ; do
	echo ''
	n=$(( $n + 1 ))
done
echo -e '\e[18A\c'
echo -e '\e7\c'

stage=0
xstage=1
startsec=`tinydate '+%s'`
totalrems='??? [min]'

for stuff in `echo $stuffs | sed 's/\(.\)/\1 /g'` ; do
	skip=0
	stagestart=`tinydate '+%s'`
	case "$stuff" in
		[0z])	method="'0'" ;;
		[1f])	method="'1'" ;;
		[Rr])	method="random data" ;;
		[Pp])	stuff -g > $PATTERNFIL
			method="random pattern" ;;
		[Qq])	method="inverted pattern" ;;
	esac
	rems='??? [min]'
	skip=0
	echo -e '\e8\c'
	echo -e '\e7\c'
	n=0
	while [ $n -lt 18 ] ; do
		echo "$spc"
		n=$(( $n + 1 ))
	done
	while [ $skip -le $max ]; do
		# カーソル位置
		echo -e '\e8\c'
		echo -e '\e7\c'
		# 計時
		cursec=`tinydate '+%s'`
		# バーグラフ
		p=$(( 100 * $skip / $max ))
		m=$(( 1000 '*' $skip / $max ))
		q=$(( $p / 2 ))
		i=0
		bar=''
		while [ $i -lt $q ]; do
			bar="$bar"'#'
			i=$(( $i + 1 ))
		done
		while [ $i -lt 50 ]; do
			bar="$bar"'_'
			i=$(( $i + 1 ))
		done
		#
		# 残り時間
		elapsed=$(( $cursec - $stagestart ))
		if [ $m -gt 0 ]; then
			remain=$(( ( 1000 - $m ) * $elapsed / $m + 1 ))
			if [ $remain -gt 120 ]; then
				rems=$(( ( $remain + 59 ) / 60 ))
				rems="${rems} [min]"
			else
				rems="${remain} [sec]"
			fi
		fi
		if [ $elapsed -gt 120 ]; then
			elas=$(( ( $elapsed + 30 ) / 60 ))
			elas="${elas} [min]"
		else
			elas="${elapsed} [sec]"
		fi
		#
		# 全体バーグラフ
		pp=$(( ( $p + 100 * $stage ) / $nstuff ))
		mm=$(( ( $m + 1000 * $stage ) / $nstuff ))
		q=$(( $pp / 2 ))
		i=0
		totalbar=''
		while [ $i -lt $q ]; do
			totalbar="$totalbar"'#'
			i=$(( $i + 1 ))
		done
		while [ $i -lt 50 ]; do
			totalbar="$totalbar"'_'
			i=$(( $i + 1 ))
		done
		ee=$(( $cursec - $startsec ))
		if [ $mm -gt 0 ]; then
			rr=$(( ( 1000 - $mm ) * $ee / $mm + 1 ))
			if [ $rr -gt 120 ]; then
				totalrems=$(( ( $rr + 59 ) / 60 ))
				totalrems="${totalrems} [min]"
			else
				totalrems="${rr} [sec]"
			fi
		fi
		if [ $ee -gt 120 ]; then
			totalelas=$(( ( $ee + 30 ) / 60 ))
			totalelas="${totalelas} [min]"
		else
			totalelas="${ee} [sec]"
		fi
		#

		echo "erasing data on disk: ${disk}."
		echo "stage ${xstage} of ${nstuff}, ${pp}% done   "
		echo "${totalelas} elapsed, ${totalrems} remains (approx.)   "
		echo ''
		echo "    0%|${totalbar}|100%"
		echo ''
		echo "now writing ${method} onto the disk.  ${p}% done   "
		echo "${elas} elapsed, ${rems} remains (approx.)   "
		echo ''
		echo "    0%|${bar}|100%"
		echo ''
		case x$DEBUG in
			x[Yy]*)
				echo 'now in debug mode...'
				sleep 1
				;;
			*)
				stuff -$stuff < $PATTERNFIL | dd ibs=$ibs obs=$obs seek=$skip count=$icnt of=/dev/r$disk
				;;
		esac
		# 次ループ
		skip=$(( $skip + $ocnt ))
	done
	stage=$(( $stage + 1 ))
	xstage=$(( $xstage + 1 ))
done

echo ''; echo ''; echo ''; echo ''; echo ''

cursec=`tinydate '+%s'`
elapsed=$(( $cursec - $startsec ))
if [ $elapsed -gt 120 ]; then
	min=$(( $elapsed / 60 ))
	sec=$(( $elapsed - $min * 60 ))
	elas="${min} [min] ${sec} [sec]"
else
	elas="${elapsed} [sec]"
fi

echo 'The contents of this storage device' > /tmp/erase-log.$disk
echo "$disk ($cap) '$dname'" >> /tmp/erase-log.$disk
echo -n 'were erased with ' >> /tmp/erase-log.$disk
echo 'hard disk wiping tool "wipe-out."' >> /tmp/erase-log.$disk
echo "Erase $nstuff times ($stuffs)" >> /tmp/erase-log.$disk
echo " epoch: $startsec -> $cursec" >> /tmp/erase-log.$disk

echo "but NOT verified" > /tmp/verify-log.$disk

echo "completed ($elas)"
echo ''
echo -n 'Press ENTER to return menu...'
read x

exit 0

# EOF
