1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #!/bin/bash
- #shellcheck disable=SC1090
-
- . "$(sfpath)" || exit 3
-
- shellfu import pretty
-
- IMAPDOMO_CFGDIR="__IMAPDOMO_CONFIG_USER__"
- IMAPDOMO_USER_CACHE="__IMAPDOMO_USER_CACHE__"
- IMAPDOMO_HOME="__IMAPDOMO_SHARE__"
-
-
- usage() {
- mkusage "$@" "[options] ACTION" \
- -o \
- "-c DIR change to DIR before doing anything" \
- "-l list handlers and exit" \
- "-d turn on debugging mode" \
- -- \
- "imapdomo will try to read init.lua and handlers/ACTION.lua from its" \
- "configuration directory." \
- "" \
- "See imapfilter_config(5)) for guide and API reference. Few functions"\
- "are also available in .imapdomo/common.lua"
- }
-
- mkcmd() {
- #
- # Compose imapfilter command
- #
- echo -n "IMAPDOMO_ACTION=$Action"
- echo -n " IMAPDOMO_HEADERS=$HeaderDir"
- echo -n " IMAPFILTER_HOME=$CfgDir"
- echo -n " imapfilter"
- if $Debug
- then
- mkdir -p "$LogDir"
- echo -n " -d $LogDir/debug.log"
- echo -n " -v"
- fi
- echo -n " -c $IMAPDOMO_HOME/main.lua"
- }
-
- show_version() {
- echo '__MKIT_PROJ_NAME__ (__MKIT_PROJ_TAGLINE__) __MKIT_PROJ_VERSION__'
- exit 0
- }
-
- show_semversion() {
- echo '__MKIT_PROJ_VERSION__'
- exit 0
- }
-
- lshandlers() {
- #
- # List recognized handlers
- #
- find "$IMAPDOMO_HOME/handlers" -name "*.lua" -printf "%f\n" \
- | sed 's/.lua$//'
- }
-
- main() {
- local Action # what to do
- local Debug # true if debugging
- local cmd # imapfilter command
- local CfgDir # config directory
- local LogDir # directory to store logs
- local HeaderDir # directory to store headers by save_header()
- local CdTo # change dir to this before running imapfilter
- CfgDir="$IMAPDOMO_CFGDIR"
- LogDir="$IMAPDOMO_USER_CACHE/logs"
- HeaderDir="$IMAPDOMO_USER_CACHE/headers"
- Debug=false
- #shellcheck disable=SC2034
- while true; do case $1 in
- -c) CdTo="$2"; shift 2 || usage -w "missing value to: $1" ;;
- -d) Debug=true; PRETTY_DEBUG=true; shift ;;
- -l) lshandlers; exit ;;
- -V|--version-semver) show_semversion ;;
- --version) show_version ;;
- -*) usage -w "unknown argument: '$1'" ;;
- *) break ;;
- esac done
- Action="$1"; shift
- lshandlers | grep -qw "$Action" \
- || dle "no handler for action: $Action.lua in $IMAPDOMO_HOME/handlers"
- cmd=$(mkcmd)
- debug -v cmd
- bash -n <<<"$cmd" || die
- if test -n "$CdTo";
- then
- cd "$CdTo" || die
- fi
- mkdir -p "$HeaderDir" || die
- eval "$cmd"
- }
-
- main "$@"
|