lib/pvl/main.sh
author Tero Marttila <tero.marttila@aalto.fi>
Thu, 26 Feb 2015 21:38:09 +0200
changeset 627 a81206440be2
child 628 b10ad946d01d
permissions -rw-r--r--
(none)
set -ue

shopt -s globstar nullglob

. $LIB/pvl/util.sh
. $LIB/pvl/cmd.sh
. $LIB/pvl/log.sh

function main_help {
    cat <<END
Usage: $0 [options]

General:
    -h      display this help text
END
    
    for module in ${MODULES[@]}; do
        ${module}_help
    done
}

function main_opts {
    local module=

    # build opts string
    local opts=$(
        echo -n 'h'

        for module in ${MODULES[@]}; do
            module_getopts=${module}_GETOPTS
            echo -n ${!module_getopts}
        done
    )

    local OPTIND
    while getopts "$opts" opt; do
        local opt_module=

        if [ "$opt" = 'h' ]; then
            main_help
            exit 0;
        fi

        for module in ${MODULES[@]}; do
            if ${module}_opt $opt "${OPTARG:-}"; then
                opt_module=$module
            fi
        done

        if [ "$opt_module" ]; then
            continue
        else
            die "opt: $opt"
        fi
    done
    shift $(($OPTIND - 1))
}

function main {
    local module=

    main_opts "$@"

    for module in ${MODULES[@]}; do
        func_test ${module}_init && ${module}_init
    done

    ${MODULE}_main     
}