#!/bin/sh

# A hack to stop "dialog" messing up the terminal when this script is
# started from /etc/inittab
export TERM=xterm

. /etc/inquisitor/global

DIALOG=dialog

test_descriptions()
{
	find $SHARE_DIR/test -maxdepth 1 -type f | while read L; do
		if grep -q '^# NAME=' "$L"; then
			echo "'`basename $L`'"
			sed -ne '/^# NAME=/ { s/^# NAME=\(.*\)$/"\1"/; p }' <"$L"
		fi
	done
}

result_list()
{
	ls -1 /home | sed "s/\(.*\)/\"\1\" 'File'/"
}

while true; do
	exec 3>&1
	value=`$DIALOG --item-help --nocancel --backtitle 'Inquisitor Live' \
	        --title 'Choose an action' --menu '' \
	        16 72 10 \
	        'Non-destructive' 'Non-destructive tests loop' '' \
		'Full' 'Full tests loop - WARNING!' 'WARNING! This test would overwrite all the data on your hard drive!' \
		'Benchmark-RO' 'Non-destructive benchmark-only test loop' '' \
		'Benchmark-RW' 'Destructive benchmark test loop - WARNING!' 'WARNING! This test would overwrite all the data on your hard drive!' \
		'Single' 'Run a single test' 'Choose and run a single test with fine-tuning of all parameters' \
		'Detects' 'Detects only' '' \
		'View' 'View test results' '' \
		'Save' 'Save test results' 'Save test results to external storage media (USB flash, etc)' \
		'About' 'About Inquisitor' '' \
		'Reboot' 'Quit and reboot' '' \
	2>&1 1>&3`
	retval=$?
	exec 3>&-

	clear

	case $retval in
	0)
		;;
	1)
		exit 0
		;;
	255)
		if test -n "$value" ; then
			echo "$value"
		else
			continue # ESC pressed
		fi
		;;
	esac

	case "$value" in
	Non-destructive)
		cat <<__EOF__ >$HOME/test_script
PLANNER=1 TEST_NAME=cpu TESTTIME=1800 run_test cpu
PLANNER=1 TEST_NAME=memory TEST_LOOPS=1 LOGTIME=120 run_test memory
PLANNER=1 TEST_NAME=net URL=3000/test_file.html TIMEOUT=30 MD5=ca658fd4159bc35698edf9a1cdd70876 run_test net
PLANNER=1 TEST_NAME=fdd FLOPPY_SIZE=1440 run_test fdd
PLANNER=1 TEST_NAME=odd_read MESH_POINTS=1024 TEST_IMAGE_BLOCKS=50000 FORCE_NON_INTERACTIVE=false TEST_IMAGE_HASH=2e8744dfd11bf425001aad57976d42cc run_test odd_read
__EOF__
		inquisitor
		;;
	Full)
		if $DIALOG --backtitle 'Inquisitor Live: Full tests loop' --title 'WARNING!' --clear --yesno 'This is a full test that will overwrite all the contents of your hard drives! Are you sure you want to run this test?' 8 60; then
			clear
			cat <<__EOF__ >$HOME/test_script
PLANNER=1 TEST_NAME=cpu TESTTIME=1800 run_test cpu
PLANNER=1 TEST_NAME=memory TEST_LOOPS=1 LOGTIME=120 run_test memory
PLANNER=1 TEST_NAME=hdd-passthrough DISK_GROUP_SIZE=8 run_test hdd-passthrough
PLANNER=1 TEST_NAME=hdd-array JOBS=16 TIMEOUT=3600 STRESS_TREE=/usr/share/inquisitor/linux-2.6.22.5-31-stress.tar.gz LOGTIME=120 run_test hdd-array
PLANNER=1 TEST_NAME=net URL=3000/test_file.html TIMEOUT=30 MD5=ca658fd4159bc35698edf9a1cdd70876 run_test net
PLANNER=1 TEST_NAME=fdd FLOPPY_SIZE=1440 run_test fdd
PLANNER=1 TEST_NAME=odd_read MESH_POINTS=1024 TEST_IMAGE_BLOCKS=50000 FORCE_NON_INTERACTIVE=false TEST_IMAGE_HASH=2e8744dfd11bf425001aad57976d42cc run_test odd_read
__EOF__
			inquisitor
		else
			continue
		fi
		;;
	Benchmark-RO)
		cat <<__EOF__ >$HOME/test_script
PLANNER=1 TEST_NAME=whetstone LOOPS=20000 run_test whetstone
PLANNER=1 TEST_NAME=dhrystone DURATION=30 run_test dhrystone
PLANNER=1 TEST_NAME=hdparm AVG_SAMPLES=5 run_test hdparm
PLANNER=1 TEST_NAME=unixbench run_test unixbench
__EOF__
		inquisitor
		;;
	Benchmark-RW)
		if $DIALOG --backtitle 'Inquisitor Live: Destructive benchmark test loop' --title 'WARNING!' --clear --yesno 'This is test run will overwrite all the contents of your hard drives! Are you sure you want to run this test?' 8 60; then
			clear
			cat <<__EOF__ >$HOME/test_script
PLANNER=1 TEST_NAME=whetstone LOOPS=20000 run_test whetstone
PLANNER=1 TEST_NAME=dhrystone DURATION=30 run_test dhrystone
PLANNER=1 TEST_NAME=hdparm AVG_SAMPLES=5 run_test hdparm
PLANNER=1 TEST_NAME=unixbench run_test unixbench
PLANNER=1 TEST_NAME=bonnie run_test bonnie
__EOF__
			inquisitor
		else
			continue
		fi
		;;
	Single)
		exec 3>&1
		RESULT=`echo $(test_descriptions) | xargs $DIALOG --clear --backtitle 'Inquisitor Live: Run a single test' \
			--title 'Choose a test' \
			--menu '' 20 51 14 2>&1 1>&3`
		retval=$?
		exec 3>&-
		if [ "$retval" = 0 ]; then
			clear
			echo "Running test '$RESULT'"
			$SHARE_DIR/test/$RESULT
		else
			continue
		fi
		;;
	Detects)
		inq-detect
		;;
	View)
		exec 3>&1
		RESULT=`echo $(result_list) | xargs $DIALOG --clear --backtitle 'Inquisitor Live: View test results' \
			--title 'Choose a file' \
			--menu '' 20 51 14 2>&1 1>&3`
		retval=$?
		exec 3>&-
		if [ "$retval" = 0 ]; then
			clear
			$DIALOG --clear --backtitle "Inquisitor Live: View test results: $RESULT" --textbox "$HOME/$RESULT" 0 0
			continue
		else
			continue
		fi
		;;
	Save)
		exec 3>&1
		RESULT=`$DIALOG --backtitle 'Inquisitor Live: Save test results' \
			--title 'Save file as...' \
			--clear \
			--inputbox 'Save test results tarball (.tar.gz) as:' 8 51 '/media/disk/results.tar.gz' 2>&1 1>&3`
		retval=$?
		exec 3>&-
		if [ "$retval" = 0 ]; then
			clear
			echo 'Saving test results...'
			find $HOME -maxdepth 1 -type f | xargs tar -cvzf "$RESULT"
			echo 'Saving complete'
		else
			continue
		fi
		;;
	About)
		$DIALOG --backtitle 'Inquisitor Live: About' \
			--title "Inquisitor Live v$INQ_VERSION" \
			--msgbox "Copyright (C) 2004-2008 by Inquisitor team. \
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 3 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, see <http://www.gnu.org/licenses/>." 15 72
		continue
		;;
	Reboot)
		/sbin/reboot
		;;
	esac

	echo
	echo 'Press ENTER to return to main menu'
	read L
done
