#!/bin/sh
#?
#? NAME
#?      $0 - simple wrapper to call  o-saft.pl or o-saft.tcl
#?
#? SYNOPSIS
#?      $0 [options] target
#?
#? DESCRIPTION
#?      Calls  o-saft.pl or o-saft.tcl  with all specified arguments.
#?      o-saft.pl's  output is piped to the command specified with the  -post=
#?      option. The command given there must be located in contrib/ directory,
#?      or must be found via  PATH  environment variable. The default is  cat,
#?      which means that all output is on  STDOUT.
#?
#? OPTIONS
#?      -h      got it
#?      -list   list available programs in ./contrib/ directory
#?      -cli    start o-saft.pl  with remaining arguments; this is the default
#?      -cgi    start o-saft.cgi with remaining arguments
#?      -gui    start o-saft.tcl with remaining arguments
#       -tcl    alias for -gui
#?      -docker start o-saft-docker with remaining arguments
#       -id=*   passed to o-saft-docker
#       -tag=*  passed to o-saft-docker
#?      -post="command [options]"
#?              command to pipe output of  o-saft.pl  to
#?      --      pass all remaining arguments to  o-saft.pl  or  o-saft.tcl
#?
#? LIMITATIONS
#?      All option listed above must preceed options to be passed through.
#?
#? EXAMPLES
#?          $0 +cmd --option target
#?          $0 -post='bunt.pl'      ' +cmd --option target
#?          $0 -post='bunt.pl -blind' +cmd --option target
#?          $0 -gui target
#?
#?          $0 -docker +cmd --option target
#?          $0 -docker -post='bunt.pl'      ' +cmd --option target
#?          $0 -docker -post='bunt.pl -blind' +cmd --option target
#?          $0 -docker -gui target
#?
# 
# Hacker's INFO
#       Uses options  -h  and  -post=  and not  --h  or  --help  or  --post  to
#       avoid conflicts with same option for  o-saft.pl .
#
#? VERSION
#?      @(#) o-saft 1.9 18/07/16 14:36:07
#?
#? AUTHOR
#?      17-dec-17 Achim Hoffmann
#?
# -----------------------------------------------------------------------------

ich=${0##*/}
dir=${0%/*};    [ "$dir" = "$0" ] && dir="."    # $0 found via $PATH in .
try=
prg=$ich.pl     # most likely ich=o-saft
gui=$ich.tcl
cgi=$ich.cgi
post="cat"      # default, avoids special handling if -post= missing
mode=cli        # cli, cgi, gui
contrib=$dir/contrib
docker=0        # cli or gui mode
docker_id=
docker_tag=

while [ $# -gt 0 ]; do
    case "$1" in
        -h  | '-?')
            \sed -ne "s/\$0/$ich/g" -e '/^#?/s/#?//p' $0
            \cat <<EoT
 NOTE
    To get help for  o-saft.pl, please use:
        $0 --help
    or
        o-saft.pl --help

EoT
            exit 0
            ;;
        -list)
            for exe in $contrib/* ; do
                [ -d "$exe" ] && continue
                [ -x "$exe" ] && echo "$exe"
            done
            exit 0
            ;;
        -n      | --n)      try=echo    ; ;;
        -cgi    | --cgi)    mode=cgi    ; ;;
        -cli    | --cli)    mode=cli    ; ;;
        -gui    | --gui)    mode=gui    ; ;;
        -tcl    | --tcl)    mode=gui    ; ;;
        -docker | --docker) docker=1    ; ;;
        -id*)           docker_id="$1"  ; ;;
        -tag*)          docker_tag="$1" ; ;;
        -post=*)
            post="`expr "$1" ':' '-post=\(.*\)'`"
            cmd=`echo "$post" | awk '{print $1}'`   # remove additional trailing arguments
            [ -x "$contrib/$cmd" ] && post="$contrib/$cmd"
            shift
            break
            ;;
        --) shift; break; ;;
        *)  break; ;;
    esac
    shift
done

# Note: using $* (instead of $@) does not contain parsed options
[ -n "$try" ] && echo "# docker=$docker, mode=$mode, post=$post, $*#"

# docker mode is special
if [ $docker -eq 1 ]; then
    if [ -n "$osaft_vm_build" ]; then
        echo "**WARNING: option -docker ignored inside VM; exit"
    else
        echo o-saft-docker $docker_id $docker_tag -$mode -post=$post $*
        o-saft-docker $docker_id $docker_tag -$mode -post=$post $*
        exit $?
    fi
fi

# all other modes
case "$mode" in
   #cgi) $try $cgi $post $* ; ;;
    gui) $try $gui --post=$post $* ; ;;
    *)   if [ -n "$try" ]; then
            $try "$prg $docker_id $docker_tag $* | $post"
         else
            echo "$prg $docker_id $docker_tag $* | $post"
            $prg $docker_id $docker_tag $* | $post 
         fi
         ;;
esac
exit 0
