#!/bin/bash
echo -e "__________ secure & fast wipe and refurbishment harddisk script v1.1 by dwaves.org GPLv2 __________\n
it is recommended to run this script inside a screen session\n
label the session according to the harddisk-3-letter-id, example: screen -S sdd\n
this way its possible to shred multiple harddisks at the same time and keep overview\n
please modify the script line 'operator=' to specify who is responsible for deleting the harddisk"
echo "____________________________________________________________________________________________________"

# operator of the machine
operator="operator operator@domain.de";

# change into log dir
mkdir /root/deleteLogs
cd /root/deleteLogs

echo "...list all available harddisks"

PARTITIONS=/dev/sd*
for f in $PARTITIONS
do
	echo "========== $f =========================================";
	lsblk $f;
	hdparm -I $f | grep Model;
	lsblk -o 'NAME,MAJ:MIN,RM,SIZE,RO,FSTYPE,MOUNTPOINT,UUID'|grep $f
	echo "==============================================================";
done

echo "BE CAREFUL!"
echo "CHOSING THE WRONG HARDDISK WILL LEAD TO DATALOSS!"
echo "HAVE BACKUPS OF ALL DATA OF THE SYSTEM OR USE A SYSTEM THAT HAS NO VALUABLE DATA ATTACHED!"

read -p "WHICH harddisk shall be wiped (first with /dev/zero then with /dev/random)? (please enter the 3xletter id):" harddisk

echo "__________________________________________________________"
echo "ABOUT TO SHRED HARDDISK: $harddisk"
echo "__________________________________________________________"

val=
if [[ -z "$harddiskID" ]]
then
read -p "refurbish-workflow of harddisks works like this:
1. buy second hand pcs
2. label the pcs with permanent marker with a pcID-number
3. label every harddisk with pcID:harddiskID number
4. start this script and pass the pcID:harddiskID number, so you will know in the logs what harddisk was wiped

please enter the harddiskID
:" harddiskID
echo "__________________________________________________________"
fi

echo "... WIPE ALL DATA ON $harddisk? (LAST CHANCE TO ABORT) [y,n] ==="
hdparm -I /dev/$harddisk|grep Model

read input
if [[ $input == "N" || $input == "n" ]]; then
	echo "... aborting."
	exit;
fi

# serial of harddisk
# remove all whitespace: | tr -d ' '
# replace whitespace with underscore: safename="$(echo $filename | sed 's/ /_-_/g')"

hdmodel=$(hdparm -I /dev/$harddisk|grep Model)
hdserial=$(hdparm -I /dev/$harddisk|grep Serial)
filename=$(date +%Y-%m-%d)_$(echo $hdmodel)_$(echo $hdserial)_delete.log
filename="$(echo $filename | sed 's/ /_-_/g')"

# date of deletion
touch "$filename"
echo $(date "+DATE-%Y-%m-%d-TIME-%H-%M-%S")" Log datei angelegt." >> "$filename";

# write serial of harddisk to file
echo "=============== harddisk model & serial =============" >> "$filename";
echo "Self-Labeled-harddiskID:"$harddiskID >> "$filename";
echo $hdmodel >> "$filename";
echo $hdserial >> "$filename";
echo "=====================================================" >> "$filename";

# who operated the machine
echo $operator >> "$filename";

# what way of deletion was taken
echo "method of deletion: debian-dd one pass with zeros, one pass with random data - securely erase files from magnetic media" >> "$filename";

# seriennummer von pc
echo "================= system informations ============" >> "$filename";
dmidecode|grep -A 13 "System Information" >> "$filename";
echo "==================================================" >> "$filename";

# starting packground monitoring process in order to see dd output
$(while true; do kill -USR1 $(pgrep ^dd); sleep 1; clear; done;) &

# wipe with zeros
echo $(date "+DATE-%Y-%m-%d-TIME-%H-%M-%S")" wipe with zeros" >> "$filename";
dd if=/dev/zero of=/dev/$harddisk bs=4k conv=notrunc >> "$filename";

# wipe again with random data
echo $(date "+DATE-%Y-%m-%d-TIME-%H-%M-%S")" wipe again with random data" >> "$filename";
dd if=/dev/urandom of=/dev/$harddisk bs=4k conv=notrunc
# shred -vfz -n 100 /dev/sda

echo $(date "+DATE-%Y-%m-%d-TIME-%H-%M-%S")" overwrite-shred harddisk finished: " >> "$filename";
