#/usr/bin/env bash

_rsm_completions() {
	SVDIR='/etc/runit/sv'
	RUNSVDIR='/etc/runit/runsvdir/default'

	if [ "${#COMP_WORDS[@]}" -eq 3 ]
	then
		cmd="${COMP_WORDS[1]}"
		# notify-send "$cmd"
		case "$cmd" in
			status|enable|logs|alllogs|errorlogs)
				COMPREPLY=($(compgen -W "$(ls -1 $SVDIR | xargs echo)" \
					"${COMP_WORDS[2]}"))
				;;
			disable|start|stop|restart|reload)
				COMPREPLY=($(compgen -W "$(ls -1 $RUNSVDIR | xargs echo)" \
					"${COMP_WORDS[2]}"))
				;;

		esac
	elif [ "${#COMP_WORDS[@]}" -eq 2 ]
	then
		COMPREPLY=($(compgen -W "status enable disable start \
			stop restart reload logs alllogs errorlogs" "${COMP_WORDS[1]}"))
	else
		COMPREPLY=('wtf')
	fi

	return 0
}

complete -F _rsm_completions rsm
