#!/bin/bash

__contains_word () {
		local w word=$1; shift
		for w in "$@"; do
				[[ $w = "$word" ]] && return
		done
}

_voxl_construct_sku(){

	source /home/root/.profile.d/modalai_sku_definitions.sh

	local OPTS=('--board --cam --debug --family --help --quiet --version --wizard')
	local OPTS_SHORT=('-b -c -d -f -h -q -v -w')

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

	#Check to see if the previous command was a valid OPT
	if  __contains_word "${COMP_WORDS[COMP_CWORD-1]}" $OPTS  ||
		__contains_word "${COMP_WORDS[COMP_CWORD-1]}" $OPTS_SHORT ; then

		case "${COMP_WORDS[COMP_CWORD-1]}" in
			"-f"|"--family") # family name
				COMPREPLY=( $(compgen -W '${VOXL_FAMILY_NAMES[@]} ${VOXL_FAMILY_CODES[@]}' -- ${COMP_WORDS[COMP_CWORD]}) )
				return 0
				;;
			"-b"|"--board") # compute board number
				COMPREPLY=( $(compgen -W '${VOXL_BOARD_NAMES[@]}' -- ${COMP_WORDS[COMP_CWORD]}) )
				return 0
				;;
			*)
				COMPREPLY=( $(compgen -W '${OPTS}' -- ${COMP_WORDS[COMP_CWORD]}) )
				return 0
				;;
		esac
	else
		COMPREPLY=( $(compgen -W '${OPTS}' -- ${COMP_WORDS[COMP_CWORD]}) )
		return 0
	fi


}

complete -F _voxl_construct_sku voxl-configure-sku
