# bash completion for opensim script

echo ":$PATH:" | grep -q ":/opt/opensim/bin:" || export PATH="$PATH:/opt/opensim/bin"

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

# The instances: the enabled ones (the links of robust.d and opensim.d), or with "all" the grids and
# simulators the setup made, enabled or not. Names as the launcher knows them: lower case.
_opensim_instances()
{
  local etc file
  for etc in $(_opensim_etc); do
    if [ "$1" = all ]; then
      for file in "$etc"/grids/*/Robust*.ini "$etc"/grids/*/sims/*.ini; do
        [ -f "$file" ] || continue
        case $file in
          */sims/*) basename "$file" .ini ;;
          *) basename "$(dirname "$file")" ;;
        esac
      done
    else
      for file in "$etc"/*.d/*.ini; do
        [ -e "$file" ] && basename "$file" .ini
      done
    fi
  done | tr '[:upper:]' '[:lower:]' | sort -u
}

# The grids the setup made, by nick
_opensim_grids()
{
  local etc dir
  for etc in $(_opensim_etc); do
    for dir in "$etc"/grids/*/; do
      [ -d "$dir" ] && basename "$dir"
    done
  done | tr '[:upper:]' '[:lower:]' | sort -u
}

_opensim()
{
  local cur
  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  case $COMP_CWORD in
    1)
      COMPREPLY=( $(compgen -W "start stop restart status setup enable disable console screen command rest ports next web import save load backup install-dotnet help" -- "$cur") )
      ;;
    *)
      case ${COMP_WORDS[1]} in
        start)
          COMPREPLY=( $(compgen -W "$(_opensim_instances)" -- "$cur") )
          ;;
        stop|restart)
          COMPREPLY=( $(compgen -W "$([ "$COMP_CWORD" = 2 ] && echo now) $(_opensim_instances)" -- "$cur") )
          ;;
        enable|disable)
          COMPREPLY=( $(compgen -W "$(_opensim_instances all)" -- "$cur") )
          ;;
        console|screen|command|ports)
          [ "$COMP_CWORD" = 2 ] || [ "${COMP_WORDS[1]}" = ports ] &&
            COMPREPLY=( $(compgen -W "$([ "${COMP_WORDS[1]}" = ports ] && echo '--publish --ufw') $(_opensim_instances)" -- "$cur") )
          ;;
        setup)
          COMPREPLY=( $(compgen -f -W "--file --check --result" -- "$cur") )
          ;;
        next)
          case $COMP_CWORD in
            2) COMPREPLY=( $(compgen -W "port $(_opensim_grids)" -- "$cur") ) ;;
            3) [ "${COMP_WORDS[2]}" != port ] && COMPREPLY=( $(compgen -W "location" -- "$cur") ) ;;
          esac
          ;;
        import)
          # opensim import [GRID] FILE [--grid] [--simulator] [--users] [--apply]
          if [ "$COMP_CWORD" = 2 ]; then
            COMPREPLY=( $(compgen -f -W "$(_opensim_grids)" -- "$cur") )
          else
            COMPREPLY=( $(compgen -f -W "--grid --simulator --users --apply --core --name --keep-going --result --format" -- "$cur") )
          fi
          ;;
        save|load)
          # opensim save|load iar|oar [GRID [SIM|REGION]] [options] ...
          if [ "$COMP_CWORD" = 2 ]; then
            COMPREPLY=( $(compgen -W "iar oar" -- "$cur") )
          elif [ "${COMP_WORDS[2]}" = iar ] && [ "${COMP_WORDS[1]}" = save ]; then
            COMPREPLY=( $(compgen -f -W "--home --verbose --noassets --perm= --skipbadassets --creators --exclude --excludefolder --keep $([ "$COMP_CWORD" = 3 ] && _opensim_grids)" -- "$cur") )
          elif [ "${COMP_WORDS[2]}" = iar ]; then
            COMPREPLY=( $(compgen -f -W "--merge $([ "$COMP_CWORD" = 3 ] && _opensim_grids)" -- "$cur") )
          elif [ "${COMP_WORDS[1]}" = save ]; then
            COMPREPLY=( $(compgen -f -W "--region --all --noassets --home --publish --perm= --keep $(_opensim_grids) $(_opensim_instances all)" -- "$cur") )
          else
            COMPREPLY=( $(compgen -f -W "--region --merge --skip-assets --force-terrain --force-parcels --no-objects --displacement --rotation --default-user $(_opensim_grids) $(_opensim_instances all)" -- "$cur") )
          fi
          ;;
        backup)
          # opensim backup [GRID [SIM]] [--logs] [--archives] [--output DIR] [--keep N]
          if [ "${COMP_WORDS[COMP_CWORD-1]}" = --output ]; then
            COMPREPLY=( $(compgen -d -- "$cur") )
          else
            COMPREPLY=( $(compgen -W "--logs --archives --output --keep $(_opensim_grids) $(_opensim_instances all)" -- "$cur") )
          fi
          ;;
        web)
          # opensim web [GRID] [show|check|snippet SERVER]
          local i=2
          case ${COMP_WORDS[2]} in show|check|snippet) ;; *) [ "$COMP_CWORD" -gt 2 ] && i=3 ;; esac
          if [ "$COMP_CWORD" = 2 ]; then
            COMPREPLY=( $(compgen -W "show snippet check $(_opensim_grids)" -- "$cur") )
          elif [ "$COMP_CWORD" = "$i" ]; then
            COMPREPLY=( $(compgen -W "show snippet check" -- "$cur") )
          elif [ "$COMP_CWORD" = $((i + 1)) ] && [ "${COMP_WORDS[i]}" = snippet ]; then
            COMPREPLY=( $(compgen -W "caddy nginx apache" -- "$cur") )
          fi
          ;;
      esac
      ;;
  esac
}
complete -F _opensim opensim
