_quadletctl() 
{
	local cur cmds cur_cmds user_args opts unit_dirs
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	cmds=(enable disable status)
	opts=(${cmds[@]} --user --help -h)

	if [ $COMP_CWORD -gt 1 ]; then
		opts=($(LC_ALL=C comm -13 <(printf '%s\n' "${COMP_WORDS[@]}" | LC_ALL=C sort) <(printf '%s\n' "${opts[@]}" | LC_ALL=C sort)))
		cur_cmds=($(LC_ALL=C comm -12 <(printf '%s\n' "${cmds[@]}" | LC_ALL=C sort) <(printf '%s\n' "${COMP_WORDS[@]}" | LC_ALL=C sort)))
		if [ ${#cur_cmds[@]} -ge 1 ]; then
			opts=($(LC_ALL=C comm -13 <(printf '%s\n' "${cmds[@]}" | LC_ALL=C sort) <(printf '%s\n' "${opts[@]}" | LC_ALL=C sort)))
			user_args=($(LC_ALL=C comm -12 <(printf '%s\n' "--user" | LC_ALL=C sort) <(printf '%s\n' "${COMP_WORDS[@]}" | LC_ALL=C sort)))
			[ ${#user_args[@]} -ge 1 ] \
				&& unit_dirs="${XDG_RUNTIME_DIR:-/run/user/$UID}/containers/systemd;${XDG_CONFIG_HOME:-$HOME/.config/containers/systemd;/etc/containers/systemd/users/$UID;/etc/containers/systemd/users}" \
				|| unit_dirs="/run/containers/systemd;/etc/containers/systemd;/usr/share/containers/systemd"
			for DIR in ${unit_dirs//;/ }; do
				for UNIT in $DIR/*; do
					[ -f $UNIT ] && opts+=($(basename $UNIT))
				done
			done
		fi
	fi

	COMPREPLY=( $(compgen -W "${opts[*]}" -- ${cur}) )
	return 0
}

complete -F _quadletctl quadletctl
