# Copyright (c) 2012-2017 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0.
#
# SPDX-License-Identifier: EPL-2.0

_golo ()
{
	local cur prev special i

	COMPREPLY=()
	_get_comp_words_by_ref cur prev

	# Find out if we have a special word and our previous flag.
	for (( i=1; i < ${#COMP_WORDS[@]}-1; i++ )); do
		if [[ ${COMP_WORDS[i]} == @(compile|check|run|golo|diagnose|doc|version|new) ]]; then
			special=${COMP_WORDS[i]}
		fi
		if [[ ${COMP_WORDS[i]} == -* ]]; then
			lastflag=${COMP_WORDS[i]}
		fi
	done

	# We know the special and that we're not trying to use a flag.
	# We probably want to show files.
	if [[ $cur != -* && ! -z ${special} ]]; then
		case "${lastflag}" in
			--files|--classpath|--output|--path)
				_filedir
				return 0
				;;
		esac
	fi

	# Our special wasn't defined. We need to start there.
	if [ -z ${special} ]; then
		COMPREPLY=( $( compgen -W 'version compile check run golo new doc diagnose --help' -- "$cur" ) )
		return 0
	fi

	# Match against flags with specific options.
	case "$prev" in
		--format)
			COMPREPLY=( $( compgen -W 'html markdown ctags' -- "$cur" ) )
			return 0
			;;
		--tool)
			COMPREPLY=( $( compgen -W 'ast ir' -- "$cur" ) )
			return 0
			;;
    --stage)
			COMPREPLY=( $( compgen -W 'ast raw refined' -- "$cur" ) )
			return 0
			;;
		--type)
			COMPREPLY=( $( compgen -W 'maven gradle simple' -- "$cur" ) )
			return 0
			;;
    --profile)
      COMPREPLY=( $( compgen -W 'app lib' -- "$cur" ) )
      return 0
      ;;
    --vcs)
      COMPREPLY=( $( compgen -W 'none git hg' -- "$cur" ) )
      return 0
      ;;
	esac

	# We know a special, but we're looking to learn the flags.
	case "${special}" in
		compile)
			COMPREPLY=( $( compgen -W '--output' -- "$cur" ) )
			;;
		check)
			COMPREPLY=( $( compgen -W '--exit --verbose' -- "$cur" ) )
			;;
		version)
			COMPREPLY=( $( compgen -W '--full' -- "$cur" ) )
			;;
		run)
			COMPREPLY=( $( compgen -W '--module --classpath' -- "$cur" ) )
			;;
		golo)
			COMPREPLY=( $( compgen -W '--files --args --classpath --module' -- "$cur" ) )
			;;
		diagnose)
			COMPREPLY=( $( compgen -W '--tool --stage' -- "$cur" ) )
			;;
		new)
			COMPREPLY=( $( compgen -W '--type --path --profile --vcs' -- "$cur" ) )
			;;
		doc)
			COMPREPLY=( $( compgen -W '--format --output' -- "$cur" ) )
			;;
	esac
	return 0
}

complete -F _golo golo
complete -F _golo vanilla-golo
