# completion file for bash

__superctl_subcommands() {
	printf "start\nstop\nrestart\nenable\ndisable\nstatus\nset\nreload\n"
}

__superctl__services() {
	superctl status | while read -a lines; do
		if [ -z "$shallowstatus" ]; then
			shallowstatus=1
			continue
		fi
		printf "%s\n" "${lines[0]}"
	done
}

__superctl_completion()
{
	if [ "$COMP_CWORD" -eq 1 ]; then
		COMPREPLY=($(compgen -W "$(__superctl_subcommands)" -- "${COMP_WORDS[1]}"))
		return
	fi

	case "${COMP_WORDS[1]}" in
		status|start|stop|restart)
			if [ "$COMP_CWORD" -eq 2 ]; then
				COMPREPLY=($(compgen -W "$(__superctl__services)" -- "${COMP_WORDS[$COMP_CWORD]}"))
			fi
			;;
		enable|disable)
			if [ "$COMP_CWORD" -eq 2 ]; then
				COMPREPLY=($(compgen -W "$(__superctl__services) --now" -- "${COMP_WORDS[$COMP_CWORD]}"))
			elif [ "$COMP_CWORD" -eq 3 ]; then
				case  "${COMP_WORDS[2]}" in
					--now)
						COMPREPLY=($(compgen -W "$(__superctl__services)" -- "${COMP_WORDS[$COMP_CWORD]}"))
						;;
					*)
						COMPREPLY=("--now")
						;;
				esac
			fi
			;;
	esac
}

complete -F __superctl_completion superctl
