#!/bin/sh
#
# $Id: faum-gen-vhdl,v 1.66 2009-02-05 17:32:20 potyra Exp $
#
# Copyright (C) 2004-2009 FAUmachine Team <info@faumachine.org>.
# This program is free software. You can redistribute it and/or modify it
# under the terms of the GNU General Public License, either version 2 of
# the License, or (at your option) any later version. See COPYING.
#
# $1: generate-file
# [ $2 ]: base directory to look for screenshots/pointers
# emit vhdl script from commands in generate-file on stdout.
#

time_unit() {
	val=`echo $1 | sed -e 's/[a-z]*$//'`
	unit=`echo $1 | sed -e 's/^[0-9]*//'`

	if [ -z "$unit" ]; then
		unit="ms"
	fi

	case $unit in
	fs|ps|ns|us)
		echo "$0: $unit: Unsupported time unit." 1>&2
		exit 1
		;;
	ms | sec | min | hr)
		printf "${val} ${unit}"
		;;
	*)
		echo "$0: ${unit}: Unknown time unit." 1>&2
		exit 1
		;;
	esac
}

# $1 file name of screenshot
# EXP_DIR global (path to experiment directory)
# set SCREENSHOT to found path, exit if not found.
search_screenshot() {
	SHOT=$1
	if [ -e "${SHOT}" ]; then
		return;
	fi

	SHOT="screenshots/$1"
	if [ -e "$SHOT" ]; then
		return;
	fi

	if [ -z "$EXP_DIR" ]; then
		echo "cannot find screenshot $1" 1>&2
		exit 1
	fi

	SHOT="$EXP_DIR/$1"
	if [ -e "$SHOT" ]; then
		return;
	fi

	SHOT="$EXP_DIR/screenshots/$1"
	if [ -e "$SHOT" ]; then
		return;
	fi

	echo "Cannot find screenshot $1" 1>&2
	exit 1
}

# no arguments
# check if ./pointers/ or $EXP_DIR/pointers exists and
# contains images. Exit with error if not.
# will set POINTERDIR on success.
check_mouse_pointerdir() {
	POINTERDIR="pointers"
	if [ -d "$POINTERDIR" ]; then
		PICS=$(echo $POINTERDIR/*.png $POINTERDIR/*.ppm)
		if [ "$PICS" != "$POINTERDIR/*.png $POINTERDIR/*.ppm" ]; then
			return
		fi
	fi

	POINTERDIR="$EXP_DIR/pointers"
	if [ -d "$POINTERDIR" ]; then
		PICS=$(echo $POINTERDIR/*.png $POINTERDIR/*.ppm)
		if [ "$PICS" != "$POINTERDIR/*.png $POINTERDIR/*.ppm" ]; then
			return
		fi
	fi

	echo "Cannot find a pointers directory with images." 1>&2
	exit 1
}

EXP_DIR=$2

IFS='	'
cat $1 \
| grep -v '^#' \
| sed 's/\\/\\\\/g' \
| sed -e 's/		*/	/g' \
| while read p1 p2 p3 p4; do

if [ "$p1" = "debug" ]; then
	#
	# debug
	#
	printf "\n"
	printf "		-- debug on/off\n"
	printf "		debug;\n"
	printf "\n"


elif [ "$p1" = "power_on" ]; then
	#
	# power_on
	#
	printf "\n"
	printf "		-- power on machine\n"
	printf "		power_button <= true;\n"
	printf "		wait for 1000 ms;\n"
	printf "		power_button <= false;\n"
	printf "\n"

elif [ "$p1" = "power_off" ]; then
	#
	# power_off
	#
	printf "\n"
	printf "		-- power off machine\n"
	printf "		power_button <= true;\n"
	printf "		wait for 5000 ms;\n"
	printf "		power_button <= false;\n"
	printf "\n"

elif [ "$p1" = "power_switch_on" ]; then
	#
	# power_switch_on
	#
	printf "\n"
	printf "		-- power on machine\n"
	printf "		power_switch <= true;\n"
	printf "		wait for 1000 ms ;\n"
	printf "\n"

elif [ "$p1" = "power_switch_off" ]; then
	#
	# power_switch_off
	#
	printf "\n"
	printf "		-- power off machine\n"
	printf "		power_switch <= false;\n"
	printf "		wait for 1000 ms;\n"
	printf "\n"

elif [ "$p1" = "reset" ]; then
	#
	# reset
	#
	printf "\n"
	printf "		-- reset machine\n"
	printf "		reset_button <= true;\n"
	printf "		wait for 1000 ms;\n"
	printf "		reset_button <= false;\n"
	printf "		wait for 1000 ms;\n"
	printf "\n"

elif [ "$p1" = "wait_ppm" ]; then
	#
	# wait_ppm	<screenshotname>	<time until timeout>
	#
	search_screenshot "$p2"
	printf "\n"
	printf "		-- wait for screenshot %s\n" $p2
	printf "		ASSERT false REPORT \"waiting for %s (%s)\" SEVERITY note;\n" $p2 `time_unit $p3`
	printf "		wakeup_screensaver <= true;\n"
	printf "		send_string(gfx_pattern0, \"%s\");\n" "$SHOT"
	printf "		WAIT ON gfx_match0(1) UNTIL gfx_match0(1) >= 0 FOR %s;\n" `time_unit $p3`
	printf "		send_string(gfx_pattern0, \"\");\n"
	printf "		wakeup_screensaver <= false;\n"
	printf "		IF gfx_match0(1) < 0 THEN\n"
	printf "			screen_shot <= -1;\n"
	printf "			WAIT FOR 1000 ms;\n"
	printf "			ASSERT false REPORT \"timeout, screenshot taken\" SEVERITY failure;\n"
	printf "		END IF;\n"
	printf "		WAIT ON gfx_match0 UNTIL (gfx_match0(1) = -2);\n"
	printf "		ASSERT false REPORT \"found %s\" SEVERITY note;\n" $p2
	printf "\n"

elif [ "$p1" = "wait_asc" ]; then
	#
	# wait_asc	<character sequence>	<time until timeout>
	#
	printf "\n"
	printf "		-- wait for pattern %s\n" $p2
	printf "		ASSERT false REPORT \"waiting for %s (%s)\" SEVERITY note;\n" $p2 `time_unit $p3`
	printf "		wakeup_screensaver <= true;\n"
	printf "		send_string(int_asc_text0, \"%s\");\n" $p2
	printf "		WAIT ON int_asc_text0_state UNTIL int_asc_text0_state FOR %s;\n" `time_unit $p3`
	printf "		send_string(int_asc_text0, \"\");\n"
	printf "		wakeup_screensaver <= false;\n"
	printf "		IF NOT int_asc_text0_state THEN\n"
	printf "			screen_shot <= -1;\n"
	printf "			WAIT FOR 1000 ms;\n"
	printf "			ASSERT false REPORT \"timeout, screenshot taken\" SEVERITY failure;\n"
	printf "		END IF;\n"
	printf "		WAIT ON int_asc_text0_state UNTIL NOT int_asc_text0_state;\n"
	printf "		ASSERT false REPORT \"%s: found and completed!\" SEVERITY note;\n" $p2
	printf "\n"

elif [ "$p1" = "wait_text" ]; then
	#
	# wait_text	<character sequence>	<time until timeout>
	#
	printf "\n"
	printf "		-- wait for text %s\n" $p2
	printf "		ASSERT false REPORT \"waiting for %s (%s)\" SEVERITY note;\n" $p2 `time_unit $p3`
	printf "		wakeup_screensaver <= true;\n"
	printf "		send_string(int_text0, \"%s\");\n" $p2
	printf "		WAIT ON int_text0_state UNTIL (int_text0_state) FOR %s;\n" `time_unit $p3`
	printf "		send_string(int_text0, \"\");\n"
	printf "		wakeup_screensaver <= false;\n"
	printf "		IF (int_text0_state = false) THEN\n"
	printf "			screen_shot <= -1;\n"
	printf "			WAIT FOR 1000 ms;\n"
	printf "			ASSERT false REPORT \"%s: timeout, screenshot taken\" SEVERITY failure;\n" $p2
	printf "		END IF;\n"
	printf "		ASSERT false REPORT \"%s: done. deactivating...\" SEVERITY note;\n" $p2
	printf "		ASSERT false REPORT \"%s: done. waiting on ack\" SEVERITY note;\n" $p2
	printf "		WAIT ON int_text0_state UNTIL (int_text0_state = false);\n"
	printf "		ASSERT false REPORT \"%s: pattern found and completed!!\" SEVERITY note;\n" $p2
	printf "\n"

elif [ "$p1" = "delay" ]; then
	#
	# delay <time>
	#
	printf "\n"
	printf "		-- delay for %s\n" `time_unit $p2`
	printf "		ASSERT false REPORT \"delaying for %s\" SEVERITY note;\n" `time_unit $p2`
	printf "		WAIT FOR %s;\n" `time_unit $p2`
	printf "\n"

elif [ "$p1" = "type" ]; then
	#
	# type <character sequence>
	#
	printf "\n"
	printf "		-- type sequence %s\n" $p2
	printf "		send_keyboard(key, 0, %s, 100 ms);\n" $p2
	printf "\n"

elif [ "$p1" = "press_alt" ]; then
	#
	# press_alt
	#
	printf "\n"
	printf "		-- press alt key\n"
	printf "		key(56) <= true;\n"
	printf "		WAIT FOR 500 ms;\n"
	printf "\n"

elif [ "$p1" = "release_alt" ]; then
	#
	# release_alt
	#
	printf "\n"
	printf "		-- release alt key\n"
	printf "		key(56) <= false;\n"
	printf "		WAIT FOR 500 ms;\n"
	printf "\n"

elif [ "$p1" = "press_ctrl" ]; then
	#
	# press_ctrl
	#
	printf "\n"
	printf "		-- press ctrl key\n"
	printf "		key(29) <= true;\n"
	printf "		WAIT FOR 500 ms;\n"
	printf "\n"

elif [ "$p1" = "release_ctrl" ]; then
	#
	# release_ctrl
	#
	printf "\n"
	printf "		-- release ctrl key\n"
	printf "		key(29) <= false;\n"
	printf "		WAIT FOR 500 ms;\n"
	printf "\n"

elif [ "$p1" = "press_windows" ]; then
	#
	# press_windows
	#
	printf "\n"
	printf "		-- press windows key\n"
	printf "		key(125) <= true;\n"
	printf "		WAIT FOR 500 ms;\n"
	printf "\n"

elif [ "$p1" = "release_windows" ]; then
	#
	# release_windows
	#
	printf "\n"
	printf "		-- release windows key\n"
	printf "		key(125) <= false;\n"
	printf "		WAIT FOR 500 ms;\n"
	printf "\n"

elif [ "$p1" = "mouse_move" ]; then
	#
	# mouse_move <image>
	#
	check_mouse_pointerdir
	search_screenshot "$p2"
	printf "\n"
	printf "		-- wait for screenshot %s\n" $p2
	printf "		ASSERT false REPORT \"waiting for %s\" SEVERITY note;\n" $p2
	printf "		send_string(gfx_pattern0, \"%s\");\n" "$SHOT"
	printf "		WAIT ON gfx_match0(1) UNTIL gfx_match0(1) >= 0 FOR 10000 ms;\n"
	printf "		x := gfx_match0(1) + gfx_match0(3) / 2;\n"
	printf "		y := gfx_match0(2) + gfx_match0(4) / 2;\n"
	printf "		send_string(gfx_pattern0, \"\");\n"
	printf "		IF gfx_match0(1) < 0 THEN\n"
	printf "			screen_shot <= -1;\n"
	printf "			WAIT FOR 1000 ms;\n"
	printf "			ASSERT false REPORT \"timeout, screenshot taken\" SEVERITY failure;\n"
	printf "		END IF;\n"
	printf "		WAIT ON gfx_match0 UNTIL (gfx_match0(1) = -2);\n"
	printf "		ASSERT false REPORT \"found %s\" SEVERITY note;\n" $p2
	printf "\n"
	printf "		-- activate mouse detection\n"
	printf "		send_string(mouse_pointerdir, \"$POINTERDIR\");\n"
	printf "\n"
	printf "		-- move mouse to positon\n"
	printf "		control_mouse(x, y, mouse_match, mouse_event, mouse_x, mouse_y, mouse_dx, mouse_dy);\n"
	printf "\n"
	printf "		-- deactivate mouse detection\n"
	printf "		send_string(mouse_pointerdir, \"\");\n"
	printf "\n"

elif [ "$p1" = "mouse_button1_press" ]; then
	printf "\n"
	printf "		-- press mouse button\n"
	printf "		mouse_button1 <= true;\n"
	printf "\n"

elif [ "$p1" = "mouse_button1_release" ]; then
	printf "\n"
	printf "		-- release mouse button\n"
	printf "		mouse_button1 <= false;\n"
	printf "\n"

elif [ "$p1" = "mouse_click" ]; then
	#
	# mouse_click <image>
	#
	check_mouse_pointerdir
	search_screenshot "$p2"
	printf "\n"
	printf "		-- wait for screenshot %s\n" $p2
	printf "		ASSERT false REPORT \"waiting for %s\" SEVERITY note;\n" $p2
	printf "		send_string(gfx_pattern0, \"%s\");\n" "$SHOT"
	printf "		WAIT ON gfx_match0(1) UNTIL gfx_match0(1) >= 0 FOR 10000 ms;\n"
	printf "		x := gfx_match0(1) + gfx_match0(3) / 2;\n"
	printf "		y := gfx_match0(2) + gfx_match0(4) / 2;\n"
	printf "		send_string(gfx_pattern0, \"\");\n"
	printf "		IF gfx_match0(1) < 0 THEN\n"
	printf "			screen_shot <= -1;\n"
	printf "			WAIT FOR 1000 ms;\n"
	printf "			ASSERT false REPORT \"timeout, screenshot taken\" SEVERITY failure;\n"
	printf "		END IF;\n"
	printf "		WAIT ON gfx_match0 UNTIL (gfx_match0(1) = -2);\n"
	printf "		ASSERT false REPORT \"found %s\" SEVERITY note;\n" $p2
	printf "\n"
	printf "		-- activate mouse detection\n"
	printf "		send_string(mouse_pointerdir, \"$POINTERDIR\");\n"
	printf "\n"
	printf "		-- move mouse to positon\n"
	printf "		control_mouse(x, y, mouse_match, mouse_event, mouse_x, mouse_y, mouse_dx, mouse_dy);\n"
	printf "\n"
	printf "		-- deactivate mouse detection\n"
	printf "		send_string(mouse_pointerdir, \"\");\n"
	printf "\n"
	printf "		-- click mouse\n"
	printf "		mouse_button1 <= true;\n"
	printf "		WAIT FOR 1000 ms;\n"
	printf "		mouse_button1 <= false;\n"
	printf "		WAIT FOR 1000 ms;\n"
	printf "\n"

elif [ "$p1" = "mouse_dclick" ]; then
	#
	# mouse_dclick <image>
	#
	search_screenshot "$p2"
	check_mouse_pointerdir
	printf "\n"
	printf "		-- wait for screenshot %s\n" $p2
	printf "		ASSERT false REPORT \"waiting for %s\" SEVERITY note;\n" $p2
	printf "		send_string(gfx_pattern0, \"%s\");\n" "$SHOT"
	printf "		WAIT ON gfx_match0(1) UNTIL gfx_match0(1) >= 0 FOR 10 sec;\n"
	printf "		x := gfx_match0(1) + gfx_match0(3) / 2;\n"
	printf "		y := gfx_match0(2) + gfx_match0(4) / 2;\n"
	printf "		send_string(gfx_pattern0, \"\");\n"
	printf "		IF gfx_match0(1) < 0 THEN\n"
	printf "			screen_shot <= -1;\n"
	printf "			WAIT FOR 1 sec;\n"
	printf "			ASSERT false REPORT \"timeout, screenshot taken\" SEVERITY failure;\n"
	printf "		END IF;\n"
	printf "		WAIT ON gfx_match0 UNTIL (gfx_match0(1) = -2);\n"
	printf "		ASSERT false REPORT \"found %s\" SEVERITY note;\n" $p2
	printf "\n"
	printf "		-- activate mouse detection\n"
	printf "		send_string(mouse_pointerdir, \"$POINTERDIR\");\n"
	printf "\n"
	printf "		-- move mouse to positon\n"
	printf "		control_mouse(x, y, mouse_match, mouse_event, mouse_x, mouse_y, mouse_dx, mouse_dy);\n"
	printf "\n"
	printf "		-- deactivate mouse detection\n"
	printf "		send_string(mouse_pointerdir, \"\");\n"
	printf "\n"
	printf "		-- double click mouse\n"
	printf "		mouse_button1 <= true;\n"
	printf "		WAIT FOR 1000 ms;\n"
	printf "		mouse_button1 <= false;\n"
	printf "		WAIT FOR 1000 ms;\n"
	printf "		mouse_button1 <= true;\n"
	printf "		WAIT FOR 1000 ms;\n"
	printf "		mouse_button1 <= false;\n"
	printf "		WAIT FOR 1000 ms;\n"
	printf "\n"

elif [ "$p1" = "floppy_insert" ] ; then
	#
	# floppy_insert	<character sequence>
	#
	if [ -f $p2 ] ; then
		img=$p2
	elif [ -f ../CDROM.images/$p2 ] ; then
		img=../CDROM.images/$p2
	else
		echo "$p2: Not found."
		exit 1
	fi
	printf "\n"
	printf "		-- floppy_insert %s\n" $img
	printf "		send_string(floppy, \"%s\");\n" $img
	printf "		wait for 1000 ms;\n"
	printf "\n"

elif [ "$p1" = "floppy_remove" ] ; then
	#
	# floppy_remove
	#
	printf "\n"
	printf "		-- floppy_remove\n"
	printf "		send_string(floppy, "'"");\n'
	printf "		wait for 1000 ms;\n"
	printf "\n"

elif [ "$p1" = "cdrom_insert" ] ; then
	#
	# cdrom_insert	<character sequence>
	#
	if [ -f $p2 ] ; then
		img=$p2
	elif [ -f ../CDROM.images/$p2 ] ; then
		img=../CDROM.images/$p2
	else
		echo "$p2: Not found."
		exit 1
	fi
	printf "\n"
	printf "		-- cdrom_insert %s\n" $img
	printf "		send_string(cdrom, \"%s\");\n" $img
	printf "		wait for 1000 ms;\n"
	printf "\n"

elif [ "$p1" = "cdrom_remove" ] ; then
	#
	# cdrom_remove
	#
	printf "\n"
	printf "		-- cdrom_remove\n"
	printf "		send_string(cdrom, "'"");\n'
	printf "		wait for 1000 ms;\n"
	printf "\n"

elif [ "$p1" = "sshot" ]; then
	#
	# sshot
	#
	printf "\n"
	printf "		-- take a screenshot\n"
	printf "		screen_shot <= -1;\n"
	printf "		WAIT FOR 1000 ms;\n"
	printf "		ASSERT false REPORT \"screenshot taken.\" SEVERITY note;\n"
	printf "\n"

elif [ "$p1" = "log" ]; then
	#
	# log		<character sequence>
	#
	printf "\n"
	printf "		ASSERT false REPORT \"%s\" SEVERITY note;\n" $p2
	printf "\n"

elif [ "$p1" = "" ] ; then
	#
	# null command
	#
	# do nothing...
	true

else
	#
	# Illegal command
	#
	echo "$0: $1: Illegal command $p1" 1>&2
	exit 1
fi

done
