#!/bin/bash
#
# Copyright (C) 2010-2016 Canonical
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
FWTS="Firmware Test Suite"
OPTIONS="/tmp/options.$$"
DIALOG_CMD="/tmp/fwts.cmd.$$"
export DIALOGRC="/usr/share/fwts/fwts-live-dialogrc"

FWTS_DATE=`date +%d%m%Y`
FWTS_TIME=`date +%H%M`

#
# Check if executed as root or with sudo
#
if [ $EUID -ne 0 ]; then
	echo "`basename $0`: must be executed with sudo"
	exit 1
fi

#
# for debugging, use:
# -  WORK_DIR=./fwts/$FWTS_DATE/$FWTS_TIME
# if booting in fwts-live
# - change FWTS_AUTO_SHUTDOWN= to alter default shutdown after testing completion
#
WORK_DIR=/fwts/$FWTS_DATE/$FWTS_TIME
FWTS_AUTO_SHUTDOWN="$1"
SHUTDOWN_AT_END=1

#
# set WORK_DIR to /cdrom/fwts if booting via casper 
#
if [ `grep -qs boot=casper /proc/cmdline; echo $?` -eq 0 ]; then
	CASPER_DETECTED=1
	WORK_DIR=/cdrom/fwts/$FWTS_DATE/$FWTS_TIME
fi

#
# check /proc/cmdline and argv[1] for FWTS_AUTO_SHUTDOWN to toggle auto shutdown
#
if [ `grep -qs FWTS_AUTO_SHUTDOWN=1 /proc/cmdline; echo $?` -ne 0 ]; then
	if [ -n "${CASPER_DETECTED:+x}" ]; then
		SHUTDOWN_AT_END=0
	elif [ -n "${FWTS_AUTO_SHUTDOWN:+x}" ]; then
		SHUTDOWN_AT_END=0
	fi
fi

do_help()
{
	dialog  --backtitle "$FWTS" --title "Help" --msgbox \
		"To make selections from the menu use the following keys:\n\nUse the up/down cursor keys to navigate to the desired menu option and the <Space> bar to tick the menu selection.\n\nUse the left/right or tab keys to navigate the button options and press the <Enter> key to select." 13 60
}

#
#  Run a bunch of tests and monitor progress
#
do_test()
{
	
	num_tests=`fwts $1 --show-tests | wc -l`
	num_tests=$((num_tests - 1))
	if [ $num_tests -gt 0 ]; then
  		fwts --force-clean --skip-test=s3,s4 $1 --show-progress-dialog --log-type plaintext,html | dialog --backtitle "$FWTS" --title "$2" --gauge "" 13 80 0
	fi
}

#
#  Get user choice of tests to run
#
select_tests()
{
	while true
	do
		declare -a tests
		x=0
		#
		#  Discover available tests
		#
		fwts --batch --batch-experimental --uefitests --unsafe --show-tests | grep "^ "| sort | uniq > $OPTIONS
		while read test text
		do
			((x++))
			tests[$x]=$test
			txt="${txt} ${x} \"${text}\" off"
		done < $OPTIONS
		rm $OPTIONS
	
		#
		# Construct and run dialog
		#
		echo dialog --backtitle '"$FWTS"' --title '"Select Tests to Run"' --help-button  --checklist '"Select from the list below the test(s) you want to run. Use up/down cursor keys, space to select and enter to start:"' 20 70 $x $txt > $DIALOG_CMD
		. $DIALOG_CMD 2> $OPTIONS
		ret=$?
		rm $DIALOG_CMD

		#
		# Scan return selections and build fwts test scenarios
		#
		x=0
		for i in `cat $OPTIONS`
		do
			((x++))
			i=${i#\"}
			i=${i%\"}
			run_tests="$run_tests ${tests[$i]}"
		done

		#
		# Handle dialog exit states:
		#
		case $ret in
		0)
			if [ $x -eq 0 ]; then
				dialog --backtitle "$FWTS" --title "No Tests Selected!" --msgbox \
			"   You did not select any tests to be run." 5 50
			else
  				fwts --force-clean $run_tests --show-progress-dialog --log-type plaintext,html | dialog  --backtitle "$FWTS" --title "$2" --gauge "" 15 80 0
				done_tests_finish
			fi
			;;
		1)
			return
			;;
		2)
			do_help
			;;
		255)
			no_tests_finish
			;;
		esac
	done
}

#
#  View the results log
#
view_results()
{	
	tempfile=/tmp/view_results.tmp.$$
	dialog --print-maxsize 2>| $tempfile
	term_height=`sed -n "s/^MaxSize: \([0-9]*\), \([0-9]*\)$/\1/p" $tempfile 2> /dev/null`
	term_width=` sed -n "s/^MaxSize: \([0-9]*\), \([0-9]*\)$/\2/p" $tempfile 2> /dev/null`
	term_height=$((term_height))
	term_width=$((term_width - 2))
	rm $tempfile

	dialog --backtitle "$FWTS" --title "Test Results" \
		--textbox results.log \
		$term_height $term_width
}

#
#  Shutdown or exit
#
finish()
{
	if [ $SHUTDOWN_AT_END -eq 1 ]; then
		dialog  --backtitle "$FWTS" --infobox "      Shutting down.." 3 30
  		shutdown -P now
		exit 0
	else
		dialog --clear
		exit 0
	fi
}

#
#  Aborted, no tests run
#
no_tests_finish()
{
	dialog --backtitle "$FWTS" --title "Abort Testing!" --msgbox \
		"   You did not select any tests to be run.\n\n          Press Enter to shutdown." 7 50
	finish
}

#
#  Finish after running some tests
#
done_tests_finish()
{
	#
	# Dump out firmware and system data, LP: #1322543
	#
	cd $WORK_DIR
	fwts --dump > /dev/null

	dialog  --backtitle "$FWTS" --title "Testing Complete" --yesno \
"The results can be found on the USB stick in the\n
the directory: /fwts/$FWTS_DATE/$FWTS_TIME/results.log\n\n
     Do you want to view the results log now?" 9 55
	case $? in
	0)
		view_results
		;;
	1|255)
		;;
	esac

	dialog  --backtitle "$FWTS" --title "Testing Complete" --msgbox \
"The results can be found on the USB stick in the\n
the directory: /fwts/$FWTS_DATE/$FWTS_TIME/results.log\n\n
            Press Enter to shutdown" 9 55
	finish
}

#
#  Here we go..
#
mkdir -p $WORK_DIR >& /dev/null
if [ $? -ne 0 ]; then
	dialog --ok-label "Shutdown" --backtitle "$FWTS" --title "Error" --msgbox "Could not create directory fwts/$FWTS_DATE/$FWTS_TIME to store test results.\n\n          Press Enter to shutdown." 8 50 
	finish
	exit 0
fi

while true
do
	dialog --help-button --backtitle "$FWTS" --title "Select Tests" --radiolist \
  		"This will run a suite of firmware tests that will check the BIOS and ACPI tables. It can also find issues that can cause Linux problems.\n\n\
The default below is to run just all the Batch Tests, but you can select more tests below if required.\n\nPlease select below (using cursor up/down and space) and press enter to continue:" \
		22 70 6 \
		1 "All Batch Tests" on \
		2 "ACPI Tests" off \
		3 "UEFI Tests" off \
		4 "Recommended Tests" off \
		5 "Select Individual Tests" off \
		6 "Abort Testing" off \
	2> $OPTIONS

	case $? in
	0)
		cd $WORK_DIR >& /dev/null
		case `cat $OPTIONS` in
		'1')
			do_test "--batch --uefitests" 'Running Batch Tests'
			done_tests_finish
			;;
		'2')
			do_test "--acpitests" 'Running ACPI Tests'
			done_tests_finish
			;;
		'3')
			do_test "--uefitests" 'Running UEFI Tests'
			done_tests_finish
			;;
		'4')
			do_test "version cpufreq maxfreq msr mtrr nx virt aspm dmicheck apicedge klog oops --acpitests --uefitests --log-level=medium" 'Running Recommended Tests'
			done_tests_finish
			;;
		'5')
			select_tests
			;;
		'6')
			no_tests_finish
			;;
		esac
		;;
	2)
		do_help
		;;
	1|255)
		no_tests_finish
		;;
	esac
done
