#!/bin/bash

# How many arguments need 
ARG_NUM=3

# Check argument
if [ $# -ne $ARG_NUM ]; then
  echo "The Argument numbers are $ARG_NUM"
  echo "IOL_ipmitool-exportBySingle_v5.sh <BMC IP> <Username> <Password>"
  exit 1
fi

# 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_ipmitool_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_ipmitool_log_with_bmc_*.log

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

#do nothing
	sleep 1

fi

#get time in integer
time=`date +%d-%m`
date=`date`
uptime=`uptime`

#display log file name
logFile="/home/${time}_export_ipmitool_log_with_bmc_$1.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 -I lanplus -H $1 -U $2 -P $3 sel time set '$(date +\%m/\%d/\%Y\ \%H:\%M:\%S)'"
c[1]="ipmitool -I lanplus -H $1 -U $2 -P $3 sel time get"
c[2]="ipmitool -I lanplus -H $1 -U $2 -P $3 sel"
c[3]="ipmitool -I lanplus -H $1 -U $2 -P $3 sel elist"
c[4]="ipmitool -I lanplus -H $1 -U $2 -P $3 sel -v elist"
c[5]="ipmitool -I lanplus -H $1 -U $2 -P $3 sel list"
c[6]="ipmitool -I lanplus -H $1 -U $2 -P $3 sdr elist"
c[7]="ipmitool -I lanplus -H $1 -U $2 -P $3 sdr -v"
c[8]="ipmitool -I lanplus -H $1 -U $2 -P $3 sensor"
c[9]="ipmitool -I lanplus -H $1 -U $2 -P $3 lan print"
c[10]="ipmitool -I lanplus -H $1 -U $2 -P $3 lan print 2"
c[11]="ipmitool -I lanplus -H $1 -U $2 -P $3 fru print"
c[12]="ipmitool -I lanplus -H $1 -U $2 -P $3 fru print -v"
c[13]="ipmitool -I lanplus -H $1 -U $2 -P $3 mc info"
c[14]="ipmitool -I lanplus -H $1 -U $2 -P $3 hpm check"
c[15]="ipmitool -I lanplus -H $1 -U $2 -P $3 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}" >> $logFile 2>&1
echo -e "$(TZ="Asia/Taipei" date) <Taipei Zone>\n" >> $logFile 2>&1

#log System operation time since last boot
echo -e "Jump & Deploy 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
		count=$(ipmitool -I lanplus -H $1 -U $2 -P $3 sel list | wc -l)
		echo -e "ipmitool -I lanplus -H $1 -U $2 -P $3 sel list | wc -l" >> $logFile 2>&1
                echo -e "The $1 Current Sel logs are ${count}.\n" >> $logFile 2>&1
		echo "--- End of log ---" >> $logFile 2>&1
	fi
done

if [ "$count" -gt "4090" ]; then
       ipmitool -I lanplus -H $1 -U $2 -P $3 sel clear

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

fi
