#!/bin/bash
################################################################################
# Copyright 2026 ModalAI Inc.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# 4. The Software is used solely in conjunction with devices provided by
#    ModalAI Inc.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
################################################################################

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


USER=$(whoami)

RESET_ALL="\e[0m"
RED="\e[91m"
YLW="\e[33m"
GRN="\e[32m"
SET_BOLD="\e[1m"

PRINT_ERROR (){
	echo -e "${RED}[ERROR] $@${RESET_ALL}"
}

PRINT_GREEEN_LINE (){
	echo -e "${GRN}${SET_BOLD}------------------------------------------------------------------${RESET_ALL}"
}

PRINT_DEBUG () {
	if $DEBUG; then
		echo "$@"
	fi
}


## local mode variables
DEBUG=false
NON_INTERACTIVE=false

# conf file paths
VOXL_PX4_CONF_FILE="/etc/modalai/voxl-px4.conf"

# set param in $VOXL_PX4_CONF_FILE
set_px4_conf_param () {
	if [ "$#" != "2" ]; then
		echo "set_px4_conf_param expected 2 args"
		exit 1
	fi

	var=$1
	val=$2

	sed -i "/$var=/c $var=$val" ${VOXL_PX4_CONF_FILE}
}

## set most parameters which don't have quotes in json
set_param () {
	if [ "$#" != "3" ]; then
		echo "set_param expected 3 args"
		exit 1
	fi

	CONFIG_FILE=$1
	# remove quotes if they exist
	var=$2
	var="${var%\"}"
	var="${var#\"}"
	val=$3
	val="${val%\"}"
	val="${val#\"}"

	sed -E -i "/\"$var\":/c\	\"$var\":	$val," ${CONFIG_FILE}
}

# Replaces the nth occurrence of a specified variable's value in a JSON-like config file.
set_nth_param() {
	if [ "$#" != "4" ]; then
		echo "set_nth_param expected 4 args"
		exit 1
	fi

	CONFIG_FILE=$1
	var=$2
	val=$3
	occurrence=$4

	# Replace only the nth occurrence of the variable
	sed -E -i ":a;N;\$!ba; s/(\"$var\"[[:space:]]*:[[:space:]]*)[0-9a-zA-Z\"._-]+(,)/\1$val\2/${occurrence}" "$CONFIG_FILE"
}




## set string parameters which need quotes in json
set_param_string () {
	if [ "$#" != "3" ]; then
		echo "set_param_string expected 3 args"
		exit 1
	fi
	CONFIG_FILE=$1
	var=$2
	var="${var%\"}"
	var="${var#\"}"
	sed -E -i "/\"$var\":/c\	\"$var\":	\"$3\"," ${CONFIG_FILE}
}

## Set the nth occurrence of a string parameter which needs quotes in a JSON-like config file
set_nth_param_string() {
	if [ "$#" != "4" ]; then
		echo "set_nth_param_string expected 4 args"
		exit 1
	fi

	CONFIG_FILE=$1
	var=$2
	val=$3
	occurrence=$4

	# Replace only the nth occurrence of the variable with a quoted value
	sed -E -i ":a;N;\$!ba; s/(\"$var\"[[:space:]]*:[[:space:]]*)\"[^\"]*\"(,)/\1\"$val\"\2/${occurrence}" "$CONFIG_FILE"
}



## set string parameters which need quotes in json
set_variable () {
	if [ "$#" != "3" ]; then
		echo "set_param_string expected 3 args"
		exit 1
	fi
	CONFIG_FILE=$1
	sed -i -E "s/$2/$3/g" ${CONFIG_FILE}
}

print_usage () {
	clear -x
	echo -e ""
	PRINT_GREEEN_LINE
	echo -e "                ${GRN}${SET_BOLD}Welcome to voxl-configure-mpa!${RESET_ALL}"
	echo -e ""
	echo -e "This tool reads the pre-configured product SKU from the disk"
	echo -e "resets all MPA services and their configuration files back to a"
	echo -e "repeatable factory configuration."
	echo -e "This is performed as the last step when flashing a VOXL compute"
	echo -e "board with a platform release."
	echo -e ""
	echo -e ""
	echo -e ""
	echo -e "${GRN}${SET_BOLD}Arguments:${RESET_ALL}"
	echo -e "-d, --debug    debug mode"
	echo -e "-h, --help     show this help text"
	echo -e "-n, --non-interactive  no questions, for scripted setup"
	echo -e ""
	PRINT_GREEEN_LINE
	exit 0
}

_ask_continue () {
	while true; do
		echo ""
		read -p "Continue? y/n: " input
		case $input in
			[yY]*)
				echo 'Continuing'
				break
				;;
			[nN]*)
				echo 'Ok, exiting'
				exit 0
				;;
			*)
				echo 'Invalid input' >&2
		esac
	done
}

_run_step () {

	echo -e ""
	echo -e "${GRN}executing: $cmd ${RESET_ALL}"

	executable_name=${cmd%% *}

	# check to see if cmd is a local bash function
	if ! declare -F $executable_name > /dev/null; then
		if ! [[ $(type -P "$executable_name") ]]; then
			echo -e "${RED}${SET_BOLD}System is missing $executable_name${RESET_ALL}"
			echo -e "${RED}${SET_BOLD}Likely it did not get installed or was removed${RESET_ALL}"
			HAD_FAILURE=true
			FAILURES+=("$cmd")
			return
		fi
	fi

	## now actually run the command
	$cmd
	status=$?
	if [ $status -eq 0 ]; then
		echo -e "${GRN}Success!${RESET_ALL}"
		return
	fi

	## if we got here, it failed on the first try. Wait and a try a second time
	echo -e "${YLW}first try failed, executing a second time: $cmd ${RESET_ALL}"
	sleep 1

	$cmd
	status=$?
	if [ $status -eq 0 ]; then
		echo -e "${GRN}Success running a second time${RESET_ALL}"
		HAD_SUCCESSFUL_RETRY=true
		SUCCESSFUL_RETRIES+=("$cmd")
		return
	fi

	echo -e "${RED}${SET_BOLD}FAILED TO EXECUTE: $cmd${RESET_ALL}"
	HAD_FAILURE=true
	FAILURES+=("$cmd")

	## special extra warning for the most common thing to randomly fail
	if [[ $cmd == *"voxl-configure-px4-params"* ]]; then
		echo -e "${RED}This is likely because voxl-px4 on the SDSP failed to restart.${RESET_ALL}"
		echo -e "${RED}Please try running voxl-configure-mpa again.${RESET_ALL}"
		echo -e "${RED}If that doesn't work, power cycle and try again.${RESET_ALL}"
	fi
}



_main(){

	while (( "$#" )); do
		case "$1" in

		"-d"|"--debug")
			echo "enabling debug mode"
			DEBUG=true
			;;

		"-h"|"--help")
			print_usage
			exit 0
			;;

		"-n"|"--non-interactive")
			echo "enabling non-interactive mode"
			NON_INTERACTIVE=true
			;;

		"-r"|"--reset"|"reset"|"factory"|"factory-reset"|"--factory-reset")
			echo "reset argument no longer needed"
			;;


		"-w"|"--wizard")
			echo "wizard argument no longer needed"
			;;

		*)
			echo "Invalid arg: $1, exiting"
			exit -1
			;;

		esac
		shift
	done


	PRINT_GREEEN_LINE
	echo -e "               ${GRN}${SET_BOLD}Welcome to voxl-configure-mpa!${RESET_ALL}"
	echo -e ""


	## do a quick parse, this will migrate old filename to new in case
	if ! voxl-inspect-sku --quiet; then
		exit 1
	fi

	## read from file
	VOXL_SKU=$( cat $SKU_FILENAME )
	if ! voxl-parse-and-export-sku-variables $VOXL_SKU; then
		exit 1
	fi

	## show the user what they have
	echo "VOXL is currently thinks it is in the following hardware:"
	echo ""
	voxl-print-sku-variables
	echo ""
	echo "If this doesn't look right, quit and run voxl-configure-sku to"
	echo "set it correctly. Then run voxl-configure-mpa again."

	if ! $NON_INTERACTIVE; then
		echo ""
		_ask_continue
	fi

	############################################################################
	## now for each of the _CONF_XYZ variables, construct the appropriate args
	## start with working defaults and update as appropriate
	############################################################################
	_USE_FPV_PX4=false
	_ARGS_EXTRINSICS="D0014_Starling_2"
	_ARGS_VIO_CAMS="tracking_front_down"
	_ARGS_CAM_NUM=""
	_ARGS_CAM_ROT=""
	_ARGS_IMU="factory_enable"
	_ARGS_PX4_IMU="factory_disable"
	_ARGS_CPU_MON="factory_enable"
	_ARGS_QVIO="disable"
	_ARGS_OPEN_VINS="factory_enable"
	_ARGS_DFS="factory_disable"
	_ARGS_TAG_DETECTOR="factory_disable"
	_ARGS_TFLITE="factory_disable"
	_ARGS_VVHUB="factory_enable"
	_ARGS_MAV_SERVER="factory_enable"
	_ARGS_PORTAL="enable"
	_ARGS_LEPTON="disable"
	_ARGS_LEPTON_TRACKER="disable"
	_ARGS_FLOW="disable"
	_ARGS_FEATURE_TRACKER="disable"
	_ARGS_UVC="disable"
	_ARGS_PX4="factory_enable"
	_ARGS_PX4_PARAMS=""
	_ARGS_ESC=""
	_ARGS_STREAMER="factory_enable"
	_ARGS_MODEM="disable"
	_ARGS_MAVCAM="factory_enable"
	_ARGS_RANGEFINDER="disable"
	_ARGS_VTX="--disable"
	_ARGS_VRX="--disable"
	_ARGS_STATE_ESTIMATOR="disable"
	_ARGS_IO="disable"
	_ARGS_OSD="disable"
	_ARGS_STATIC_IP="disable"
	_ARGS_RC_SERVER="disable"
	_ARGS_VOXL_ELRS=""

	############################################################################
	## go through each name and populate the other _CONF_XYZ variables with
	## appropriate defaults if they are missing. Do this BEFORE generating
	## args from the conf since some _CONF_XYZ vars may still be blank from
	## an incomplete part number or not enough arguments passed to this script
	############################################################################
	case "$VOXL_FAMILY_CODE" in

		"MDK-F0001") # Flight Deck with voxl+fc or voxl-flight
			_ARGS_EXTRINSICS="D0001_M500"
			_ARGS_VIO_CAMS="voxl1_tracking_old"
			;;

		"MDK-F0002") # VOXL Deck with NO Flight Core!
			_ARGS_EXTRINSICS="D0001_M500"
			_ARGS_VIO_CAMS="voxl1_tracking_old"
			;;

		"MRB-D0001") # M500
			_ARGS_EXTRINSICS="D0001_M500"
			_ARGS_VIO_CAMS="voxl1_tracking_old"
			;;

		"MCM-C0001") # voxlcam
			_ARGS_EXTRINSICS="D0003_Seeker_voxlcam"
			_ARGS_VIO_CAMS="voxl1_tracking_old"
			_ARGS_CAM_ROT="rotate_stereo"
			;;

		"MRB-D0003") # Seeker V1
			_ARGS_EXTRINSICS="D0003_Seeker_voxlcam"
			_ARGS_VIO_CAMS="voxl1_tracking_old"
			_ARGS_CAM_ROT="rotate_stereo"
			## turn off streamer for the version without hires
			if [ $VOXL_CAM_NUM == "7" ]; then
				_ARGS_STREAMER="disable"
				_ARGS_MAVCAM="disable"
			fi
			;;

		"MRB-D0004") # Qualcomm rb5 flight 5g development drone
			_ARGS_EXTRINSICS="D0004_RB5_Flight"
			_ARGS_VIO_CAMS="rb5_flight"
			_ARGS_IMU="disable"
			_ARGS_PX4_IMU="factory_enable"
			_ARGS_ESC="setup_sentinel_v1"
			_ARGS_PX4="sentinel_v1"
			_ARGS_QVIO="factory_enable"
			_ARGS_OPEN_VINS="disable"
			;;

		"MRB-D0005") # Starling
			_ARGS_EXTRINSICS="D0005_Starling"
			_ARGS_VIO_CAMS="single_tracking"
			_ARGS_QVIO="factory_enable"
			_ARGS_OPEN_VINS="disable"
			_ARGS_ELRS="--scan"
			_ARGS_PX4="crsf_gps_apm"
			_ARGS_PX4_PARAMS="MRB-D0005-V2"
			if [ "$VOXL_HW_VERSION" == "3" ]; then
				_ARGS_ESC="setup_starling_black_motors"
			else
				_ARGS_ESC="setup_starling_silver_motors"
			fi
			;;

		"MRB-D0006"|"MDK-F0006") # Sentinel and flight deck with voxl2
			_ARGS_EXTRINSICS="D0006_Sentinel"
			_ARGS_VIO_CAMS="single_tracking"
			_ARGS_QVIO="factory_enable"
			_ARGS_OPEN_VINS="disable"
			_ARGS_ESC="setup_sentinel_v1"
			if [ "$VOXL_HW_VERSION" == "1" ]; then
				# V1 with Holybro M8 gps and spektrum radio
				_ARGS_PX4="spektrum_gps_apm"
				_ARGS_PX4_PARAMS="MRB-D0006"
			else
				# V2 with MRo M10 gps and ELRS radio
				_ARGS_PX4="crsf_gps_apm"
				_ARGS_PX4_PARAMS="MRB-D0006-V2"
				_ARGS_ELRS="--scan"
			fi
			;;

		"MRB-D0008") # FPV
			_USE_FPV_PX4=true
			_ARGS_EXTRINSICS="D0008_FPV_V4"
			_ARGS_VIO_CAMS="tracking_left_right"
			_ARGS_PX4="d0008"
			_ARGS_PX4_PARAMS="MRB-D0008"
			_ARGS_FLOW="disable"
			_ARGS_OPEN_VINS="factory_enable"
			_ARGS_LEPTON_TRACKER="factory_enable_lepton"
			_ARGS_STATE_ESTIMATOR="factory_enable"

			if [ "$VOXL_HW_VERSION" == "3" ]; then
				_ARGS_ESC="setup_fpv_revb_v3" # tmotor
				_ARGS_PX4_PARAMS="MRB-D0008" # tmotor reversed motor dirs

			elif [ "$VOXL_HW_VERSION" == "4" ]; then
				_ARGS_ESC="setup_fpv_revb_v4" # m0138
				_ARGS_PX4_PARAMS="MRB-D0008-V4"
				_ARGS_LEPTON="m0130_manual"
				_ARGS_STREAMER="disable"
				_ARGS_OPEN_VINS="d8v4"
				_ARGS_VIO_CAMS="tracking_left_right"

			else # v5 and newer
				_ARGS_STREAMER="disable"
				_ARGS_OPEN_VINS="fpv"
				_ARGS_ESC="setup_fpv_revb_v4" # m0138
				_ARGS_PX4_PARAMS="MRB-D0008-V4"
				_ARGS_LEPTON="m0173_manual"
				_ARGS_EXTRINSICS="D0008_FPV_V5"
				_ARGS_VIO_CAMS="tracking_front_down"
			fi

			if [[ "$VOXL_MODEM_NUM" == "18" || "$VOXL_MODEM_NUM" == "19" || "$VOXL_MODEM_NUM" == "23" ]]; then
				_ARGS_VVHUB="factory_enable_vfc_pos_back_flow_steam_deck"
				_ARGS_STREAMER="factory_enable"
			elif [[ "$VOXL_HW_VERSION" == "4" ]]; then
				_ARGS_VVHUB="factory_enable_vfc_pos_back_flow_d0008v4"
			else
				_ARGS_VVHUB="factory_enable_vfc_pos_back_flow_d0008"
			fi

			;;


		"MRB-D0010") # starling  with stereo cam and rangefinders
			_ARGS_EXTRINSICS="D0005_Starling"
			_ARGS_VIO_CAMS="single_tracking"
			_ARGS_QVIO="factory_enable"
			_ARGS_OPEN_VINS="disable"
			_ARGS_ESC="setup_starling_black_motors"
			_ARGS_PX4="crsf_nogps_apm"
			_ARGS_PX4_PARAMS="MRB-D0010"
			_ARGS_RANGEFINDER="4_on_m0141"
			;;


		"MRB-D0011") # PX4 Autonomy Dev Kit
			_ARGS_EXTRINSICS="D0011_Starling_PX4_Edition"
			_ARGS_VIO_CAMS="single_tracking"
			_ARGS_QVIO="factory_enable"
			_ARGS_OPEN_VINS="disable"
			_ARGS_ESC="setup_starling_black_motors"
			_ARGS_PX4="crsf_gps_noapm"
			_ARGS_PX4_PARAMS="MRB-D0011"
			_ARGS_RANGEFINDER="1_downward_on_m0141"
			;;


		"MRB-D0012") # Starling 2 Max
			if [ "$VOXL_HW_VERSION" == "1" ]; then
				if [ "$VOXL_CAM_NUM" == "29" ]; then
					_ARGS_EXTRINSICS="D0012_Starling_2_Max_V1_C29_with_tof"
				else
					_ARGS_EXTRINSICS="D0012_Starling_2_Max_V1_C28_no_tof"
				fi
			else # version 2 and 3
				if [ "$VOXL_CAM_NUM" == "29" ]; then
					_ARGS_EXTRINSICS="D0012_Starling_2_Max_V2_C29_with_tof"
				else
					_ARGS_EXTRINSICS="D0012_Starling_2_Max_V2_C28_no_tof"
				fi
			fi

			# new custom 2204 motors spin opposite the tmotor 2203.5
			if [ "$VOXL_HW_VERSION" == "3" ]; then
				_ARGS_ESC="setup_d0012_custom_2204"
			else
				_ARGS_ESC="setup_d0012_tmotor_2203.5"
			fi


			_ARGS_VIO_CAMS="tracking_front_down"
			_ARGS_PX4="crsf_gps_noapm"
			_ARGS_PX4_PARAMS="MRB-D0012"
			_ARGS_RANGEFINDER="1_downward_on_m0173"
			## lepton is handled later in "extras" sku parsing
			;;


		"MRB-D0013") # D0013 Stinger
			if [ "$VOXL_HW_VERSION" == "1" ]; then
				_ARGS_EXTRINSICS="D0013_Stinger"
			else
				_ARGS_EXTRINSICS="D0013_Stinger_V2"
			fi
			_USE_FPV_PX4=true
			_ARGS_VIO_CAMS="tracking_front_down"
			_ARGS_ESC="setup_d0013"
			_ARGS_PX4="crsf_nogps_noapm"

			if [ "$VOXL_HW_VERSION" == "3" ]; then
				_ARGS_PX4_PARAMS="MRB-D0013-V3"
			else
				_ARGS_PX4_PARAMS="MRB-D0013"
			fi

			_ARGS_STREAMER="disable"
			_ARGS_OPEN_VINS="fpv"
			_ARGS_MAVCAM="disable"
			_ARGS_LEPTON="m0188"
			_ARGS_LEPTON_TRACKER="factory_enable_lepton"
			_ARGS_STATE_ESTIMATOR="factory_enable"
			_ARGS_VVHUB="factory_enable_vfc_pos_back_flow_d0013"
			;;

		"MRB-D0014") # Starling 2
			_ARGS_EXTRINSICS="D0014_Starling_2"
			_ARGS_VIO_CAMS="tracking_front_down"
			_ARGS_PX4="crsf_gps_noapm"
			_ARGS_PX4_PARAMS="MRB-D0014"
			_ARGS_ESC="setup_d0014_starling_2"
			_ARGS_RANGEFINDER="1_downward_on_m0173"

			if [ "$VOXL_CAM_NUM" == "27" ]; then
				_ARGS_VIO_CAMS="tracking_front_down_rear"
				_ARGS_OPEN_VINS="starling2_2cam"
			fi

			## lepton is handled later in "extras" sku parsing
			;;

		"MRB-D0015") # D0015
			#_ARGS_ESC="setup_d0015"
			_ARGS_EXTRINSICS="D0015_Fixed_Wing"
			_ARGS_PX4="d0015"
			_ARGS_PX4_PARAMS="MRB-D0015"
			_ARGS_QVIO="disable"
			_ARGS_OPEN_VINS="disable"
			;;


		"MRB-D0016") # Sparrow
			_ARGS_EXTRINSICS="D0016_Sparrow"
			_ARGS_VIO_CAMS="tracking_front_down_rear"
			_ARGS_PX4="crsf_nogps_noapm"
			_ARGS_PX4_PARAMS="MRB-D0016"
			_ARGS_ESC="setup_d0014_starling_2"
			_ARGS_FEATURE_TRACKER="factory_disable"
			_ARGS_CPU_MON="auto"
			;;
		
		"MRB-D0017") # D0017 Shikra
			_USE_FPV_PX4=true
			_ARGS_ESC="setup_d0017"
			_ARGS_PX4="crsf_nogps_noapm"
			_ARGS_PX4_PARAMS="MRB-D0017"
			_ARGS_STREAMER="disable"
			_ARGS_MAVCAM="disable"
			_ARGS_QVIO="disable"
			_ARGS_OPEN_VINS="disable"
			;;

		"MRB-D0019") # Seeker 7
			_USE_FPV_PX4=true
			_ARGS_PX4="crsf_gps_noapm"
			_ARGS_FLOW="disable"
			_ARGS_VVHUB="factory_enable_vfc_pos_back_flow_d0019"
			_ARGS_LEPTON_TRACKER="factory_enable_lepton"
			_ARGS_STATE_ESTIMATOR="factory_enable"
			_ARGS_ESC="setup_d0019" # m0138
			_ARGS_PX4_PARAMS="MRB-D0019"
			_ARGS_LEPTON="m0173_manual"
			_ARGS_EXTRINSICS="D0008_FPV_V5"
			_ARGS_VIO_CAMS="tracking_front_down"
			_ARGS_OPEN_VINS="disable"

			## enable streamer when LTE is installed
			if [[ "$VOXL_MODEM_NUM" == "2026" || "$VOXL_MODEM_NUM" == "2126" ]]; then
				_ARGS_STREAMER="factory_enable"
			else
				_ARGS_STREAMER="disable"
			fi
			;;

		"MRB-D0020") # Seeker 10
			_USE_FPV_PX4=true
			_ARGS_PX4="crsf_gps_noapm"
			_ARGS_FLOW="disable"
			_ARGS_VVHUB="factory_enable_vfc_pos_back_flow_d0020"
			_ARGS_LEPTON_TRACKER="factory_enable_lepton"
			_ARGS_STATE_ESTIMATOR="factory_enable"
			_ARGS_ESC="setup_d0020" # m0138
			_ARGS_EXTRINSICS="D0008_FPV_V5"
			_ARGS_VIO_CAMS="tracking_front_down"
			_ARGS_OPEN_VINS="disable"
			_ARGS_IO="d0020_payload_esad"

			# lepton disbaled later for C50 and C60 strike configs
			_ARGS_LEPTON="m0173_manual"

			## enable streamer when LTE is installed
			if [[ "$VOXL_MODEM_NUM" == "2026" || "$VOXL_MODEM_NUM" == "2126" ]]; then
				_ARGS_STREAMER="factory_enable"
			else
				_ARGS_STREAMER="disable"
			fi

			# load different param sets based on hw version and strike vs non-strike
			if [ "$_CONF_VERSION" == "6" ]; then
				# strike drones with no tracking cam or lepton don't need VVHUB
				if [[ "$VOXL_CAM_NUM" == "50" || "$VOXL_CAM_NUM" == "60" ]]; then
					_ARGS_VVHUB="factory_disable"
					_ARGS_PX4_PARAMS="MRB-D0020-V6-XS"
				else
					_ARGS_PX4_PARAMS="MRB-D0020-V6"
				fi
			else
				# strike drones with no tracking cam or lepton don't need VVHUB
				if [[ "$VOXL_CAM_NUM" == "50" || "$VOXL_CAM_NUM" == "60" ]]; then
					_ARGS_VVHUB="factory_disable"
					_ARGS_PX4_PARAMS="MRB-D0020-XS"
				else
					_ARGS_PX4_PARAMS="MRB-D0020"
				fi
				
			fi

			# strike drones with no tracking cam or lepton don't need VVHUB
			if [[ "$VOXL_CAM_NUM" == "50" || "$VOXL_CAM_NUM" == "60" ]]; then
				_ARGS_VVHUB="factory_disable"
			fi
			;;

		"TF-M0054") # voxl2 test fixture
			_ARGS_PX4="sentinel_v1"
			_ARGS_PX4_PARAMS="MRB-D0006"
			_ARGS_OPEN_VINS="disable"
			_ARGS_STREAMER="disable"
			_ARGS_MAVCAM="disable"
			;;

		"TF-M0104") # voxl2-mini test fixture
			_ARGS_PX4="sentinel_v1"
			_ARGS_PX4_PARAMS="MRB-D0006"
			_ARGS_OPEN_VINS="disable"
			_ARGS_STREAMER="disable"
			_ARGS_MAVCAM="disable"
			;;

		"MCCA-M0054") # voxl2 board only
			_ARGS_PX4="starling_v2"
			_ARGS_PX4_PARAMS="MRB-D0005-V2"
			_ARGS_OPEN_VINS="disable"
			_ARGS_STREAMER="disable"
			_ARGS_MAVCAM="disable"
			;;

		"MCCA-M0104") # voxl2-mini board only
			_ARGS_PX4="starling_v2"
			_ARGS_PX4_PARAMS="MRB-D0005-V2"
			_ARGS_OPEN_VINS="disable"
			_ARGS_STREAMER="disable"
			_ARGS_MAVCAM="disable"
			;;

		"MVX-T0001" | "MVX-T0002") # standalone vtx
			_ARGS_PX4="disable"
			_ARGS_STREAMER="disable"
			_ARGS_VVHUB="factory_disable"
			_ARGS_OPEN_VINS="disable"
			_ARGS_PORTAL="disable"
			_ARGS_MAVCAM="disable"
			_ARGS_IMU="disable"
			;;

		"MVX-R0001" | "MVX-R0002") # standalone vrx
			_ARGS_PX4="disable"
			_ARGS_VVHUB="factory_disable"
			_ARGS_MAV_SERVER="factory_disable"
			_ARGS_IMU="disable"
			_ARGS_OPEN_VINS="disable"
			_ARGS_PORTAL="disable"
			_ARGS_MAVCAM="disable"
			_ARGS_STATIC_IP="factory_enable"
			;;

		"MVX-H0001") # vrx-hub
			_ARGS_PX4="disable"
			_ARGS_VVHUB="factory_disable"
			_ARGS_MAV_SERVER="factory_disable"
			_ARGS_IMU="disable"
			_ARGS_OPEN_VINS="disable"
			_ARGS_PORTAL="disable"
			_ARGS_MAVCAM="disable"
			_ARGS_STATIC_IP="factory_enable_dgcs_pilot"
			_ARGS_RC_SERVER="factory_enable_dgcs_pilot"
			_ARGS_VRX="--factory_enable_dgcs_pilot"
			;;

		"MVX-S0001") # vrx-spire
			_ARGS_PX4="disable"
			_ARGS_VVHUB="factory_disable"
			_ARGS_MAV_SERVER="factory_disable"
			_ARGS_IMU="disable"
			_ARGS_OPEN_VINS="disable"
			_ARGS_PORTAL="disable"
			_ARGS_MAVCAM="disable"
			_ARGS_STATIC_IP="factory_enable_dgcs_tower"
			_ARGS_RC_SERVER="factory_enable_dgcs_tower"
			_ARGS_VOXL_ELRS="-p /dev/ttyHS1 --target MODALAI_M0193_TX"
			_ARGS_VRX="--factory_enable_dgcs_tower"
			;;

		*)
			echo -e "${RED}[ERROR] unknown family code: ${VOXL_FAMILY_CODE}${RESET_ALL}"
			exit 1
			;;
	esac


	# clear -x
	# echo -e "${GRN}${SET_BOLD}#############################################################${RESET_ALL}"
	# echo -e "${GRN}${SET_BOLD}Hardware Configuration:${RESET_ALL}"
	# modal_print_sku_variables


	############################################################################
	## Now set up config commands based on the config
	############################################################################

	# cam num argument is same as SKU config unless set differently by the
	# family logic above
	if [ "$_ARGS_CAM_NUM" == "" ]; then
		_ARGS_CAM_NUM=${VOXL_CAM_NUM}
	fi

	# modem handling
	if [ "$VOXL_MODEM_NUM" != "" ]; then
		case "$VOXL_MODEM_NUM" in
			"3"|"10"|"11"|"27"|"29")
				_ARGS_MODEM="microhard"
				;;
			"18"|"19"|"23")
				_ARGS_MODEM="doodle"
				;;

			"25"|"26"|"2026"|"2126") #M175+M176 or M0185
				_ARGS_VTX="--factory_enable"
				_ARGS_OSD="factory_enable"
				case "$VOXL_FAMILY_CODE" in
					"MRB-D0008") # fpv rev b
						if [ "$VOXL_HW_VERSION" == "5" ]; then
							_ARGS_VTX="--d0008_v5"
						else
							_ARGS_VTX="--d0008_v4"
						fi
						;;
					"MRB-D0013") # stinger
						_ARGS_VTX="--d0013"
						;;
					"MRB-D0016") #sparrow disable for now during prototyping
						_ARGS_VTX="--disable"
						;;
					"MRB-D0017") # shikra
						_ARGS_VTX="--d0013"
						;;
					"MRB-D0019")
						_ARGS_VTX="--d0008_v5"
						;;
					"MRB-D0020")
						if [[ "$VOXL_CAM_NUM" == "50" || "$VOXL_CAM_NUM" == "60" ]]; then
							_ARGS_VTX="--d0020_strike"
						else
							_ARGS_VTX="--d0008_v5"
						fi
						;;
					*)
						echo "voxl-vtx: using default config"
						;;
				esac

				# start LTE network
				if [[ "$VOXL_MODEM_NUM" == "2026" || "$VOXL_MODEM_NUM" == "2126" ]]; then
    				_ARGS_MODEM="v2-tmobile"

				fi

				case "$VOXL_FAMILY_CODE" in
					"MVX-T0001")
						_ARGS_VTX="--d0013" # voxl2-mini so we can use d0013 config
						_ARGS_OSD="external_fc"
						;;
					*)
						echo "voxl-vtx: using default config"
						;;
				esac
				;;
			"30")
				_ARGS_VRX="--factory_enable_mini_pini"
				;;
			"31"|"37")
				case "$VOXL_FAMILY_CODE" in
					"MVX-H0001")
						_ARGS_VRX="--factory_enable_dgcs_pilot"
						;;
					"MVX-S0001")
						_ARGS_VRX="--factory_enable_dgcs_tower"
						;;
					*)
						_ARGS_VRX="--factory_enable_spark_lan"
						;;
				esac
				;;
			"35") # dtc
				case "$VOXL_FAMILY_CODE" in
					"MRB-D0008"|"MRB-D0019"|"MRB-D0020")
						_ARGS_VTX="--factory_enable_dtc"
						_ARGS_OSD="factory_enable"
						_ARGS_STATIC_IP="factory_enable_dtc_drone"
						;;
					"MVX-R0001"|"MVX-H0001")
						_ARGS_VRX="--factory_enable_dtc"
						_ARGS_RC_SERVER="factory_enable_dgcs_pilot"
						_ARGS_STATIC_IP="factory_enable_dtc_vrx"
						;;
					*)
						echo "voxl-vtx: using default config"
						;;
				esac

				;;
			"38")
				# fiber
				_ARGS_OSD="factory_enable"
				case "$VOXL_FAMILY_CODE" in
					"MVX-R0001")
						_ARGS_VRX="--factory_enable_fiber"
						_ARGS_RC_SERVER="factory_enable_dgcs_pilot"
						;;
					"MRB-D0020")
						_ARGS_VTX="--factory_enable_fiber"
						_ARGS_STATIC_IP="factory_enable_dgcs_tower" # use same ip as tower
						_ARGS_IO="d0020_ip_payload_esad"
						;;
					*)
						echo "unsupported fiber config for family code: ${VOXL_FAMILY_CODE}"
						;;
				esac

				;;
			*)
				echo "No handling specified for Modem Num: ${VOXL_MODEM_NUM}"
				;;
		esac
	fi

	# misc handling
	if [ "$VOXL_EXTRAS_NUM" != "" ]; then
		case "$VOXL_EXTRAS_NUM" in
			"2"|"7")
				_ARGS_UVC="boson"
				;;

			"8"|"12"|"14"|"16")
				case "$VOXL_FAMILY_CODE" in
					"MRB-D0008") # seeker
						if [ "$VOXL_HW_VERSION" == "4" ]; then
							_ARGS_LEPTON="m0130_manual"
						elif [ "$VOXL_HW_VERSION" == "5" ]; then
							_ARGS_LEPTON="m0173_manual"
						fi
						;;

					"MRB-D0012"|"MRB-D0014") # starling 2s
						_ARGS_LEPTON="m0173_rotated"
						;;

					"MRB-D0013") # stinger
						_ARGS_LEPTON="m0188"
						;;

					"MRB-D0019") # seeker 7
						_ARGS_LEPTON="m0173_manual"
						;;

					"MRB-D0020") # seeker 10
						_ARGS_LEPTON="m0173_manual"
						;;

					*)
						echo "voxl-lepton-server: using default config"
						;;
				esac
				;;

			"3")
				case "$VOXL_FAMILY_CODE" in
					"MRB-D0008")
						_ARGS_IO="m0216_camera_control"
						;;
					*)
						echo "No handling specified for Misc Num 3 on family: ${VOXL_FAMILY_CODE}"
						;;
				esac
				;;

			*)
				echo "No handling specified for Misc Num: ${VOXL_EXTRAS_NUM}"
				;;
		esac
	fi

	# disable lepton for XS drones
	if [[ "$VOXL_CAM_NUM" == "50" ]] || [[ "$VOXL_CAM_NUM" == "60" ]]; then
		_ARGS_LEPTON="disable"
	fi

	# First pass at experimental handling, a second pass is done later for
	# things that need to go later in the setup sequence
	if [ "$VOXL_EXP_NUM" != "" ]; then
		echo "Experimental config specified"

		# Check if the bit for "1" is set (vfc)
		if (( ($VOXL_EXP_NUM & 1) != 0 )); then
			echo "Executing actions for bitmask 1 (vfc)"
			_ARGS_VVHUB="factory_enable_vfc_pos_back_flow"
			_ARGS_LEPTON_TRACKER="factory_enable_lepton"
			_ARGS_STATE_ESTIMATOR="factory_enable"

		fi
		# Check if the bit for "2" is set (zero rf mission)
		if (( ($VOXL_EXP_NUM & 2) != 0 )); then
			# Add the commands for bitmask 2
			echo "Executing actions for bitmask 2 (zero rf mission)"
		fi
		# Check if the bit for "4" is set (M0065)
		if (( ($VOXL_EXP_NUM & 4) != 0 )); then
			# Add the commands for bitmask 4
			echo "Executing actions for bitmask 4 (M0065)"
		fi
		# Check if the bit for "8" is set (test team tweaks)
		if (( ($VOXL_EXP_NUM & 8) != 0 )); then
			# Add the commands for bitmask 8
			echo "Executing actions for bitmask 8 test team tweaks"
		fi
		# Continue with other bits as needed...
	fi


	############################################################################
	## make a list of setup steps to take
	############################################################################
	CONFIG_STEPS=()

	if $_USE_FPV_PX4 ; then CONFIG_STEPS+=("apt install -y voxl-fpv-px4"); fi

	## do standard stuff, most wrapped in checks due to ubun2.0
	if [ -f /etc/systemd/system/voxl-wait-for-fs.service ]; then
		CONFIG_STEPS+=("systemctl enable --now voxl-wait-for-fs")
	fi

	if [ -f /etc/systemd/system/voxl-dma-latency.service ]; then
		CONFIG_STEPS+=("systemctl enable --now voxl-dma-latency")
	fi

	CONFIG_STEPS+=("systemctl disable voxl-static-ip")

	if [ -f /usr/bin/voxl-configure-extrinsics ]; then
		CONFIG_STEPS+=("voxl-configure-extrinsics ${_ARGS_EXTRINSICS}")
	fi

	if [ -f /usr/bin/voxl-configure-vio-cams ]; then
		CONFIG_STEPS+=("voxl-configure-vio-cams ${_ARGS_VIO_CAMS}")
	fi

	if [ -n "${_ARGS_CAM_NUM}" ] && [ -f /usr/bin/voxl-configure-cameras ]; then
		CONFIG_STEPS+=("voxl-configure-cameras ${_ARGS_CAM_NUM} ${_ARGS_CAM_ROT}")
	fi

	if [ -f /usr/bin/voxl-configure-cpu-monitor ]; then
		CONFIG_STEPS+=("voxl-configure-cpu-monitor ${_ARGS_CPU_MON}")
	fi

	if [ -f /usr/bin/voxl-configure-qvio ]; then
		CONFIG_STEPS+=("voxl-configure-qvio ${_ARGS_QVIO}")
	fi

	if [ -f /usr/bin/voxl-configure-dfs ]; then
		CONFIG_STEPS+=("voxl-configure-dfs ${_ARGS_DFS}")
	fi

	if [ -f /usr/bin/voxl-configure-tag-detector ]; then
		CONFIG_STEPS+=("voxl-configure-tag-detector ${_ARGS_TAG_DETECTOR}")
	fi

	if [ -f /usr/bin/voxl-configure-tflite ]; then
		CONFIG_STEPS+=("voxl-configure-tflite ${_ARGS_TFLITE}")
	fi

	if [ -f /usr/bin/voxl-configure-vision-hub ]; then
		CONFIG_STEPS+=("voxl-configure-vision-hub ${_ARGS_VVHUB}")
	fi

	if [ -f /usr/bin/voxl-configure-mavlink-server ]; then
		CONFIG_STEPS+=("voxl-configure-mavlink-server ${_ARGS_MAV_SERVER}")
	fi

	if [ -f /usr/bin/voxl-configure-portal ]; then
		CONFIG_STEPS+=("voxl-configure-portal ${_ARGS_PORTAL}")
	fi

	if [ -f /usr/bin/voxl-configure-lepton ]; then
		CONFIG_STEPS+=("voxl-configure-lepton ${_ARGS_LEPTON}")
	fi

	if [ -f /usr/bin/voxl-configure-lepton-tracker ]; then
		CONFIG_STEPS+=("voxl-configure-lepton-tracker ${_ARGS_LEPTON_TRACKER}")
	fi

	if [ -f /usr/bin/voxl-configure-uvc ]; then
		CONFIG_STEPS+=("voxl-configure-uvc ${_ARGS_UVC}")
	fi

	if [ -f /usr/bin/voxl-configure-streamer ]; then
		CONFIG_STEPS+=("voxl-configure-streamer ${_ARGS_STREAMER}")
	fi

	if [ -f /usr/bin/voxl-configure-modem ]; then
		CONFIG_STEPS+=("voxl-configure-modem ${_ARGS_MODEM}")
	fi

	if [ -f /usr/bin/voxl-configure-mavcam ]; then
		CONFIG_STEPS+=("voxl-configure-mavcam ${_ARGS_MAVCAM}")
	fi

	if [ -f /usr/bin/voxl-configure-rangefinders ]; then
		CONFIG_STEPS+=("voxl-configure-rangefinders ${_ARGS_RANGEFINDER}")
	fi
	
	if [ -f /usr/bin/voxl-configure-vtx ]; then
		CONFIG_STEPS+=("voxl-configure-vtx ${_ARGS_VTX}")
	fi

	if [ -f /usr/bin/voxl-configure-vrx ]; then
		CONFIG_STEPS+=("voxl-configure-vrx ${_ARGS_VRX}")
	fi

	if [ -f /usr/bin/voxl-configure-state-estimator ]; then
		CONFIG_STEPS+=("voxl-configure-state-estimator ${_ARGS_STATE_ESTIMATOR}")
	fi

	if [ -f /usr/bin/voxl-configure-io-server ]; then
		CONFIG_STEPS+=("voxl-configure-io-server ${_ARGS_IO}")
	fi

	if [ -f /usr/bin/voxl-configure-osd ]; then
		CONFIG_STEPS+=("voxl-configure-osd ${_ARGS_OSD}")
	fi

	if [ -f /usr/bin/voxl-configure-static-ip-watcher ]; then
		CONFIG_STEPS+=("voxl-configure-static-ip-watcher ${_ARGS_STATIC_IP}")
	fi

	if [ -f /usr/bin/voxl-configure-rc-server ]; then
		CONFIG_STEPS+=("voxl-configure-rc-server ${_ARGS_RC_SERVER}")
	fi

	## then stuff that might be missing on some platforms
	if [ -f /usr/bin/voxl-configure-px4-imu-server ]; then
		CONFIG_STEPS+=("voxl-configure-px4-imu-server ${_ARGS_PX4_IMU}")
	fi
	if [ -f /usr/bin/voxl-configure-imu ]; then
		CONFIG_STEPS+=("voxl-configure-imu ${_ARGS_IMU}")
	fi
	if [ -f /usr/bin/voxl-configure-flow-server ]; then
		CONFIG_STEPS+=("voxl-configure-flow-server ${_ARGS_FLOW}")
	fi
	## feature tracker is no longer used, only run if still installed, this should disable it
	if [ -f /usr/bin/voxl-configure-feature-tracker ]; then
		CONFIG_STEPS+=("voxl-configure-feature-tracker ${_ARGS_FEATURE_TRACKER}")
	fi
	if [ -f /usr/bin/voxl-configure-open-vins ]; then
		CONFIG_STEPS+=("voxl-configure-open-vins ${_ARGS_OPEN_VINS}")
	fi

	## set up elrs before esc setup to ensure slpi port access works
	case "$VOXL_TRANSMITTER_NUM" in 
		"7"|"8"|"10")
			CONFIG_STEPS+=("voxl-elrs ${_ARGS_VOXL_ELRS} configure")
			;;
		*)
			echo "[INFO] Not running voxl-elrs configuration for this SKU"
			;;
	esac

	## esc should come before PX4 since px4 would need to be disabled if running
	if [ $_ARGS_ESC ]; then

		## stop and disable voxl-px4 top stop voxl-esc from stopping and restarting it
		## voxl-px4 will be re-enabled in the next step
		CONFIG_STEPS+=("systemctl stop voxl-px4")
		CONFIG_STEPS+=("systemctl disable voxl-px4")
		CONFIG_STEPS+=("sleep 3")
		CONFIG_STEPS+=("voxl-esc ${_ARGS_ESC}")
	fi

	if [ -f /usr/bin/voxl-configure-px4 ]; then
		## remove px4 params here before starting px4 for the first time
		## deleting it later in voxl-px4-params doesn't work reliably
		## no need to stop voxl-px4 since it should have been stopped before
		## the esc step, but stop it anyway just in case
		CONFIG_STEPS+=("systemctl stop voxl-px4")
		CONFIG_STEPS+=("rm -f /data/px4/param/parameters")
		CONFIG_STEPS+=("voxl-configure-px4 ${_ARGS_PX4}")
	fi

	if [ -f /usr/bin/voxl-configure-px4-params ] && [ $_ARGS_PX4_PARAMS ]; then
		CONFIG_STEPS+=("systemctl stop voxl-mavlink-server")
		CONFIG_STEPS+=("sleep 1")
		CONFIG_STEPS+=("voxl-configure-px4-params --non-interactive --platform ${_ARGS_PX4_PARAMS}")
		# add ghost helper if enabled
		if [ "$VOXL_TRANSMITTER_NUM" == "9" ]; then
			CONFIG_STEPS+=("voxl-configure-px4-params --non-interactive --file /usr/share/modalai/px4_params/v1.14/radio_helpers/ghost.params")
		fi
	fi

	## enable artifact mode on D0008/D0019/D0020
	if [[ "$VOXL_FAMILY_CODE" == *"MRB-D0008"* || "$VOXL_FAMILY_CODE" == *"MRB-D0019"* || "$VOXL_FAMILY_CODE" == *"MRB-D0020"* ]]; then
		CONFIG_STEPS+=("set_px4_conf_param ARTIFACT_MODE ENABLE")
	fi


	# px4 transmitter handling, needs to be done after px4 has been configured
	if [ "$VOXL_TRANSMITTER_NUM" != "" ]; then
		case "$VOXL_TRANSMITTER_NUM" in
			"1") # spektrum
				CONFIG_STEPS+=("set_px4_conf_param RC SPEKTRUM")
				;;
			"6"|"7"|"8") # tbs crossfire or elrs
				CONFIG_STEPS+=("set_px4_conf_param RC CRSF_RAW")
				;;
			"9") # ghost rc
				CONFIG_STEPS+=("set_px4_conf_param RC GHST")
				;;
			*)
				echo "No handling specified for Transmitter Num: ${VOXL_TRANSMITTER_NUM}"
				;;
		esac
	fi

	# voxl-streamer customizations for certain camera configs
	if [ "$VOXL_CAM_NUM" != "" ]; then
		if [ "$_ARGS_STREAMER" == "factory_enable" ]; then
			case "$VOXL_CAM_NUM" in
				"28"|"29"|"30") # hires_front
				# set voxl-streamer to hires_front_small_encoded
					CONFIG_STEPS+=("set_param_string /etc/modalai/voxl-streamer.conf input-pipe hires_front_small_encoded")
					;;
				"18"|"32"|"36"|"37"|"46"|"47") # hires_misp_color
				# set voxl-streamer to hires_misp_color
					CONFIG_STEPS+=("set_param_string /etc/modalai/voxl-streamer.conf input-pipe hires_misp_color")
					;;
				*)
					echo "No custom voxl-streamer mods for: ${VOXL_CAM_NUM}"
					;;
			esac
		fi
	fi

	case "$VOXL_FAMILY_CODE" in
		"MVX-R0001")
			CONFIG_STEPS+=("set_param_string /etc/modalai/voxl-streamer.conf input-pipe vrx_encoded")
			;;
		"MVX-R0002")
			CONFIG_STEPS+=("set_param_string /etc/modalai/voxl-streamer.conf input-pipe vrx_encoded")
			;;
		*)
			;;
	esac


	# modem-based customizations for certain configs
	if [ "$VOXL_MODEM_NUM" != "" ]; then
		case "$VOXL_MODEM_NUM" in
			"18"|"19"|"23")
				# for joystick usage
				CONFIG_STEPS+=("set_px4_conf_param RC EXTERNAL")
				CONFIG_STEPS+=("voxl-configure-px4-params --non-interactive --file /usr/share/modalai/px4_params/v1.14/radio_helpers/steam_deck_joystick.params")
				CONFIG_STEPS+=("voxl-configure-px4-params --non-interactive --file /usr/share/modalai/px4_params/v1.14/radio_helpers/pwm_via_elrs.params")
				;;
			"30")
				CONFIG_STEPS+=("cp -f /usr/share/modalai/vtx/splash-0-M30.jpg /usr/share/modalai/vtx/splash.jpg")
				;;
			"31")
				CONFIG_STEPS+=("cp -f /usr/share/modalai/vtx/splash-1.jpg /usr/share/modalai/vtx/splash.jpg")
				;;
			"32") # wavemux
				CONFIG_STEPS+=("voxl-configure-wavemux")
				;;
			*)
				echo "No custom modem mods for: ${VOXL_MODEM_NUM}"
				;;
		esac
	fi

	# misc handling post px4 config tweaks
	if [ "$VOXL_EXTRAS_NUM" != "" ]; then
		case "$VOXL_EXTRAS_NUM" in
			"1"|"3"|"5"|"7") # DJI OSD
				CONFIG_STEPS+=("set_px4_conf_param OSD DJI_MSP_DP")
				CONFIG_STEPS+=("voxl-configure-px4-params --non-interactive --file /usr/share/modalai/px4_params/v1.14/other_helpers/msp_dp_osd.params")
				;;
			"4"|"6"|"12"|"14"|"15"|"16") # HDZERO or Walksnail
				CONFIG_STEPS+=("set_px4_conf_param OSD HDZERO")
				CONFIG_STEPS+=("voxl-configure-px4-params --non-interactive --file /usr/share/modalai/px4_params/v1.14/other_helpers/msp_dp_osd_skyvision.params")
				;;
			*)
				echo "No handling specified for Misc Num: ${VOXL_EXTRAS_NUM}"
				;;
		esac
	fi

	# second pass of experimental handling, a first pass is up above since order matters
	if [ "$VOXL_EXP_NUM" != "" ]; then
		echo "Experimental config specified"
		
		# Check if the bit for "1" is set (vfc)
		if (( ($VOXL_EXP_NUM & 1) != 0 )); then
			echo "Executing actions for bitmask 1"
		fi
		# Check if the bit for "2" is set (zero rf mission)
		if (( ($VOXL_EXP_NUM & 2) != 0 )); then
			# Add the commands for bitmask 2
			echo "Executing actions for bitmask 2"
			CONFIG_STEPS+=("voxl-configure-px4-params --non-interactive --file /usr/share/modalai/px4_params/v1.14/EKF2_helpers/indoor_vio_missing_gps.params")
			CONFIG_STEPS+=("set_param /etc/modalai/voxl-mavlink-server.conf autopilot_mission_delay_start 45")
			CONFIG_STEPS+=("voxl-configure-px4-params --non-interactive --file /usr/share/modalai/px4_params/v1.14/experimental_do_not_use/zero_rf_missions.params")
			CONFIG_STEPS+=("set_variable /usr/bin/voxl-static-ip LOCAL_IP=\"\" LOCAL_IP="\"192.168.8.20\")
			CONFIG_STEPS+=("set_variable /usr/bin/voxl-static-ip wlan0 eth0")
			CONFIG_STEPS+=("systemctl enable voxl-static-ip")
		fi
		# Check if the bit for "4" is set (M0065)
		if (( ($VOXL_EXP_NUM & 4) != 0 )); then
			# Add the commands for bitmask 4
			echo "Executing actions for bitmask 4"
			CONFIG_STEPS+=("set_px4_conf_param ESC VOXL2_IO_PWM_ESC")
			CONFIG_STEPS+=("set_px4_conf_param RC M0065_SBUS")
			CONFIG_STEPS+=("voxl-configure-px4-params --non-interactive --file /usr/share/modalai/px4_params/v1.14/experimental_do_not_use/voxl2_io.params")
		fi
		# Check if the bit for "8" is set (test team tweaks)
		if (( ($VOXL_EXP_NUM & 8) != 0 )); then
			# Add the commands for bitmask 8
			echo "Executing actions for bitmask 8 test team tweaks"
			CONFIG_STEPS+=("voxl-configure-px4-params --non-interactive --file /usr/share/modalai/px4_params/v1.14/fpv_helpers/test_team_tweaks_LOAD_LAST.params")
			CONFIG_STEPS+=("set_px4_conf_param ARTIFACT_MODE DISABLE")
		fi
		# Continue with other bits as needed...
	fi


	############################################################################
	## print the whole list of setup steps and pause if in debug mode
	############################################################################
	echo ""
	echo -e "${GRN}${SET_BOLD}About to Execute:${RESET_ALL}"
	for cmd in "${CONFIG_STEPS[@]}"; do echo -e "$cmd"; done

	## in debug mode we probably want to pause here to inspect what
	## was just printed
	if $DEBUG && ! $NON_INTERACTIVE; then
		echo ""
		_ask_continue
	fi

	############################################################################
	## execute everything in the list
	############################################################################
	HAD_FAILURE=false
	FAILURES=()
	HAD_SUCCESSFUL_RETRY=false
	SUCCESSFUL_RETRIES=()

	set +e
	for cmd in "${CONFIG_STEPS[@]}"; do
		_run_step "${cmd}"
	done

	## very important, sync everything to disk before saying we are done!!
	sync

	## check if any extra calibration is needed so we can warn the user
	echo -e ""
	voxl-check-calibration --quiet
	MISSING_CAL=$?
	COLOR=${GRN}

	if $HAD_SUCCESSFUL_RETRY; then
		echo -e ""
		PRINT_GREEEN_LINE
		echo -e "${YLW}${SET_BOLD}         The following steps worked on the second try:${RESET_ALL}"
		for cmd in "${SUCCESSFUL_RETRIES[@]}"; do
			echo -e "${YLW}$cmd${RESET_ALL}"
		done
	fi

	if ! $HAD_FAILURE; then
		echo -e ""
		PRINT_GREEEN_LINE
		echo -e "${GRN}${SET_BOLD}        SUCCESSFULLY CONFIGURED MPA SERVICES!${RESET_ALL}"
		echo -e "${GRN}${SET_BOLD}        Services will start up on next reboot${RESET_ALL}"
		echo -e ""
	else
		echo -e ""
		PRINT_GREEEN_LINE
		echo -e "${RED}${SET_BOLD}         FAILED TO EXECUTE the following steps:${RESET_ALL}"
		for cmd in "${FAILURES[@]}"; do
			echo -e "${RED}$cmd${RESET_ALL}"
		done
		echo -e ""
		echo -e "${RED}${SET_BOLD}      Encountered Problems Configuring MPA Services :-/${RESET_ALL}"
		echo -e "${RED}${SET_BOLD}        Some Services may not start up on next reboot${RESET_ALL}"
		echo -e ""
		COLOR=${RED}
	fi


	#rerun cal file check, this time printing the result
	voxl-check-calibration

	echo -e ""
	echo -e "${GRN}${SET_BOLD}                PLEASE POWER CYCLE YOUR VOXL${RESET_ALL}"
	PRINT_GREEEN_LINE
	echo -e ""

	if $HAD_FAILURE; then
		exit 1
	fi

	exit 0
}



_main "$@"

