#!/bin/zsh

### Update antidote and/or bundles.
#function antidote-update {
  0=${(%):-%x}

  if [[ "$1" == -h || "$1" == --help ]]; then
    antidote-help update
    return $?
  fi

  local o_self o_bundles o_dry_run
  zparseopts ${_adote_zparopt_flags} -- \
    s=o_self    -self=s    \
    b=o_bundles -bundles=b \
    n=o_dry_run -dry-run=n 2>/dev/null

  # bundle update via subprocess (pass through flags it understands)
  local -a o_passthru=( $o_dry_run )
  local ret=0
  if (( $#o_bundles )) || ! (( $#o_self )); then
    antidote-zsh update "${o_passthru[@]}"
    ret=$?
  fi

  # self-update in parent shell
  if (( $#o_self )) || ! (( $#o_bundles )); then
    local antidote_dir="${0:a:h:h}"
    if (( $#o_dry_run )); then
      print "antidote: skipping self-update (dry run)"
    elif [[ -d "${antidote_dir}/.git" ]]; then
      print "Updating antidote..."
      if git -C "$antidote_dir" pull --quiet --rebase 2>/dev/null; then
        # re-setup antidote to pick up new version
        (( $+functions[antidote-setup] )) && unfunction antidote-setup
        builtin autoload -Uz ${0:a:h}/antidote-setup
        antidote-setup
        print "antidote self-update complete."
        print ""
        antidote --version
      else
        print -ru2 "antidote: self-update failed."
        ret=1
      fi
    else
      print "Self updating is disabled in this build."
      print "Use your OS package manager to update antidote itself."
    fi
  fi

  return $ret
#}
