# Parameter Interpreter
# jens.fischer@ixos.leipzig.de

# option signs
OPTIONS[0]=-h
OPTIONS[1]=-a
OPTIONS[2]=-n
OPTIONS[3]=-l
OPTIONS[4]=-d

# option types
OPTTYPES[0]=f
OPTTYPES[1]=t
OPTTYPES[2]=t
OPTTYPES[3]=t
OPTTYPES[4]=n

# names of option variables
OPTVARS[0]=help
OPTVARS[1]=archivist
OPTVARS[2]=archive
OPTVARS[3]=savelist
OPTVARS[4]=depth

# list of files
FILES=

# option variables
help=FALSE
archivist=${ARCHIVIST:-tar cvf}
archive=${ARCHIVE:-archiv}
savelist=${basedir}/etc/savelist
depth=2

getoptions()
{
    # loop for all command line parameters
    while [ $# -gt 0 ]
    do
        # substitute blanks for @'s
        pstr=`echo ${1} | tr @ ' '`

        j=0
        # loop for all options; break at empty OPTIONS component
        while [ ! -z "${OPTIONS[j]}" ]
        do
            # look if the command line parameter starts with a option sequence
            echo ${pstr} | egrep -e "^\\${OPTIONS[j]}" 1>/dev/null 2>&1
            if [ $? -eq 0 ]
            then
                # ok, we've found an option

                # look for the options type
                case ${OPTTYPES[j]} in
                    f) # switch the flag
                       eval flag='$'${OPTVARS[j]}
                       if [ "${flag}" = "TRUE" ]
                       then
                           eval ${OPTVARS[j]}=FALSE
                       else
                           eval ${OPTVARS[j]}=TRUE
                       fi;;
                    t) # catch the text parameter(s)
                       tvr=`echo ${pstr} | sed "s/^\\${OPTIONS[j]} *//"`
                       if [ -z "${tvr}" ]
                       then
                           shift
                           # substitute blanks for @'s
                           tvr=`echo ${1} | tr @ ' '`
                       fi
                       eval ${OPTVARS[j]}='${tvr}';;
                    n) # catch the digital parameter
                       nvr=`echo ${pstr} | sed "s/^\\${OPTIONS[j]} *//"`
                       if [ -z "${nvr}" ]
                       then
                           shift
                           eval nvr=${1}
                       fi

                       # has the parameter numerical format ?
                       err=`expr ${nvr} / 1 2>&1`
                       if [ $? -ne 0 ]
                       then
                           echo "non-numreric argument for option ${OPTIONS[j]}"
                           exit 1
                       fi

                       eval ${OPTVARS[j]}=${nvr};;
                esac

                break      # while loop
            fi

            let j=j+1
        done

        # no matching option found
        if [ -z "${OPTIONS[j]}" ]
        then
            echo ${1} | egrep -e "^[-+]" 1>/dev/null 2>&1
            if [ $? -eq 0 ]
            then
                echo "unknown option ${pstr}"
                exit 1
            else
                if [ -z "${FILES}" ]
                then
                    FILES=${1}
                else
                    FILES="${FILES} ${1}"
                fi
            fi
        fi

        shift
    done
}

usage()
{
    cat << USAGE_END 
usage : save [-h] [-a <archivist>] [-n <archive>] [-l <savelist>] [-d <depth>]
             [<files>]
    -h                ->  prints this help message;
    -a <archivist>    ->  changes the name of the archive program
                          (default : archivist = \${ARCHIVIST:-${archivist}})
    -n <archive>      ->  changes the name of the archive
                          (default : archive = \${ARCHIVE:-${archive}})
    -l <savelist>     ->  changes the name of the list of directories to save
                          (default : savelist = ${savelist})
    -d <depth>        ->  changes the depth archiving directories
                          (default : 2)
    <files>           ->  list of files not included in the <savelist>-file

NOTE: 
options and there arguments may be given connected or separated, e.g.
"-d3" or "-d 3";
if an option argument contains a space use a '@'-sign insteed, e.g.
"-a tar@cvf" runs the archive program as "tar cvf" 
USAGE_END
    exit 0
}

process()
{
    if [ "${help}" = "TRUE" ]
    then
        usage
    fi
}

TRACE()
{
    echo "help=<${help}>"
    echo "archivist=<${archivist}>"
    echo "archive=<${archive}>"
    echo "savelist=<${savelist}>"
    echo "depth=<${depth}>"
    echo "FILES=<${FILES}>"
}

main()
{
    getoptions $*
    TRACE
    process
}

main $*


