#!/bin/bash

# Export system logs and check whether it with any error message to those with BMC platforms.

#conversation dialog for deleting logs or not
echo "path of logs are under "/home/export_sys_log_with_bmc_*.log""
read -p "Do you want to delete all logs before you run this script? 'y' or 'n'" choice

if [ "${choice}" == "y" ] || [ "${choice}" == "Y" ]; then

#delete all logs
	echo -e "Delete log file(s)...\n"
	rm -rf /home/export_sys_log_with_bmc_*.log

elif [ "${choice}" == "n" ] || [ "${choice}" == "N" ]; then

#do nothing
	sleep 1

fi

#get time in integer
time=`date +%s`
date=`date`
uptime=`uptime`

#display log file name
logFile="/home/export_sys_log_with_bmc_${time}.log"
echo -e "This new log file name would be '$logFile'\n" 

echo "Generate log file $logFile"

END=14
#export logs by following command lines
c[0]="ipmitool sel time set '$(date +\%m/\%d/\%Y\ \%H:\%M:\%S)'"
c[1]="ipmitool sel time get"
c[2]="ipmitool sel"
c[3]="ipmitool sel elist"
c[4]="ipmitool sel -v elist"
c[5]="ipmitool sel list"
c[6]="ipmitool sdr elist"
c[7]="ipmitool sdr -v"
c[8]="ipmitool sensor"
c[9]="ipmitool lan print"
c[10]="ipmitool lan print 2"
c[11]="ipmitool fru print"
c[12]="ipmitool fru print -v"
c[13]="ipmitool mc info"
c[14]="ipmitool hpm check"
c[15]="ipmitool sol info 1"

##log info in log file
#log Current Date and Time
echo -e "--- Beginning of log ---\n" >> $logFile 2>&1
echo -e "Current Date and Time." >> $logFile 2>&1
echo -e ${date}"\n" >> $logFile 2>&1

#log System operation time since last boot
echo -e "System operation time since last boot" >> $logFile 2>&1
echo -e ${uptime}"\n" >> $logFile 2>&1

for ((i=0; i<=$END; i++))
do
	cmd1=${c[$i]}" >> $logFile 2>&1"
	cmd2=${c[$i]}
	echo $i":# "$cmd2
	echo "# "$cmd2 >> $logFile 2>&1
	bash -c "$cmd1"
	if [ "$i" != "$END" ]; then
		echo -e "\n\n" >> $logFile 2>&1
	else
		echo "--- End of log ---" >> $logFile 2>&1
	fi
done

echo -e "Log file $logFile has been generated.\n"

