#!/bin/sh
#      erase

Usage() {
echo "The Erase Utility restores the machine to a default factory state, erasing disk drives,"
echo "removing non-volatile data, and restoring default configuration values."
echo "Please use this tool with caution.  "
echo "To run the functions on this tool, erase must be run with a -s switch.  "
echo " "
echo "Usage: erase [ -supv ]"
echo "where -s      Switch needed to run erase functions"
echo "      -u user Username for RMM2 module"
echo "      -p pswd Password for RMM2 module"
echo "      -v sda  Volume to skip during the storage cleaning"
echo " "
}

#The following variables may need to be changed.  They can be included as command input as shown above
#or changed here in the script if not passed as command input.

SKIP_VOLUME=   	#Enter the volume that you wish to skip.  For example SKIP_VOLUME=sda
DEPLOYDIR=$DeployPath/Linux
logfile=$DeploymentStore/erase.log
ADDLOG=addLogRecord
# ADDLOG=echo

RUNERASE=0
SETUN=0
SETPW=0
SKVOL=0
EXITCODE=0

#Root access required for dwipe functions.
#RMM2 authentication information is set to default.

# echo "erase $* [$#]"
if [ $# -lt 1 ]
then
   Usage
   exit 1
fi
echo "# erase $*" >$logfile
for i in $*
do
  if [ $SETUN -eq 1 ];
  then
	RMMuser=$i
	SETUN=0
  fi
  if [ $SETPW -eq 1 ];
  then
	RMMpassword=$i
	SETPW=0
  fi
  if [ $SKVOL -eq 1 ];
  then
	SKIP_VOLUME=$i
	SKVOL=0
  fi
  case $i in
	-s) RUNERASE=1 ;;
	-u) SETUN=1 ;;
	-p) SETPW=1 ;;
	-v) SKVOL=1 ;;
	-*) Usage  ;;
  esac
done

if [ $RUNERASE -eq 1 ];
then
    $ADDLOG ERASE INFO "Begin ERASE:  logfile=$logfile"
	#RESETBIOS
	echo Restoring BIOS settings to a default set of values ... >>$logfile
	cd $DEPLOYDIR/syscfg
	#this is a syscfg requirement - syscfg must be run from the syscfg directory
	# http://support.startlogic.com/submit-ticket.php
	./syscfg /bldfs  >>$logfile 2>&1
	ret=$?
	if [ $ret -eq 0 ]
	then
		$ADDLOG ERASE INFO "Restoring BIOS settings to default completed successfully."
	else
		EXITCODE=1
		$ADDLOG ERASE ERROR "Restoring BIOS settings to default did not complete successfully."
	fi
	cd $DEPLOYDIR

	# The kiratool may not be present in the PDK toolkit, needs licensing addressed
	##RESETRMM2
    queryProperty  RMM "OEM" -
    ret=$?
    if [ $ret -eq 0 ]
    then
        echo Restoring RMM2 settings to a default set of values
        [ "$RMMuser" =  "" ] && RMMuser="admin"
        [ "$RMMpassword" =  "" ] && RMMpassword="password"
        #Detect if rmm2 is present
        kiratool -c info  >>$logfile 2>&1
        if [ $? = 441 ]
        then
        	$ADDLOG ERASE INFO "RMM2 not detected in system ... skipping."
        else
        	kiratool -l 127.0.0.1 -u $RMMuser -p $RMMpassword defaults
        	if [ $? = 0 ];
        	then
        		$ADDLOG ERASE INFO "Restoring RMM2 settings completed successfully."
        	else
        		$ADDLOG ERASE ERROR "Restoring RMM2 settings did not complete successfully."
        		EXITCODE=1
        	fi
        fi
    fi

	#RESETBMC
	# Use dmidecode here to determine the platform string.
	# PLATFORM="`dmidecode -s bios-version | cut -d'.' -f1`"
        PLATFORM=`queryProperty IntelBIOSID BoardFamilyID -`
	cd $DEPLOYDIR
	if [ "$PLATFORM" = "S5000" ]
	then
		# S5000-Clears NVRAM and sets BMC to default settings
		iresetbmc -c -b  >>$logfile 2>&1
		if [ $? -eq 0 ]
		then
			$ADDLOG ERASE INFO "Restoring BMC settings to default values completed successfully."
		else
			$ADDLOG ERASE ERROR "Restoring BMC settings to default values did not complete successfully."
		fi
	fi
	cd $DEPLOYDIR
	if [ "$PLATFORM" = "S3000" ]
	then
		# S3000-Restores AMT settings to default
		amtcfg -d   >>$logfile 2>&1
		ret=$?
		if [ $ret -eq 2 ]; then
			# amt unprovision takes a long time, so often times out (returns 2).
			# This is really ok, because the request succeeded.
			ret=0
		fi
		if [ $ret -eq 0 ]
		then
			$ADDLOG ERASE INFO "Restoring AMT settings to default values completed successfully."
		else
			$ADDLOG ERASE ERROR "Restoring AMT settings to default values did not complete successfully."
		fi
	fi

	#CLEANSTORAGE
    atalist=`queryProperty Disk device -`
    scsilist=`sgdiskmon -r |grep -v sgdiskmon |grep Disk |awk '{print $3}'`
    if [ "$SKIP_VOLUME" = "" ]
    then
        devlist="$atalist"
    else
        devlist="`echo $atalist |grep -v $SKIP_VOLUME`"
    fi
	echo "Erasing hard drives by writing data to each block.  Please be aware that"   >>$logfile
	echo "this may take a long period of time based on the size of the volume. "  >>$logfile
	echo "wiping these devices: $devlist "  >>$logfile
	# for DRIVE in "`df -h |grep sd |grep -v $SKIP_VOLUME|awk '{print $1}'i`" ;do
	for DRIVE in $devlist
	do
		# May want -z also to zero device afterward
		echo "# $DEPLOYDIR/dwipe -f -s -z -n 1 $DRIVE" >>$logfile
		$DEPLOYDIR/dwipe -f -s -z -n 1 $DRIVE   # >>$logfile 2>&1
		ret=$?
		echo "dwipe ret=$ret" >>$logfile
		if [ $ret = 0 ]
		then
			$ADDLOG ERASE INFO "Erasing $DRIVE completed successfully."
		else
			$ADDLOG ERASE ERROR "Erasing $DRIVE did not complete successfully."
		fi
	done

	# Clear the RAID volume, if any
	if [ "$PLATFORM" = "S3000" ]
	then
           queryProperty BIOSRAID SWRAID Enabled
           if [ $? -eq 1 ];   # true
	   then
              # vend=`queryProperty BIOSRAID Vendor LSI`
              queryProperty BIOSRAID Vendor LSI
              if [ $? -eq 1 ];   # true
	      then
	        # clear LSI SW RAID with Hypercfg
		hypercfg /DEL /l0
	        ret=$?
	        $ADDLOG ERASE INFO "LSI SW RAID hypercfg delete = $ret"
              else
	        # clear Matrix SW RAID with Raidcfg32
		# Raidcfg32 /Y /D Demo0
		ret=1
		$ADDLOG ERASE ERROR "Matrix SW RAID not supported in Linux"
              fi
	      setBIOS4RAID disable
	   fi
	else
           queryProperty BIOSRAID HWRAID Enabled
           if [ $? -eq 1 ];   # true
	   then
	      # clear LSI HW RAID with Cmdtool2
              CmdTool2 -CfgLdDel -LALL -aALL
	      ret=$?
	      $ADDLOG ERASE INFO "LSI HW RAID delete = $ret"
           else
              queryProperty BIOSRAID SWRAID Enabled
              if [ $? -eq 1 ];   # true
	      then
	         # clear LSI SW RAID with Cmdtool2
                 CmdTool2 -CfgLdDel -LALL -aALL
	         ret=$?
		 $ADDLOG ERASE INFO "LSI SW RAID delete = $ret"
		 setBIOS4RAID disable
	      fi
	   fi
	fi

fi
exit $ret
