#!/bin/zsh

### Statically source all bundles from the plugins file.
#function antidote-load {
  if [[ "$1" == -h || "$1" == --help ]]; then
    antidote-help load
    return $?
  fi

  local ret=0
  typeset -g REPLY=
  () {
    emulate -L zsh
    setopt local_options extended_glob no_monitor pipefail

    local bundlefile="$1"
    if [[ -z "$bundlefile" ]]; then
      zstyle -s ':antidote:bundle' file 'bundlefile' ||
        bundlefile=${ZDOTDIR:-$HOME}/.zsh_plugins.txt
    fi

    local staticfile="$2"
    if [[ -z "$staticfile" ]]; then
      zstyle -s ':antidote:static' file 'staticfile'
      if [[ -z "$staticfile" ]]; then
        if [[ -z "$bundlefile:t:r" ]]; then
          staticfile=${bundlefile}.zsh
        else
          staticfile=${bundlefile:r}.zsh
        fi
      fi
    fi

    if [[ ! -e "$bundlefile" ]]; then
      print -ru2 -- "antidote: bundle file not found '$bundlefile'."
      return 1
    elif [[ "$bundlefile" == "$staticfile" ]]; then
      print -ru2 -- "antidote: bundle file and static file are the same '$bundlefile'."
      return 1
    fi

    local force_bundle=0
    if ! zstyle -t ':antidote:load:checkfile' disabled; then
      # Resolve home in this shell (sets REPLY) rather than forking
      # `antidote home` on every startup. >/dev/null drops the print.
      local ahome
      antidote-home >/dev/null; ahome=$REPLY
      local loadable_check_path="$ahome/.antidote.load"
      if [[ ! -e $loadable_check_path ]]; then
        force_bundle=1
        [[ -d $loadable_check_path:h ]] || mkdir -p $loadable_check_path:h
        touch $loadable_check_path
      fi
    fi

    if [[ ! $staticfile -nt $bundlefile ]] || [[ $force_bundle -eq 1 ]]; then
      mkdir -p "${staticfile:A:h}" || return 2
      if antidote bundle <"$bundlefile" >|"${staticfile}.new"; then
        command mv -f -- "${staticfile}.new" "$staticfile" || {
          command rm -f -- "${staticfile}.new"; return 2
        }
      else
        command rm -f -- "${staticfile}.new"
        (( force_bundle )) && command rm -f -- "$loadable_check_path"
        typeset -g REPLY=$staticfile
        return 1
      fi
      if [[ -r "${staticfile}.zwc" ]] && ! zstyle -t ':antidote:static' zcompile; then
        antidote-zsh __private__ del -f -- "${staticfile}.zwc"
      fi
    fi

    typeset -g REPLY=$staticfile
  } "$@" || ret=$?
  { [[ -f "$REPLY" ]] && source "$REPLY" } || (( ret )) || ret=2
  unset REPLY
  return $ret
#}
