lib/pvl/main.sh
changeset 627 a81206440be2
child 628 b10ad946d01d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/pvl/main.sh	Thu Feb 26 21:38:09 2015 +0200
@@ -0,0 +1,70 @@
+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     
+}
+