#!/bin/bash

SSH_COMMAND='rsh'

if [ ! -e "${PTXDIST_PLATFORMCONFIG}" ]; then
	echo "platformconfig is missing"
	exit 1
fi

. "${PTXDIST_PLATFORMCONFIG}"

if test -f "${PTXDIST_TOPDIR}/tests/libptxdisttest.sh"
then
        source "${PTXDIST_TOPDIR}/tests/libptxdisttest.sh"
else
        echo "${PTXDIST_TOPDIR}/tests/libptxdisttest.sh not found." >&2
        exit 1
fi


if [ ! -e ${PTXDIST_WORKSPACE}/tests/common_testlib ]; then
        echo "${PTXDIST_WORKSPACE}/tests/common_testlib not found." >&2
        exit 1
fi

. ${PTXDIST_WORKSPACE}/tests/common_testlib


#
# sanity checks
#
acctest_boardsetup

. "${PTXDIST_BOARDSETUP}"

#
# do we have network access to the target?
#
acctest_ping

#
# check for SSH server running on target, also
# if dropbear is running on the target rather than sshd, we can't do
# automated tests, due to a required password entry on every connection.
#
nmap=$(nmap ${PTXCONF_BOARDSETUP_TARGETIP})
test "$(echo "$nmap" | grep "^22/" | sed 's/  */ /g')" = "22/tcp open ssh"
ssh_avail=$?
test "$(echo "$nmap" | grep "^514/" | sed 's/  */ /g')" = "514/tcp open shell"
rsh_avail=$?

if test "$ssh_avail" = "1" -a "$SSH_COMMAND" = "ssh"
then
	echo >&2
	echo SSH is selected as connection method. Port 22 isn\'t open though, >&2
	echo so trying RSH as fallback instead. >&2
	SSH_COMMAND='rsh'

elif test "$ssh_avail" = "0" -a "$SSH_COMMAND" = "ssh"
then
	ret=$(curl -s -m 1 "telnet://${PTXCONF_BOARDSETUP_TARGETIP}:22")
	if [ $? = 7 ] # curl: (7) couldn't connect to host
	then
		echo >&2
		echo There is no SSH server reachable on the target. >&2
		echo Cannot run automated tests, exiting. >&2
		echo Please start an SSH server \(e.g. sshd\) on the target. >&2
		exit 1
	fi
	if echo "$ret" | grep dropbear
	then
		echo >&2
		echo Sorry, dropbear is running as SSH server on the target. >&2
		echo Cannot run automated tests, because of password entry difficulty. >&2
		echo Exiting. >&2
		exit 1
	fi
fi

if test "$rsh_avail" = "1" -a "$SSH_COMMAND" = "rsh"
then
	echo >&2
	echo RSH is selected as connection method. Port 514 isn\'t open though, exiting. >&2
	echo Please start an rsh server \(e.g. urshd\) on the target. >&2
	exit 1

elif test "$rsh_avail" = "0" -a "$SSH_COMMAND" = "rsh"
then
	if ! remote_compare 'echo RSH RESPONSE TEST' 'RSH RESPONSE TEST'
	then
		echo >&2
		echo RSH is selected as connection method. Port 514 seems to be open, >&2
		echo but the RSH server does not respond as expected. Exiting. >&2
		exit 1
	fi
fi


#
# check for required PTXdist version [EDITME]
#
acctest_ptxdist 2013.03.0

#
# no need to continue if vital things are missing
#
all_on_board



# USB-Stick in port 1
message_to_user "Please insert a USB stick into the device and press any key" \
		"CAUTION: All data will be lost!"
read -s -n1
acctest_filesystem "USB-Stick" "sda" "sda1" "vfat" "/sbin/mkfs.vfat -F32" ",,b"

# USB-Stick in port 2
message_to_user "Please insert a second USB stick into the other free port and press any key" \
		"CAUTION: All data will be lost!"
read -s -n1
acctest_filesystem "USB-Stick" "sdb" "sdb1" "vfat" "/sbin/mkfs.vfat -F32" ",,b"

# SD-Card
# Note: if the SD card has a partition table the device node is mmcblk0p1
# If it has no partition table the device node is mmcblk0
acctest_filesystem "SDCard" "mmcblk0" "mmcblk0" "vfat" "/sbin/mkfs.vfat -F32"

# GPIO-Expander via audible buzzer
# FIXME: don't hardcode 250 but use base(pca9554) + 2
test_begin
message_to_user "Press any key and listen for sound from buzzer"
remote 'echo 250 > /sys/class/gpio/export'
remote 'echo out > /sys/class/gpio/gpio250/direction'
remote 'echo 1 > /sys/class/gpio/gpio250/value'
remote 'sleep 1'
remote 'echo 0 > /sys/class/gpio/gpio250/value'
line_from_user "Was the tone audible?"
ANSWER=${REPLY:0:1}
checking 'GPIO Expander functionality and buzzer'
[ "$ANSWER" = "y" -o "$ANSWER" = "Y" ]
result
test_end


# test for correct timer setup
acctest_compare_time 20

#
echo -n All tests completed. >&2
if [ $fail_count -gt 0 ]
then
	printf "%8b" "${RED} $fail_count TEST(S) FAILED.${NC}\n" >&2
	exit 1
else
	echo >&2
	exit 0
fi

