#compdef cast

_cast () {
  local state='' lstate
  local ret=1
 
  # Do simple completions or get the first state.
  _arguments -C -s \
    '(-n --no-fix)'{--fix+,-f+}'[Discover and fix broken spells]:*:fix:->fix' \
    '(--fix -f)'{--nofix+,-n+}'[Discover, but do not fix broken spells]' \
    '-s+[Download 1st, cast 2nd]' \
    {-c,--compile}'[Ignore /var/cache/sorcery and compiles]' \
    {-r,--reconfigure}'[Select new dependencies for spells]' \
    '--deps[Only configure spells and determine dependencies]' \
    '--from+[Specify an alternate for /var/spool/sorcery]:*:from:_summon_from' \
    '--url+[Specify an alternate download URL]:*:url:_summon_url' \
    '--pam[re-casts all installed spells that can use Linux-PAM]' \
    '*:install:->install' && ret=0

  ## If mode is defined. ##
  while [[ -n "$state" ]]; do

    # Copy the state and reset `state', to simplify the test below.
    lstate="$state"
    state=''


    # Dispatch...
    case "$lstate" in
    fix)
      _installed_spells
      ;;
    install)
      _spells
      ;;
    dir)
      _directories && ret=0
      ;;
    url)
      if [[ -prefix 1 (svn|svn_ssh|file|dir|cvs|rsync|smgl_tla|svn_http|svn_https|git|git_http|git_local|ftp|http|hg_http):// ]]; then
        _urls -f && ret=0
      else
        compadd -S '' ftp:// http:// svn:// svn_ssh:// file:// dir:// cvs:// rsync:// smgl_tla:// svn_http:// svn_https:// git:// git_http:// git_local:// hg_http://
      fi
      ;;
    esac

  done

}

_cast "$@"
