#! /bin/sh
#
# Name: spxtest
# Author: SpinetiX S.A.
# Copyright: 2007 SpinetiX S.A.
# License: 2007 (c) SpinetiX S.A. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
#
# Copyright 1999-2003 MontaVista Software, Inc.
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: automatic test
# Description: automatic test which only runs on first boot
### END INIT INFO
# chkconfig: 2345 99 99

# Init script information
INIT_NAME=spxtest
DESC="SpinetiX test"

# Defaults
STATUSFILE=/etc/spxtest/status
STATUS=done
WAIT=10
TYPE=local
MOUNTPOINT=/media/usb1
TESTSCRIPT=spxtest.sh

# Load init script configuration
[ -f /etc/default/$INIT_NAME ] && . /etc/default/$INIT_NAME

# Source the init script functions
. /etc/init.d/init-functions

parse_args() {
    TYPE="${1%%:*}"
    TYPEARGS="${1#*:}"
}

do_local() {
    local ret

    if [ -f "$MOUNTPOINT/$TESTSCRIPT" ]; then
	log_status_msg "starting... " -n

	tmpdir="$(mktemp -q -d /tmp/spxtest.XXXXXX)"
	if [ $? -ne 0 ]; then
	    log_failure_msg "cannot create temp dir " -n
	    return 1
	fi

	(cd "$tmpdir" && bash "$MOUNTPOINT/$TESTSCRIPT")
	ret=$?
	# Be sure the mountpoing stays ro
	mount -o ro,remount "$MOUNTPOINT"
    else
	log_failure_msg "no test script found, " -n
	ret=0
    fi
    return $ret
}

led_fail() {
    local ledfile=/sys/devices/system/leds/leds0/event
    if [ -f "$ledfile" ]; then
	echo red on > "$ledfile"
    fi
}

do_net() {
    local ret url scheme tmpdir file

    url="$1"
    if [ -z "$url" ]; then
	log_failure_msg "no URL provided " -n
	return 1
    fi

    scheme="${url%%://*}"
    tmpdir="$(mktemp -q -d /tmp/spxtest.XXXXXX)"
    if [ $? -ne 0 ]; then
	log_failure_msg "cannot create temp dir " -n
	return 1
    fi
    file="$tmpdir/spxtest"

    log_status_msg "downloading... " -n
    case "$scheme" in
	http|ftp)
            wget -q -t 1 -T 10 -O "$file" "$url"
	    ret=$?
	    ;;
	tftp)
	    local host path url_path
	    url_path="${url#${scheme}://}"
	    host="${url_path%%/*}"
	    path="/${url_path#*/}"
	    if [ -z "$host" -o -z "$path" ]; then
		log_failure_msg "missing TFTP host or path in URL " -n
		ret=1
	    else
		tftp -m binary "$host" -c get "$path" "$file" < /dev/null
		ret=$?
		# TFTP does not always return non-zero exit code on failure
		if [ "$ret" -eq 0 -a ! -s "$file" ]; then
		    ret=1
		fi
	    fi
	    ;;
	*)
	    log_failure_msg "unrecognized '$scheme' scheme " -n
	    ret=1
	    ;;
    esac

    if [ $ret -ne 0 ]; then
	log_failure_msg "failed downloading test " -n
	rm -rf "$tmpdir"
	return $ret
    fi

    log_status_msg "starting... " -n
    chmod +x "$file"
    (cd "$tmpdir" && "$file")
    ret=$?
    rm -rf "$tmpdir"

    return $ret
}

start() {
	local RET ERROR=

        KARGS="$(sed -e 's/.*\bspxtest=\([^[:space:]]\+\).*/\1/p;d' \
            < /proc/cmdline)"

	if [ -z "$KARGS" ]; then
	    STATUS=done
	    if [ -f "$STATUSFILE" ]; then
		. "$STATUSFILE"
	    fi
	else
	    parse_args "$KARGS"
	    WAIT=
	    STATUS=start
	fi

	if [ "$STATUS" != "start" ]; then
	    return 0
	fi
	
	log_status_msg "Starting $DESC: " -n

	log_status_msg "marking as done, " -n

	echo -e "# The status variable indicates if we should 'start' or we are 'done'\nSTATUS=done" > "$STATUSFILE"

	if [ -n "$WAIT" ]; then
	    log_status_msg "waiting $WAIT secs... " -n
	    sleep "$WAIT"
	fi

	case "$TYPE" in
	    local)
                do_local "$TYPEARGS"
		RET=$?
		;;
	    net)
	        do_net "$TYPEARGS"
		RET=$?
		if [ $RET -ne 0 ]; then
		    led_fail
		fi
		;;
	    *)
	        log_failure_msg "Unrecognized test type '$TYPE'" -n
		RET=$?
	esac

	if [ $RET -eq 0 ]; then
		log_success_msg "done. "
	else
		log_failure_msg " failed ($RET: $ERROR)."
		return 1
	fi
	
	log_status_msg ""
	return 0
}

parse() {
	case "$1" in
		start)
			start
			return $?
	;;
		stop)
			return 0
	;;
		*)
			echo "Usage: $INIT_NAME " \
			"{start|stop}" >&2
	;;
	esac
	
	return 1
}

parse $@
