# -*-Shell-script-*-
# Common operations to all SSSTK commands

# LABEL of the USB key where data will be saved/read
USB_KEY_LABEL=LiveUSB
ALLBOARDS=/opt/hp/hp-scripting-tools/etc/allboards.xml
CONREP_XML=/opt/hp/hp-scripting-tools/etc/conrep.xml
LD_LIBRARY_PATH=/opt/hp/hp-scripting-tools/bin:$LD_LIBRARY_PATH
IFHW_CMD="/opt/hp/hp-scripting-tools/bin/ifhw"

# Create a TMP directory and mount the USB key
if [ -z ${TMPDIR} ]; then
	TMPDIR=$(mktemp -t -d liveusb.XXXXXX)
	mount -t vfat /dev/disk/by-label/$USB_KEY_LABEL $TMPDIR
	if [ $? -ne 0 ]; then
		echo "Unable to mount the USB key"
		exit 1
	fi
fi

# Temporay file listing the HW found
if [ -z ${HWDISC_FILE} ]; then
	HWDISC_FILE="/tmp/hpdiscovery.xml"
fi

# BIOS information will be saved to/read from this file
if [ -z ${BIOS_FILE} ]; then
	BIOS_FILE="$TMPDIR/data_files/conrep.dat"
fi

# Smart Array RAID configuration will be saved to/read from this file
if [ -z ${RAID_FILE} ]; then
	RAID_FILE="$TMPDIR/data_files/cpqacuxe.dat"
fi

# iLO configuration will be saved to/read from this file
if [ -z ${ILO_FILE} ]; then
	ILO_FILE="$TMPDIR/data_files/hponcfg.dat"
fi

# iLO customization file.
# Information will be merged with/substitued to the one present in $ILO_FILE
if [ -z ${ILO_CONFIG_FILE} ]; then
	ILO_CONFIG_FILE="$TMPDIR/config/ilo"
fi

# Temporary iLO configuration file.
# It is the result of the merge/substitution between $ILO_FILE and 
# $ILO_CONFIG_FILE
if [ -z ${ILO_UPDATED_FILE} ]; then
	ILO_UPDATED_FILE="/tmp/ssstk_hponcfg.dat"
fi

# This is a very minimal iLO configuration files.
# It can be used to restore the iLO interface to a well know state.
if [ -z ${ILO_MINIMAL_FILE} ]; then
	ILO_MINIMAL_FILE="$TMPDIR/data_files/ilo.dat"
fi

# This time stamp will be added to the log file names before they are copied
# to the USB key
if [ -z ${TS} ]; then
	TS=`date +"%Y%m%d-%H%M"`
fi

PATH=$PATH:/ssstk
LD_LIBRARY_PATH=/ssstk

# Have to change to that directory otherwise hpdiscovery does not discover 
# anything
cd /ssstk

# Loads the drivers and do the pre capture/deploy initialization stuff
ssstk_init() {
	echo ""
	echo "Loading storage drivers for hardware"
	load_modules.sh > /dev/null 2>&1


	echo ""
	echo "Pausing to allow drivers to finish loading"
	sleep 15
	echo ""

	hp-discovery -f ${HWDISC_FILE} > /dev/null 2>&1
	echo "Hardware Discovery saved to ${HWDISC_FILE}"


	export "`hp-hwquery ${HWDISC_FILE} ${ALLBOARDS} SERVERTYPE=SystemName`"
	export "`hp-hwquery ${HWDISC_FILE} ${ALLBOARDS} BOOTDEVNODE=DevNode`";

	echo "Server Type: ${SERVERTYPE}"

}

# Saves the logs to the USB key and umount it
ssstk_stop() {
	echo "Saving logs on USB key"
	if [ -d "$TMPDIR/log" ]; then
		for i in /tmp/ssstk_*; do
			FILE=${i%%.*}
			EXT=${i##*.}
			cp $i ${TMPDIR}/log/${FILE##/tmp/ssstk_}-${TS}.${EXT}
		done
	fi	
	echo "Unmounting the key"
	umount $TMPDIR
	rmdir $TMPDIR
}
