#!/bin/bash
# ---------------------------------------------------------------------------
# wipedev - wipes the partition table of drives in system

# Copyright 2016, Brett Kelly <bkelly@45drives.com>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

usage() { # Help
        cat << EOF
Usage:	wipedev
		[-a] Wipe all drives
		[-d] Wipe specific drive 
		[-h] Displays this message
EOF
        exit 0
}
checkroot(){
	SCRIPT_NAME=$(basename "$0")
	if [ "$EUID" -ne 0 ];then
		echo "You must have root privileges to run $SCRIPT_NAME"
		exit 0
	fi
}

ALL_FLAG=0
DEVICE_PATH=/dev/disk/by-vdev
CONFIG_PATH=/etc
checkroot
while getopts 'ad:' OPTION;do
	case ${OPTION} in
	a)
		ALL_FLAG=1
		;;
	d)
		DISK=("$OPTARG")
		if [ -b $DEVICE_PATH/$DISK ];then
			:
		else
			echo "$DISK is not present in system"
			exit 1
		fi
		;;
	esac
done

if [ $# -eq 0 ];then
	usage
fi

if [ $ALL_FLAG -eq 1 ];then
	BAYS=$((cat $CONFIG_PATH/vdev_id.conf| awk "NR>1" | wc -l) 2>/dev/null)
	i=0
	j=3
	## LOOP THROUGH BAYS
	while [ "$i" -lt $BAYS ];do
		bay=$(cat $CONFIG_PATH/vdev_id.conf | awk -v j=$j 'NR==j{print $2}')
		BAY[$i]=$bay
		let i=i+1
		let j=j+1
	done
else
	BAYS=1
	BAY=$DISK
fi

i=0
while [ "$i" -lt $BAYS ];do 
   	if [ ! -b $DEVICE_PATH/${BAY[$i]} ];then
        echo "Drive bay [${BAY[$i]}] is empty"
		:
	else     
	    wipefs -a $DEVICE_PATH/${BAY[$i]}
	    if [ $? -eq 0 ]; then
		    echo -e "device ${BAY[$i]} fs has been wiped"
	    else
            echo "Error wiping ${BAY[$i]} fs try again manually 'wipefs -a $DEVICE_PATH/${BAY[$i]}'"
	    fi
    fi
    let i=i+1
done
