#!/bin/zsh

### Statically source all bundles from the plugins file.
#
# usage: antidote load [-h|--help] [<bundlefile> [<staticfile>]]
#
#function antidote-load {
  setopt extended_glob

  local o_help
  zparseopts $_adote_zparopt_flags -- h=o_help -help=h || return 1

  if (( $#o_help )); then
    antidote-help load
    return
  fi

  # pass in bundle file, read from zstyle, or use default .zsh_plugins.txt
  local bundlefile="$1"
  if [[ -z "$bundlefile" ]]; then
    zstyle -s ':antidote:bundle' file 'bundlefile' ||
      bundlefile=${ZDOTDIR:-$HOME}/.zsh_plugins.txt
  fi

  # pass in static file, read from zstyle, change extension, or use default .zsh_plugins.zsh
  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
    # the files can't have the same name
    print -ru2 -- "antidote: bundle file not found '$bundlefile'."
    return 1
  elif [[ "$bundlefile" == "$staticfile" ]]; then
    # the files can't have the same name
    print -ru2 -- "antidote: bundle file and static file are the same '$bundlefile'."
    return 1
  fi

  # regenerate the static file based on whether the bundle file is newer and whether
  # antidote home exists and is ready to be loaded
  local force_bundle=0
  if ! zstyle -t ':antidote:load:checkfile' disabled; then
    local loadable_check_path="$(antidote-home)/.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
    antidote bundle <"$bundlefile" >|"$staticfile"
    if [[ -r "${staticfile}.zwc" ]] && zstyle -T ':antidote:static' zcompile; then
      command rm -f -- "${staticfile}.zwc"
    fi
  fi
  source "$staticfile"
#}
