#!/usr/bin/bash

command="$0 $*"
params="$(getopt -o hs:R:m:r:t: -l help,state:,result_server:,machine:,recipesetid:,testorder: --name "$(basename -- "$0")" -- "$@")"

function usage ()
{
    cat <<EOF
Usage:
	$0 -s [STATE] [OPTION...]

Sets the state for the local machine.  Used for simple multi-host testing.

Help Options:
 -h, --help	Show help options

Application Options:
 -s, --state=STATE	Sets the current state
 -t, --testorder=ORDER	override the task/test order
EOF
    exit 1
}

eval set -- "$params"
unset params

while true
do
    case $1 in
        -s|--state)
            shift
            state=$1
            shift
            ;;
        -R|--result_server)
            shift
            shift
            ;;
        -m|--machine)
            shift
            shift
            ;;
        -r|--recipesetid)
            shift
            shift
            ;;
        -t|--testorder)
            shift
            TASKORDER=$1
            shift
            ;;
        --)
            shift
            break
            ;;
        *)
            usage
            ;;
    esac
done

if [ -z "$state" ]; then
    usage
fi
basename=$(basename -- $0)
if [ "$basename" = "rhts-sync-set" -o \
     "$basename" = "rhts_sync_set" -o \
     "$basename" = "rstrnt-sync-set" ]; then
    XTRA="${TASKORDER}"
fi
echo "$command -- State set = ${XTRA}_${state}"

rstrnt-sync set ${XTRA}_${state}
# Record the event in case any tests reboot the system
echo "${XTRA}_${state}" >> /var/lib/restraint/rstrnt_events
