#!/bin/bash
#
# tab completion for voxl-logger-auto and voxl-logger-auto-configure
#
#   voxl-logger-auto <TAB>                 start stop status arm-trigger configure
#   voxl-logger-auto arm-trigger <TAB>     on off
#   voxl-logger-auto configure <TAB>       show pipes trigger add rm set preset reset
#   ... configure add <TAB>                pipes live in /run/mpa
#   ... configure rm <TAB>                 pipes in the current config
#   ... configure trigger <TAB>            manual armed
#   ... configure set <TAB>                base_dir max_duration_s control_pipe_name
#   ... configure preset <TAB>             vio

_VLA_CONF="/etc/modalai/voxl-logger-auto.conf"
_VLA_SUBS="show pipes trigger add rm set preset reset"
_VLA_TYPES="imu cam vio qvio pose_6dof tof2 baro rc mav local_position_ned ext_vfc raw"

# pipes currently publishing in /run/mpa
_vla_live_pipes(){
	local d
	for d in /run/mpa/*/; do
		[ -e "${d}request" ] || [ -e "${d}info" ] || continue
		d="${d%/}"
		echo "${d##*/}"
	done
}

# pipe names in the config's "pipes" list
_vla_conf_pipes(){
	local conf="$1"
	[ -r "$conf" ] || conf="$_VLA_CONF"
	sed -n 's/.*"name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$conf" 2>/dev/null
}

# completion for the configure tool; $1 is the index of its first argument
_vla_configure(){
	local start=$1
	local cur="${COMP_WORDS[COMP_CWORD]}"
	local prev="${COMP_WORDS[COMP_CWORD-1]}"
	local conf="$_VLA_CONF"
	local sub="" subi=0 i w

	# find the subcommand, skipping the global options (-c takes a value)
	for (( i=start; i<COMP_CWORD; i++ )); do
		w="${COMP_WORDS[i]}"
		case "$w" in
			-c|--config) (( i++ )); conf="${COMP_WORDS[i]}" ;;
			-*) ;;
			*) sub="$w"; subi=$i; break ;;
		esac
	done

	if [ -z "$sub" ]; then
		case "$prev" in
			-c|--config) COMPREPLY=( $(compgen -f -- "$cur") ); return 0 ;;
		esac
		if [[ "$cur" == -* ]]; then
			COMPREPLY=( $(compgen -W "-c --config -n --no-apply -h --help" -- "$cur") )
		else
			COMPREPLY=( $(compgen -W "$_VLA_SUBS" -- "$cur") )
		fi
		return 0
	fi

	# position of the word being completed within the subcommand's arguments
	local argn=$(( COMP_CWORD - subi ))

	case "$sub" in
		pipes)
			COMPREPLY=( $(compgen -W "-a --all" -- "$cur") ) ;;
		trigger)
			[ $argn -eq 1 ] && COMPREPLY=( $(compgen -W "manual armed" -- "$cur") ) ;;
		add)
			case "$prev" in
				-t|--type) COMPREPLY=( $(compgen -W "$_VLA_TYPES" -- "$cur") ); return 0 ;;
				-s|--skip) return 0 ;;
			esac
			if [[ "$cur" == -* ]]; then
				COMPREPLY=( $(compgen -W "-t --type -s --skip" -- "$cur") )
			else
				COMPREPLY=( $(compgen -W "$(_vla_live_pipes)" -- "$cur") )
			fi ;;
		rm)
			# the config's pipes, minus the ones already named
			local have=" ${COMP_WORDS[*]:subi+1:argn-1} " left="" n
			for n in $(_vla_conf_pipes "$conf"); do
				[[ "$have" == *" $n "* ]] || left="$left $n"
			done
			COMPREPLY=( $(compgen -W "$left" -- "$cur") ) ;;
		set)
			if [ $argn -eq 1 ]; then
				COMPREPLY=( $(compgen -W "base_dir max_duration_s control_pipe_name" -- "$cur") )
			elif [ $argn -eq 2 ] && [ "$prev" = "base_dir" ]; then
				COMPREPLY=( $(compgen -d -- "$cur") )
			fi ;;
		preset)
			if [[ "$cur" == -* ]]; then
				COMPREPLY=( $(compgen -W "-r --replace" -- "$cur") )
			elif [ $argn -eq 1 ]; then
				COMPREPLY=( $(compgen -W "vio" -- "$cur") )
			fi ;;
	esac
	return 0
}

_voxl_logger_auto(){
	local cur="${COMP_WORDS[COMP_CWORD]}"
	COMPREPLY=()

	if [ "$COMP_CWORD" -eq 1 ]; then
		COMPREPLY=( $(compgen -W "start stop status arm-trigger configure" -- "$cur") )
		return 0
	fi

	case "${COMP_WORDS[1]}" in
		arm-trigger)
			[ "$COMP_CWORD" -eq 2 ] && COMPREPLY=( $(compgen -W "on off" -- "$cur") ) ;;
		configure|config)
			_vla_configure 2 ;;
	esac
	return 0
}

_voxl_logger_auto_configure(){
	COMPREPLY=()
	_vla_configure 1
}

complete -o filenames -F _voxl_logger_auto voxl-logger-auto
complete -o filenames -F _voxl_logger_auto_configure voxl-logger-auto-configure
