#!/bin/bash
#  $HeadURL: http://dione.no-ip.org/svn/ade/tags/1.7.6/lib/tests/bin/ADESTDTEST101_option_help.sh $ $LastChangedRevision: 3171 $
set -e
PROGNAME=`basename $0`
SANDPIT=`pwd`
. $(ade-config ade_include_prefix)/adetestsupport.sh

##############################################################################
#
#  LOAD TEST-SPECIFIC CONFIGURATION FILE
#
##############################################################################

[ "X$ADETEST_MODROOT" != X ] || error "'ADETEST_MODROOT' not defined"
[ -r "$ADETEST_MODROOT/tests/in/$PROGNAME" ] || error "can't read $ADETEST_MODROOT/tests/in/$PROGNAME"
. "$ADETEST_MODROOT/tests/in/$PROGNAME" || error "problem sourcing $ADETEST_MODROOT/tests/in/$PROGNAME"
[ "X$PROGRAMS" != X ] || error "'PROGRAMS' not defined"

##############################################################################
#
#  START OF TESTS
#
##############################################################################

echo "checking -h and --help return text that looks like usage message ..."
for PROGRAM in $PROGRAMS; do
    for OPTION in -h --help; do
        debug 4 "running '$PROGRAM $OPTION' and checking it looks like a usage message ..."
        ITSAID="`$PROGRAM $OPTION < /dev/null 2>&1 | sed -n \"s/^\\(Usage:[ 	][ 	]*\\)$PROGRAM[ 	][ 	]*.*/\\1BLANKED_BY_$PROGNAME/p\"`"
        expr "X$ITSAID" : "XUsage:[ 	][ 	]*BLANKED_BY_$PROGNAME\$" > /dev/null || { echo "'$PROGRAM $OPTION' failed: it said: $ITSAID"; false; }
    done
done

echo "checking -h and --help exit codes and output stream with ..."
for PROGRAM in $PROGRAMS; do
    for OPTION in -h --help; do
        debug 4 "running '$PROGRAM $OPTION' and checking return code ..."
        $PROGRAM $OPTION > /dev/null < /dev/null
    done
done

echo "checking -h and --help reply format ..."
for PROGRAM in $PROGRAMS; do
    debug 4 "checking $PROGRAM ..."
    for OPTION in -h --help; do
        debug 4 "running '$PROGRAM $OPTION' and checking reply format ..."
        OUTPUT="`$PROGRAM $OPTION < /dev/null 2>&1`" || true
        #debug 4 "output was $OUTPUT"
        echo "$OUTPUT" | sed -n 's/.\{80,80\}//p'
    done
done

echo "checking -h and --help messages include the right '-V' text ..."
for PROGRAM in $PROGRAMS; do
    for OPTION in -h --help; do
        debug 4 "running '$PROGRAM $OPTION' and checking third line ..."
        [ "X`$PROGRAM $OPTION < /dev/null 2>&1 | sed -n '/^Options: /p' | sed -e 's/  */ /g'`" = "XOptions: -V | --version display program version" ] || error "'Options: ' line didn't match desired format"
    done
done

#  leave this in to facilitate easier detection of silent non-non-zero
#  exits from above loop.
echo "end of tests"
