# bash completion for the opensim command

# The etc folders to look in (OPENSIM_ETC_DIRS overrides them)
_opensim_etc()
{
  echo ${OPENSIM_ETC_DIRS:-/etc/opensim}
}

# The enabled instances (the links of robust.d and opensim.d), in lower case
_opensim_instances()
{
  local etc file
  for etc in $(_opensim_etc); do
    for file in "$etc"/*.d/*.ini; do
      [ -e "$file" ] && basename "$file" .ini
    done
  done | tr '[:upper:]' '[:lower:]' | sort -u
}

# Every instance, the disabled ones too (the configs of the grids and of their simulators)
_opensim_all_instances()
{
  local etc dir file
  for etc in $(_opensim_etc); do
    for dir in "$etc"/grids/*/; do
      [ -d "$dir" ] || continue
      basename "$dir"
      for file in "$dir"sims/*.ini; do
        [ -e "$file" ] && basename "$file" .ini
      done
    done
  done | tr '[:upper:]' '[:lower:]' | sort -u
}

# The nicks of the grids of this machine
_opensim_grids()
{
  local etc dir
  for etc in $(_opensim_etc); do
    for dir in "$etc"/grids/*/; do
      [ -d "$dir" ] && basename "$dir"
    done
  done | sort -u
}

# What follows an option that takes a file: the files
_opensim_wants_file()
{
  case $1 in
    --accounts|--robust|--simulator|--grid-settings|--result|--config|--prepare|--apply|--region-config|--output) return 0 ;;
  esac
  return 1
}

_opensim()
{
  local cur prev
  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}
  if [ "$COMP_CWORD" = 1 ]; then
    COMPREPLY=( $(compgen -W "service setup import region simulator settings address posts status ports start stop restart console screen enable disable web next ban save load backup restore oar api api-key rest install-dotnet module --version" -- "$cur") )
    return
  fi
  if _opensim_wants_file "$prev"; then
    COMPREPLY=( $(compgen -f -- "$cur") )
    return
  fi
  case ${COMP_WORDS[1]} in
    --version)
      COMPREPLY=( $(compgen -W "-v" -- "$cur") )
      ;;
    setup)
      COMPREPLY=( $(compgen -W "--config --prepare --apply --region-config --existing --oar --dump --show-passwords --help" -- "$cur") )
      ;;
    import)
      COMPREPLY=( $(compgen -W "$(_opensim_grids) --accounts --robust --simulator --grid-settings --name --core --nick --grid-name --site --no-scripts --format --keep-going --result --switch --in-place --forget --copy-data --apply" -- "$cur") )
      ;;
    region|simulator)
      if [ "$COMP_CWORD" = 2 ]; then
        if [ "${COMP_WORDS[1]}" = region ]; then
          COMPREPLY=( $(compgen -W "add set remove" -- "$cur") )
        else
          COMPREPLY=( $(compgen -W "add set" -- "$cur") )
        fi
      else
        COMPREPLY=( $(compgen -W "$(_opensim_grids) --apply" -- "$cur") )
      fi
      ;;
    address)
      [ "$COMP_CWORD" = 2 ] && COMPREPLY=( $(compgen -W "$(_opensim_grids)" -- "$cur") )
      [ "$COMP_CWORD" -gt 2 ] && COMPREPLY=( $(compgen -W "--host --port --site --apply" -- "$cur") )
      ;;
    settings|posts)
      [ "$COMP_CWORD" = 2 ] && COMPREPLY=( $(compgen -W "$(_opensim_grids)" -- "$cur") )
      [ "$COMP_CWORD" -gt 2 ] && COMPREPLY=( $(compgen -W "--set delete" -- "$cur") )
      ;;
    ports)
      COMPREPLY=( $(compgen -W "$(_opensim_instances) --publish --ufw --private-address --from --manage-from" -- "$cur") )
      ;;
    start)
      COMPREPLY=( $(compgen -W "$(_opensim_instances) --robust" -- "$cur") )
      ;;
    stop|restart)
      COMPREPLY=( $(compgen -W "$([ "$COMP_CWORD" = 2 ] && echo now) $(_opensim_instances) --robust" -- "$cur") )
      ;;
    console|screen)
      [ "$COMP_CWORD" = 2 ] && COMPREPLY=( $(compgen -W "$(_opensim_instances)" -- "$cur") )
      ;;
    enable|disable)
      case $prev in
        economy) COMPREPLY=( $(compgen -W "beta gloebit" -- "$cur") ) ;;
        offline) COMPREPLY=( $(compgen -W "helpers robust" -- "$cur") ) ;;
        *) COMPREPLY=( $(compgen -W "$(_opensim_all_instances) search economy profiles offline ossl-extended map-warp3d" -- "$cur") ) ;;
      esac
      ;;
    web)
      COMPREPLY=( $(compgen -W "$(_opensim_grids) show check snippet set api caddy nginx apache --grid --docroot --socket --port --web-group --install site --edit-vhosts --vhosts" -- "$cur") )
      ;;
    next)
      if [ "$COMP_CWORD" = 2 ]; then
        COMPREPLY=( $(compgen -W "port $(_opensim_grids)" -- "$cur") )
      elif [ "$COMP_CWORD" = 3 ] && [ "$prev" != port ]; then
        COMPREPLY=( $(compgen -W "location" -- "$cur") )
      else
        COMPREPLY=( $(compgen -W "-e" -- "$cur") )
      fi
      ;;
    ban)
      COMPREPLY=( $(compgen -W "$(_opensim_grids) --output" -- "$cur") )
      ;;
    save)
      COMPREPLY=( $(compgen -W "iar oar $(_opensim_instances) --keep --home --noassets --perm --skipbadassets --region --all --publish" -- "$cur") )
      ;;
    load)
      COMPREPLY=( $(compgen -W "iar oar $(_opensim_instances) --merge --skip-assets --force-terrain --force-parcels --no-objects --displacement --rotation --default-user" -- "$cur") )
      ;;
    backup)
      COMPREPLY=( $(compgen -W "$(_opensim_grids) --logs --archives --oars --output --keep --simulator" -- "$cur") )
      ;;
    oar)
      if [ "$COMP_CWORD" = 2 ]; then
        COMPREPLY=( $(compgen -W "pack info check unpack" -- "$cur") )
      else
        COMPREPLY=( $(compgen -f -- "$cur") )
      fi
      ;;
    rest)
      COMPREPLY=( $(compgen -W "$(_opensim_instances) --url --user --repl" -- "$cur") )
      ;;
    api)
      COMPREPLY=( $(compgen -W "GET POST PATCH DELETE --data --grid" -- "$cur") )
      ;;
    api-key)
      COMPREPLY=( $(compgen -W "create list revoke" -- "$cur") )
      ;;
    restore)
      COMPREPLY=( $(compgen -f -X '!*.tar.gz' -- "$cur") $(compgen -W "--apply" -- "$cur") )
      ;;
  esac
}

complete -F _opensim opensim
