__mx_interface_mgmt() {
	local cur peripheral names operation

	cur=${COMP_WORDS[COMP_CWORD]}

	# 1st param (peripheral type)
	if [ ${COMP_CWORD} -eq 1 ]; then
		COMPREPLY=( $(compgen -W "deviceinfo console led serialport ethernet socket wifi cellular disk partition button buzzer dio can" -- ${cur}) )
	# 2nd param (peripheral name)
	elif [ ${COMP_CWORD} -eq 2 ]; then
		peripheral=${COMP_WORDS[COMP_CWORD-1]}
		names="$(mx-interface-mgmt ${peripheral} | cut -d " " -f 1 | tail -n +2)"
		COMPREPLY=( $(compgen -W "${names}" -- ${cur}) )
	# 3rd param (operation)
	elif [ ${COMP_CWORD} -eq 3 ]; then
		peripheral=${COMP_WORDS[COMP_CWORD-2]}
		case "${peripheral}" in
		"led")
			COMPREPLY=( $(compgen -W "get_state set_state" -- ${cur}) )
			;;
		"serialport")
			COMPREPLY=( $(compgen -W "get_interface set_interface set_io_state set_pull_up_down set_terminator" -- ${cur}) )
			;;
		"socket")
			COMPREPLY=( $(compgen -W "get_power set_power get_sim_slot set_sim_slot" -- ${cur}) )
			;;
		"wifi")
			COMPREPLY=( $(compgen -W "get_power set_power" -- ${cur}) )
			;;
		"cellular")
			COMPREPLY=( $(compgen -W "get_power set_power get_sim_slot set_sim_slot" -- ${cur}) )
			;;
		"disk")
			COMPREPLY=( $(compgen -W "set_automount set_io_state" -- ${cur}) )
			;;
		"partition")
			COMPREPLY=( $(compgen -W "mount unmount initialize_luks remap_luks" -- ${cur}) )
			;;
		"button")
			COMPREPLY=( $(compgen -W "get_action set_action" -- ${cur}) )
			;;
		"buzzer")
			COMPREPLY=( $(compgen -W "get_state set_state" -- ${cur}) )
			;;
		"dio")
			name=${COMP_WORDS[COMP_CWORD-1]}
			case "${name}" in
			DIO*)
				COMPREPLY=( $(compgen -W "get_state set_state get_event set_event get_direction set_direction reload" -- ${cur}) )
				;;
			*)
				COMPREPLY=( $(compgen -W "get_state set_state get_event set_event reload" -- ${cur}) )
				;;
			esac
			;;
		"can")
			COMPREPLY=( $(compgen -W "get_bitrate set_io_state set_terminator" -- ${cur}) )
			;;
		"ethernet")
			COMPREPLY=( $(compgen -W "set_io_state" -- ${cur}) )
			;;
		"console")
			COMPREPLY=( $(compgen -W "set_io_state" -- ${cur}) )
			;;
		*)
			;;
		esac
	# 4th param (operation argument)
	elif [ ${COMP_CWORD} -eq 4 ]; then
		peripheral=${COMP_WORDS[COMP_CWORD-3]}
		operation=${COMP_WORDS[COMP_CWORD-1]}
		case "${operation}" in
		"set_state")
			case "${peripheral}" in
			"led")
				COMPREPLY=( $(compgen -W "on off heartbeat" -- $cur) )
				;;
			"buzzer")
				COMPREPLY=( $(compgen -W "on off" -- $cur) )
				;;
			"dio")
				COMPREPLY=( $(compgen -W "high low" -- $cur) )
				;;
			esac
			;;
		"set_action")
			COMPREPLY=( $(compgen -W "disabled default user-defined" -- $cur) )
			;;
		"set_event")
			COMPREPLY=( $(compgen -W "none falling rising change" -- $cur) )
			;;
		"set_interface")
			COMPREPLY=( $(compgen -W "RS-232 RS-485-2W RS-422 RS-485-4W" -- $cur) )
			;;
		"set_power")
			COMPREPLY=( $(compgen -W "on off" -- $cur) )
			;;
		"set_sim_slot")
			COMPREPLY=( $(compgen -W "1 2" -- $cur) )
			;;
		"set_automount")
			COMPREPLY=( $(compgen -W "true false" -- $cur) )
			;;
		"set_io_state")
			COMPREPLY=( $(compgen -W "enabled disabled" -- $cur) )
			;;
		"set_terminator")
			COMPREPLY=( $(compgen -W "enabled disabled" -- $cur) )
			;;
		"set_pull_up_down")
			COMPREPLY=( $(compgen -W "enabled disabled" -- $cur) )
			;;
		"set_direction")
			COMPREPLY=( $(compgen -W "input output" -- $cur) )
			;;
		*)
			;;
		esac
	fi
	return 0
}

complete -F __mx_interface_mgmt mx-interface-mgmt
