#!/bin/csh
set root = '/dev'
if (!(($#argv == 2) || ($#argv == 1))) then
	echo "There are $#argv parameters, there should be 1 or 2"
	echo "Usage: cal-drive.csh DEVICE [NUMBER_OF_SECTORS]"
	echo "Example: cal-drive sdc"
	exit 1
endif
set logfile = "cal-log.txt"
set dev = $1
set n = 00000
if ($#argv == 2) set n = $2
set pat = "abcdeZYXWV091827"
echo "Calibrate Drive Version 2.0 log file ($logfile)" > $logfile
date >> $logfile
echo "Using drive: $dev" >> $logfile
#dmesg | grep $dev >> $logfile
##
#	dmesg | grep $dev > /dev/null
#	set dev_not_found = $status
#	dmesg | grep $dev | grep sector > /dev/null
#	set cnt_not_found = $status
#	if (!($cnt_not_found)) then
#	set n = `dmesg | grep $dev | grep sector | sed -e 's/^.*: //' -e 's/ .*$//'`
#	echo "$root/$dev has $n sectors"
#	echo "$root/$dev has $n sectors" >> $logfile
#	endif
#	dmesg | grep $dev | grep $n > /dev/null
#	set cnt_not_match = $status
#	if ($dev_not_found) then
#		echo "Note: $dev not in dmesg output"
#		echo "Note: $dev not in dmesg output" >> $logfile
#	else
#		if ($cnt_not_match) then
#			echo "Note: $dev does not report $n sectors"
#			echo "Note: $dev does not report $n sectors" >> $logfile
#			dmesg | grep $dev
#		endif
#	endif
#
lsblk -b >> $logfile
set cmd = (`lsblk -b | grep $dev`)
#foreach f ($cmd)
#	echo "Field: $f"
#end
set sectors = `echo "$cmd[4] / 512" | bc`
#echo "$sectors sectors"
#exit (1)
set n = $sectors
echo "This script will overwrite $n sectors of the drive on $root/$dev"
echo "Everything on the drive $root/$dev WILL BE LOST"
echo -n "Do you want to continue? [yes|no] "
set answer = $<
echo "The answer to continue? yes or no is $answer" >> $logfile
if (($answer != "yes") && ($answer != "y")) then
	echo "Ending procedure without wipe"
	echo "Ending procedure without wipe" >> $logfile
else
	set target = `yes $pat | dd bs=512 count=$n | md5sum | tr a-z A-Z`
	yes $pat | dd bs=512 count=$n > $root/$dev
	echo "MD5 should be:   $target"
	echo "MD5 should be:    $target" >> $logfile
	echo -n "MD5 on drive is: "
	echo -n "MD5 on drive is:  " >> $logfile
	md5sum < $root/$dev | tr a-z A-Z | tee md5.txt
	cat md5.txt >> $logfile
	set target = `yes $pat | dd bs=512 count=$n | sha1sum | tr a-z A-Z`
	echo "SHA1 should be:   $target"
	echo "SHA1 should be:   $target" >> $logfile
	echo -n "SHA1 on drive is: "
	sha1sum < $root/$dev | tr a-z A-Z | tee sha.txt
	echo -n "SHA1 on drive is: " >> $logfile
	cat sha.txt >> $logfile
	
endif
