123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #!/bin/bash
-
- . $(ffoom path) || exit 3
-
- ffoo import exit
- ffoo import inigrep
- ffoo import pretty
-
- #
- # =====BEGIN BUILT PART=====
- #
-
- FFOO_INIGREP_PATH="$(
- inigrep__mkpath \
- __SATURNIN_CONFIG_USER__/ini.d \
- __SATURNIN_CONFIG_USER__ \
- __SATURNIN_CONFIG_LOCAL__ \
- __SATURNIN_SHARE__/ini.d
- )"
-
- FFOO_PATH="__SATURNIN_FFOO_DIR__"
- FFOO_PRETTY_USAGE="subcommand"
- SATURNIN_CACHE_HOME="__SATURNIN_CACHE_HOME__"
- SATURNIN_LIBEXEC="__SATURNIN_LIBEXEC__"
- SATURNIN_VERSION="__VERSION__"
-
- #
- # =====END BUILT PART=====
- #
-
- export FFOO_INIGREP_PATH FFOO_PATH FFOO_PRETTY_USAGE \
- SATURNIN_VERSION SATURNIN_CACHE_HOME
-
- usage() {
- mkusage "[-d|-v] command [args...]" \
- "help" \
- "--version"
- }
-
- print_help() {
- echo ""
- echo "built-in sub-commands:"
- echo ""
- echo " conf"
- echo " help"
- echo ""
- echo "installed sub-commands:"
- echo ""
- ls_subcommands | sed -e 's/^/ /'
- return "$FFOO_EXIT_OK"
- }
-
- ls_subcommands() {
- find "$SATURNIN_LIBEXEC" \
- -mindepth 1 \
- -maxdepth 1 \
- -executable \
- | sed -e 's/.*saturnin-//' \
- | sort
- }
-
- version_info() {
- echo "$(basename $0) (Smart and ready desktop helper) $SATURNIN_VERSION"
- exit $FFOO_EXIT_OK
- }
-
- run_hook() {
- #
- # Run custom hook
- #
- local hname="$1"
- local hook_code
- test -n "$SATURNIN_SUBCOMMAND" || {
- warn "unknown subcommand, ignoring hook: $hname"
- return 0
- }
- hook_code="$(inigrep -j -p hook.$SATURNIN_SUBCOMMAND.$hname)"
- debug -v SATURNIN_SUBCOMMAND hook_code hname
- bash -n <<<"$hook_code" || {
- warn "syntax errors, ignoring hook: $hname"
- return 0
- }
- eval "$hook_code"
- }
-
- subcommand() {
- local sc_es=0 # future exit (return) status
- local lexpath="$SATURNIN_LIBEXEC/saturnin-$SATURNIN_SUBCOMMAND"
- debug -v lexpath
- debug "\$*='$*'"
- test -x "$lexpath" || {
- warn "invalid sub-command: $SATURNIN_SUBCOMMAND"
- print_help
- return $FFOO_EXIT_USAGE
- }
- run_hook pre
- "$lexpath" "$@" || return $?
- sc_es=$?
- run_hook post
- return $sc_es
- }
-
- while true; do case $1 in
- -d|--debug) export FFOO_DEBUG=true; shift ;;
- -v|--verbose) export FFOO_VERBOSE=true; shift ;;
- --version) version_info ;;
- --version-semver) echo "$SATURNIN_VERSION"; exit ;;
- -*) usage; ;;
- --*) usage; ;;
- --) shift; break ;;
- "") usage; ;;
- *) break; ;;
- esac done
-
- debug -v FFOO_PATH SATURNIN_LIBEXEC FFOO_INIGREP_PATH
- debug "\$*='$*'"
-
- export SATURNIN_SUBCOMMAND="$1"
- shift
-
- case "$SATURNIN_SUBCOMMAND" in
- conf) inigrep "$@" ;;
- help) print_help ;;
- _ls_subcommands) ls_subcommands ;;
- _lsfun) ffoom lsfun ;;
- _lsmod) ffoom lsmod ;;
- _ffrun) ffrun "$@" ;;
- *) subcommand "$@" ;;
- esac
|