#!/bin/zsh

### The main controller for antidote.
# The reason we use `antidote-main` instead putting all of this in `antidote`
# is that this allows the `antidote` function to be overridden via `antidote init`.
# The init command switches antidote from static mode to dynamic mode, but this
# core functionality remains.
#function antidote-main {
  0=${(%):-%x}

  [[ -o KSH_ARRAYS ]] && __adote_ksh_arrays=1 && unsetopt KSH_ARRAYS
  [[ -o SH_GLOB    ]] && __adote_sh_glob=1    && unsetopt SH_GLOB

  local o_help o_version
  zparseopts ${_adote_zparopt_flags} -- \
    h=o_help    -help=h    \
    v=o_version -version=v ||
    return 1

  local ret=0

  if (( ${#o_version} )); then
    __antidote_version

  elif (( ${#o_help} )); then
    antidote-help "$@"

  elif [[ ${#} -eq 0 ]]; then
    antidote-help
    ret=2

  elif [[ "${1}" = help ]]; then
    local manpage=${2:-antidote}
    antidote-help $manpage

  elif (( $+functions[antidote-${1}] )); then
    local cmd=${1}; shift
    antidote-${cmd} "$@"
    ret=$?

  else
    print -ru2 "antidote: command not found '${1}'"
    ret=1
  fi

  (( __adote_ksh_arrays )) && __adote_ksh_arrays=0 && setopt KSH_ARRAYS
  (( __adote_sh_glob ))    && __adote_sh_glob=0    && setopt SH_GLOB
  return $ret
#}
