#! /bin/sh
#
#   writelog.sh -- 消去ログをディスクに書き込む
#
#	使い方: /bin/sh writelog.sh ディスク
#		ディスク: デバイスファイルで指定、先頭の「/dev/r」は省く
#		例: /bin/sh writelog.sh ad0
#
#	0.0: Aug.  1, 2006 by Dai ISHIJIMA (for wpout07/cdrom)
#	0.1: Feb. 17, 2007 (for wpout08/floppy)
#

disk="${1:-ad0}"
mbr="${MBR:-wpout-mbr.boot}"

echo ''; echo ''; echo ''
echo "*** Write erase log onto the disk ($disk)"
echo -n "Ok? [y/n]: "
read ans
case x"$ans" in
	xy)	;;
	*)	echo 'canceled'; exit 1;;
esac

# すでに消去済みか?
if [ ! -s /tmp/erase-log.$disk ]; then
	echo -n 'No erase log found.  Sure? [y/n]: '
	read ans
	case x"$ans" in
		xy)	;;
		*)	echo 'canceled'; exit 1;;
	esac
fi

# 本当に消去されているのか?
dd if=/dev/r$disk count=1 of=/tmp/org-mbr.$disk 1>/dev/null 2>&1
if ! check < /tmp/org-mbr.$disk 1>/dev/null 2>&1 ; then
	echo -n 'MBR is not erased with 0.  Sure? [y/n]: '
	read ans
	case x"$ans" in
		xy)	;;
		*)	echo 'canceled'; exit 1;;
	esac
fi

# ログが存在しなかったらそれらしく作成
if [ ! -s /tmp/erase-log.$disk ]; then
	echo 'The contents of this storage device' > /tmp/erase-log.$disk
	echo 'may have been erased' >> /tmp/erase-log.$disk
	echo 'with hard disk wiping tool "wipe-out."' >> /tmp/erase-log.$disk
fi

if [ ! -s /tmp/verify-log.$disk ]; then
	echo 'But NOT verified.' > /tmp/verify-log.$disk
fi

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

echo -n 'Logged, epoch: ' > /tmp/stamp-log.$disk
tinydate >> /tmp/stamp-log.$disk
echo 'ABSOLUTELY NO WARRANTY.' >> /tmp/stamp-log.$disk


# 書き込み操作
(dd if=$mbr 2>/dev/null; dd if=/tmp/erase-log.$disk 2>/dev/null; \
	dd if=/tmp/verify-log.$disk 2>/dev/null; \
	dd if=/tmp/stamp-log.$disk 2>/dev/null ) |
	makembr > /tmp/boot.$disk

if dd if=/tmp/boot.$disk of=/dev/r$disk ; then
	echo "MBR log was written onto the disk."
else
	echo "unable to write log onto the disk."
fi

#
echo ''
echo -n 'Press ENTER to return menu...'
read x

exit 0

# EOF
