# bash completion for beegfs-ctl

_beegfs-ctl()
{
   local cur cmd prev opts
   COMPREPLY=()
   _get_comp_words_by_ref -n = cur prev

   # commands for beegfs-ctl
   commands="--help --listnodes --listtargets --removenode --maptarget --removetarget \
      --getentryinfo --setpattern --mirrormd --find --refreshentryinfo --createfile --createdir \
      --migrate --disposeunused --serverstats --clientstats --userstats --storagebench --getquota \
      --setquota --addmirrorgroup --listmirrorgroups --startresync --resyncstats --liststoragepools \
      --addstoragepool --removestoragepool --modifystoragepool"
   # options in every command
   mclh="--mount= --cfgFile= --logEnabled --help"

   if [[ $COMP_CWORD -eq 1 ]] ; then
      # completion for the command
      COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
   else
      if [[ "$cur" == *=* ]]; then
         prev=${cur/=*/}
         cur=${cur/*=/}
      fi

      cmd=${COMP_WORDS[1]}
      if [[ "$cur" == -* ]]; then
         # completion for the options with --
         case $cmd in
            --listnodes)
               opts="$mclh --nodetype= --details --nicdetails --reachable \
                  --reachretries= --reachtimeout= --route --errorcodes"
               ;;

            --listtargets)
               opts="$mclh --nodesfirst --longnodes --state --pools --nodetype= --spaceinfo \
                  --mirrorgroups --errorcodes --storagepools --storagepoolid="
               ;;

            --removenode)
               opts="$mclh --nodetype= --unreachable --reachretries= --reachtimeout= --force"
               ;;

            --maptarget)
               opts="$mclh --targetid= --nodeid="
               ;;

            --removetarget)
               opts="$mclh --force"
               ;;

            --mirrormd)
               opts="$mclh --force"
               ;;

            --disposeunused)
               opts="$mclh --printnodes --printfiles --printstats --dispose"
               ;;

            --serverstats)
               opts="$mclh --nodetype= --history= --interval= --perserver --names"
               ;;

            --clientstats| --userstats)
               opts="$mclh --nodetype= --interval= --maxlines= --allstats --rwunit= \
                  --perinterval --names --filter="
               ;;

            --storagebench)
               opts="$mclh --alltargets --targetids= --servers= --write --read --stop \
                  --status --cleanup --blocksize= --size= --threads= --verbose --wait"
               ;;

            --getquota)
               opts="$mclh --uid --gid --defaultlimits --all --list --range --withzero \
                  --withsystem --default --csv --storagepoolid="
               ;;

            --getentryinfo)
               opts="$mclh --unmounted --verbose --nomappings"
               ;;

            --setpattern)
               opts="$mclh --chunksize= --numtargets= --pattern= --unmounted --force --storagepoolid="
               ;;

            --find)
               opts="$mclh --targetid= --nodeid= --nodetype= --nomirrors"
               ;;

            --refreshentryinfo)
               opts="$mclh --unmounted"
               ;;

            --createfile)
               opts="$mclh --chunksize= --numtargets= --targets= --access= --uid= --gid="
               ;;

            --createdir)
               opts="$mclh --nodes= --access= --uid= --gid= --nomirror"
               ;;

            --migrate)
               opts="$mclh --targetid= --nodeid= --storagepoolid= --destinationpoolid \
                  --nomirrors --verbose"
               ;;

            --setquota)
               opts="$mclh --uid --gid --all --list --range --file= --withzero --sizelimit= \
                  --inodelimit= --default --storagepoolid="
               ;;

            --addmirrorgroup)
               opts="$mclh --primary= --secondary= --nodetype= --groupid= --mirrorgroupid= \
                  --automatic --force --dryrun --uniquegroupid"
               ;;

            --listmirrorgroups)
               opts="$mclh --nodetype="
               ;;

            --startresync)
               opts="$mclh --targetid= --mirrorgroupid= --timestamp= --timespan= --nodetype= \
                  --nodeid= --restart"
               ;;

            --resyncstats)
               opts="$mclh --targetid= --nodeid= --mirrorgroupid= --nodetype="
               ;;

            --addstoragepool)
               opts="$mclh --desc= --targets= --mirrorgroups"
               ;;
            
            --removestoragepool)
               opts="$mclh"
               ;;
            
            --modifystoragepool)
               opts="$mclh --id= --desc= --addtargets= --rmtargets= --addmirrorgroups --rmmirrorgroups"
               ;;
          esac

          COMPREPLY=( $(compgen -W "$opts" -- "$cur" ) )
          # no space after =
          [[ $COMPREPLY == *= ]] && compopt -o nospace

      else
         case $cmd in
         # completions for files and suboptions

            --listnodes)
               case $prev in
                  --nodetype)
                     COMPREPLY=( $(compgen -W "management metadata storage client" \
                        -- "$cur" ) )
                     ;;
                  --cfgFile| --mount)
                     _filedir
                     ;;
               esac
               ;;

            --removenode)
               case $prev in
                  --nodetype)
                     COMPREPLY=( $(compgen -W "metadata storage client" -- "$cur" ) )
                     ;;
                  --cfgFile| --mount)
                     _filedir
                     ;;
               esac
               ;;

            --listtargets|--startresync|--resyncstats|--addmirrorgroup|--serverstats)
               case $prev in
                  --nodetype)
                     COMPREPLY=( $(compgen -W "metadata storage" -- "$cur" ) )
                     ;;
                  --cfgFile| --mount)
                     _filedir
                     ;;
               esac
               ;;

            --clientstats)
               case $prev in
                  --nodetype)
                     COMPREPLY=( $(compgen -W "metadata storage" -- "$cur" ) )
                     ;;
                  --cfgFile| --mount)
                     _filedir
                     ;;
                  --filter)
                     if [[ "$COMP_LINE" == *--names* ]]; then
                        COMPREPLY=( $(compgen -A hostname "$cur" ) )
                     fi
                     ;;
               esac
               ;;

            --userstats)
               case $prev in
                  --nodetype)
                     COMPREPLY=( $(compgen -W "metadata storage" -- "$cur" ) )
                     ;;
                  --cfgFile| --mount)
                     _filedir
                     ;;
                  --filter)
                     if [[ "$COMP_LINE" == *--names* ]]; then
                        COMPREPLY=( $(compgen -u "$cur" ) )
                     fi
                     ;;
               esac
               ;;

            --find)
               _filedir
               case $prev in
                  --nodetype)
                     COMPREPLY=( $(compgen -W "metadata storage" -- "$cur" ) )
                     ;;
                  --cfgFile| --mount)
                     _filedir
                     ;;
               esac
               ;;

            --getentryinfo| --setpattern| --mirrormd| --refreshentryinfo| --createfile| \
            --createdir| --migrate)
               _filedir
               case $prev in
                  --cfgFile| --mount)
                     _filedir
                     ;;
               esac
               ;;

            --getquota| --maptarget| --removetarget| --disposeunused| --storagebench)
               case $prev in
                  --cfgFile| --mount)
                     _filedir
                     ;;
               esac
               ;;

            --setquota)
               case $prev in
                  --cfgFile| --mount| --file)
                     _filedir
                     ;;
               esac
               ;;
         esac
      fi
   fi

   return 0
}
complete -F _beegfs-ctl beegfs-ctl
