#!/bin/bash

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

#conversation dialog for deleting logs or not
echo -e "Path of logs are under "/home/export_sys_log_without_bmc_*.log"\n"
read -p "Do you want to delete all logs before you run this script? Press 'y' or 'n' then press 'Enter': " choice

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

#delete all logs
	echo -e "Delete log file(s)...\n"
	rm -rf /home/export_sys_log_without_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_without_bmc_${time}.log"
echo -e "This log file name would be '$logFile'\n"
echo "Generate log file $logFile\n"

END=7
#export logs by following command lines
c[1]=lscpu
c[2]=lspci
c[3]="lspci -vvv"
c[4]="dmidecode"
test ! -f /var/log/messages && echo "no data" > /var/log/messages
c[5]="cat /var/log/messages"
c[6]="dmesg"
c[7]="lshw"

##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} >> /home/export_sys_log_without_bmc_${time}.log 2>&1
echo -e " " >> /home/export_sys_log_without_bmc_${time}.log 2>&1

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

for ((i=1; 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"

