#!/bin/bash
# Arrays have to be generated on LB
nvme_db=("one drive" "two drive")
ssd_db=("ssd drive" "ssd two drive")
# Create drives list
hwinfo --short --disk | tail -n +2 | awk {'print $1'} > /tmp/clear
hwinfo --block --short | grep dev | awk {'print $1'} > /tmp/partitions

#STOP MDADM
mdadm --stop --scan

while read partitions; do
	mdadm --zero-superblock $partitions
done < /tmp/partitions

# Clear drives
while read drive; do
  MODEL=`smartctl --all $drive | grep "Model Number" | sed -e "s/Model Number://g" | sed -e 's/^[ \t]*//'`
  if [[ $MODEL == "" ]]; then
     MODEL=`smartctl --all $drive | grep "Device Model" | sed -e "s/Device Model://g" | sed -e 's/^[ \t]*//'`
  fi
  if [[ " ${nvme_db[@]} " =~ " ${MODEL} " ]]; then
        wipefs --all --force $drive
        nvme format $drive -n 1 -ses 2
	sfdisk --delete $drive
  elif [[ " ${ssd_db[@]} " =~ " ${MODEL} " ]]; then
        wipefs --all --force $drive
	FROZEN=`hdparm -I $drive | grep frozen`
        if [[ $FROZEN == *"not"* ]]; then
           hdparm --user-master u --security-set-pass q6JR7dA6 $drive
           hdparm --user-master u --security-erase q6JR7dA6 $drive
        else
           sfdisk --delete $drive
           dd if=/dev/zero of=$drive bs=16384 count=1000
        fi
  else
       if [[ $drive == *"nvme"* ]]; then
           mdadm --zero-superblock $drive
	   wipefs --all --force $drive
           nvme write-zeroes -d $drive
	   sfdisk --delete $drive
       else
           mdadm --zero-superblock $drive
	   wipefs --all --force $drive
	   sfdisk --delete $drive
           dd if=/dev/zero of=$drive bs=16384 count=1000
       fi
  fi
done < /tmp/clear
#JSON CALLBACK
curl -s 'http://dev.listabridge.clouvider.net/cleardone' > /dev/null
