#!/bin/bash
set -e
APP_SVNID='$HeadURL: http://dione.no-ip.org/svn/ade/tags/1.7.6/bin/adeshf.sh $ $LastChangedRevision: 5290 $'

. $(ade-config ade_include_prefix)/ade.sh

ADESHF_DEFINED_ERRORS=(
    "KEY=ADESHF_ERR_MISC; FMT=\"%s\""
)

adeshf()
{
    local OPTVAL RC TEMP ERRSTACK_REF CODE
    local -a DOLLARAT

    ERRSTACK_REF="$1"
    shift

    ###########################################################################
    #
    #  SET ADE OPTIONS
    #
    ###########################################################################

    #  Register application-specific errors
    ade_err_registerdefderrs ADESHF_DEFINED_ERRORS

    ###########################################################################
    #
    #  PROCESS OPTIONS
    #
    ###########################################################################

    #  Register adeshf's options (none)
    #ade_opt_register "$ERRSTACK_REF" -o f: --longoptions=op-mode:,alert: --callback-template="adeshf_opt_handler_%s" || return $?
    ade_msg_register "$ERRSTACK_REF" adeshf_usage adeshf_version adeshf_listpaths || return $?

    #  Process options
    ade_opt_process "$ERRSTACK_REF" NEW_DOLLAR_AT "$@" || return $?
    set -- "${NEW_DOLLAR_AT[@]}" 

    ##########################################################################
    #
    #  ARGUMENT PROCESSING
    #
    ##########################################################################

    [ "X$1" != X -a "X$2" = X ] || ade_msg_usage "$ERRSTACK_REF"
    CODE="$1"

    ##########################################################################
    #
    #  FORKING AND LOCKING
    #
    ##########################################################################

    #ade_lck_lock "$ERRSTACK_REF" DUMMY ade_lck_getgenericlockfilename || {
    #    RC=$?
    #    ade_err_error "$ERRSTACK_REF" ADESHF_ERR_SEEABOVE
    #    return $RC
    #}

    ##########################################################################
    #
    #   GUTS START HERE
    #
    ##########################################################################
    
    ade_err_debug "$ERRSTACK_REF" 10 "main: calling \"$CODE\" ..."
    #  In Perl, the return code of eval can be undefined meaning that the code
    #  failed to execute, but shell has no such facility, so we cannot differentiate
    #  between code that fails and code that doesn't compile.
    eval "$CODE"
    RC=$?
    if [ $RC != $ADE_ERR_OK ]; then
        ade_err_error "$ERRSTACK_REF" ADESHF_ERR_MISC "code returned non-zero ($RC)"
        #  We cannot guarantee that the call provided an ADE-compilant exit code,
        #  so don't return $RC, instead force failure.
        return $ADE_ERR_FAIL
    fi

    return $ADE_ERR_OK
}

adeshf_usage() 
{
    local ERRSTACK_REF="$1"; shift
    local USAGETEXT_REF="$1"; shift
    local PASSNO=$1; shift

    if [ $PASSNO = 1 ]; then
        eval "$USAGETEXT_REF=\"<code>\""
    elif [ $PASSNO = 2 ]; then
        eval "$USAGETEXT_REF=\"\
        \""
    fi

    return $ADE_ERR_OK
}

adeshf_listpaths() 
{
    local ERRSTACK_REF="$1"; shift
    local PATHLIST_REF=$1; shift

    eval "$PATHLIST_REF=\"\
    \""

    return $ADE_ERR_OK
} 

adeshf_version() 
{
    local ERRSTACK_REF="$1"; shift
    local VERSION_REF=$1; shift

    ade_smf_extractversionfromsvnstring "$ERRSTACK_REF" "$APP_SVNID" "$VERSION_REF"

    return $ADE_ERR_OK
} 

ade_gep_main adeshf "$@"
