#compdef git-lb-use

###############################################################################
# (c) Copyright 2018 CERN                                                     #
#                                                                             #
# This software is distributed under the terms of the GNU General Public      #
# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING".   #
#                                                                             #
# In applying this licence, CERN does not waive the privileges and immunities #
# granted to it by virtue of its status as an Intergovernmental Organization  #
# or submit itself to any jurisdiction.                                       #
###############################################################################

__lb_use_projects () {
  local -a projects
  projects=($(lb-completion-helper list_projects 2> /dev/null))
  if [[ $? -ne 0 ]]; then
    _message "could not determine list of projects, falling back to hard coded list"
    projects=(Gaudi LHCb Lbcom Rec Boole Brunel Gauss Phys Analysis Hlt Alignment Moore Online Euler Geant4 DaVinci Bender Orwell Panoramix LbScripts Dirac LHCbGrid Panoptes Curie Vetra VetraTB Compat VanDerMeer Ganga LHCbDirac Integration Erasmus Feicim Stripping LHCbExternals Urania VMDirac LHCbVMDirac Noether Tesla MooreOnline BeautyDirac Kepler AlignmentOnline Lovell L0MuonTools Calibration)
  fi
  _values 'projects' "${projects[@]}"
}

__lb_use_protocol () {
  local -a protocols
  protocols=(krb5 https ssh)
  _values 'PROTOCOL' "${protocols[@]}"
}


_cern_gitlab_urls () {
  local -a cernurls
  cernurls=('ssh://git@gitlab.cern.ch:7999/' 'https://:@gitlab.cern.ch:8443/' 'https://gitlab.cern.ch/')
  _wanted arguments expl "CERN gitlab urls" compadd -S '' $cernurls
}

__cern_or_internet_urls () {
  local alternatives
  alternatives=('cern:CERN gitlab urls:_cern_gitlab_urls')
  alternatives+=('internet:standard urls:_urls')
  _alternative "${alternatives[@]}"
}

local -a arguments
arguments=(
    '(-v --verbose -q --quiet -d --debug)'{-d,--debug}'[be very verbose]'
    '(-v --verbose -q --quiet -d --debug)'{-v,--verbose}'[be more verbose]'
    '(-q --quiet -v --verbose -d --debug)'{-q,--quiet}'[be more quiet]'
    '(: -)--version[display version information]'
    '(: -)--help[display help message]'
    '(-p --protocol)'{-p,--protocol=}'[specify connection protocol]:PROTOCOL:__lb_use_protocol'
    '(-)1: :__lb_use_projects'
    '2:[project url (if default is not desired)]:__cern_or_internet_urls'
)
_arguments -n $arguments
