#!/bin/bash

function transmit_command {

	MODE=$1
	echo "CubeSatSim is in Transmit Commands mode"
	echo

	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value
	
	echo -n "TX Frequency is: "
	echo -n ${7}
	echo " MHz"

	FILE=/home/pi/CubeSatSim/transmit_dtmf
	if [ -f "$FILE" ]; then

		echo
		echo "Sending DTMF tones to change to mode "$MODE

		case $MODE in
	
			a)
				number=1
				;;
			f)
				number=2
				;;
			b)
				number=3
				;;
			s)
				number=4
				;;
			m)
				number=5
				;;
			e)
				number=6
				;;
			j)
				number=7
				;;
			p)
				number=8
				;;
			o)
				number=10
				;;
			*)
				number=0
				;;
		esac

#		echo $number

		cat /home/pi/CubeSatSim/direwolf/direwolf-transmit-dtmf.conf > /home/pi/CubeSatSim/direwolf-tmp.conf && echo 'CBEACON dest="DTMF-3" info="'$number' #" delay=0' >> /home/pi/CubeSatSim/direwolf-tmp.conf

#	 	echo "Stopping command and control"
#		sudo systemctl stop command

#		echo "Transmit start"
		gpio write 28 0 # ptt
		gpio write 2 1 # tx LED
		timeout 3 direwolf -c /home/pi/CubeSatSim/direwolf-tmp.conf  -t 0l > /dev/null 2>&1
		gpio write 2 0 # tx LED
		gpio write 28 1 #ptt
#		echo "Transmit stop"

#	 	echo "Resuming command and control"
#		sudo systemctl start command

	else

		STRING="$1-11>APCSS:=3901.40N\07704.39WShi hi MODE="$MODE
		sudo rm /home/pi/CubeSatSim/t.txt > /dev/null 2>&1
		echo $STRING > /home/pi/CubeSatSim/t.txt
		echo
		echo -n "Sending APRS packet to change mode to "$MODE" "
		echo $STRING
		sudo touch /home/pi/CubeSatSim/ready
		sleep 3

	fi

	echo
	echo "To change the mode of this CubeSatSim use config -n"
}

function check_restart {

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value
	MODE=$1

	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value
	if [ "${14}" = "sim" ]  || [ "${14}" = "rig" ] ; then
		DOPPLER=1
	else
		DOPPLER=0
	fi

#	if [ "$MODE" = "p" ] || [ "$MODE" = "P" ] ; then
#			reboot=1
#el
	if [ "$MODE" == "f" ] || [ "$MODE" == "b" ]  || [ "$MODE" == "e" ] || [ "$MODE" == "j" ] ; then
		FILE=/home/pi/CubeSatSim/battery_saver
		if [ -f "$FILE" ]; then
			restart=1
#			echo "Need to restart since batt saver"
		else 
			reboot=1
#			echo "Need to reboot"
		fi	
	elif [ "$MODE" == "m" ] && [ "$DOPPLER" == "1" ] ; then
		reboot=1
	else
		restart=1
#		echo "Need to restart"
	fi	
}

echo
echo "CubeSatSim v2.3 configuration tool"
echo
# echo $1
# echo $2
# echo

param1=$1
param2=$2
param3=$3
param4=$4

sudo modprobe snd-aloop

if [ "$2" = "-n" ] ; then
# if [ -z "$2" ] ; then
	echo "Reboot disabled"
	noreboot=1
	param2=""
else
	noreboot=0
fi

#else
#	if [ "$2" = "-n" ] ; then
#		echo "Reboot disabled"
#		noreboot=1
#	else
#		fail=$2
#	fi
#
# fi

# echo "No reboot"
# echo $noreboot

reboot=0
restart=0

if [ "$1" = "" ]; then
	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value
	MODE=$1

	if [ "$1" = "a" ]; then
		echo "Mode is APRS"
	elif [ "$1" = "m" ]; then
		echo "Mode is CW"
	elif [ "$1" = "f" ]; then
		echo "Mode is FSK"
	elif [ "$1" = "b" ]; then
		echo "Mode is BPSK"	
	elif [ "$1" = "s" ]; then
		echo "Mode is SSTV"
	elif [ "$1" = "e" ]; then
		echo "Mode is Repeater"	
	elif [ "$1" = "j" ]; then
		echo "Mode is FUNcube"	
	elif [ "$1" = "n" ]; then
		echo -n "Mode is Transmit Commands with "
		FILE=/home/pi/CubeSatSim/transmit_dtmf
		if [ -f "$FILE" ]; then
			echo -n "DTMF"
		else	
			echo -n "APRS"
		fi
	elif [ "$1" = "p" ]; then
		echo "Mode is Pacsat"	
	elif [ "$1" = "P" ]; then
		echo "Mode is Pacsat Ground Station"	
	else
		echo 
	fi
	echo
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value
	
	if [ "$5" = "y" ] || [ "$5" = "yes" ] ; then
		echo "Simulated Telemetry is ON"
	else
		FILE=/home/pi/CubeSatSim/sim_mode_auto
		if [ -f "$FILE" ]; then
			echo "Simulated Telemetry is automatically turned ON"
#		elif [[ $(timeout 2 i2cdetect -y 1 | grep -e "44" -e "45") ]]; then  # check for battery board sensors
#		# Check the exit code of the last command
#			if [ $? -ne 0 ]; then
#				echo "Simulated Telemetry is automatically turned ON"
#			else
#				echo "Simulated Telemetry is OFF"
#			fi
		else
			echo "Simulated Telemetry is OFF"
		fi
	fi
#	echo

	FILE=/home/pi/CubeSatSim/failure_mode.txt
	if [ -f "$FILE" ]; then
		if [[ $(grep "\-1" $FILE) ]]; then
			echo "No simulated failure"
		else
			fail=$(<$FILE)
			echo -n "Simulated "
#			cat $FILE

			case $fail in

				1)
					echo "+Y Solar Panel Unplugged (1)"
					;;	
				2)
					echo "+X Solar Panel Failure (2)"
					;;
				3)
					echo "-X Solar Panel Degredation (3)"
					;;
				4)
					echo "-Y Solar Panel Short Circuit (4)"
					;;
				5)
					echo "Failed I2C Bus 1 (5)"
					;;
				6)
					echo "Failed I2C Bus 3 (6)"
					;;
				7)
					echo "Failed Camera (7)"
					;;
				8)
					echo "Failed Payload (8)"
					;;
				9)
					echo "Failed BME Sensor (9)"
					;;
				10)
					echo "Failed MPU Sensor (10)"
					;;
				"11")
					echo "Failed FM Audio (11)"
					;;
				*)
					echo "Unknown Failure"
					;;
			esac
#			echo $fail
		fi
	else
		echo "No simulated failure"
	fi

	if [ "${12}" = "y" ] || [ "${12}" = "yes" ] ; then
		echo "Random Failure Mode is ON with time period" ${13} "seconds"
	else
		echo "Random Failure Mode is OFF"
	fi

HAB=$9
SQUELCH=$6
RXPL=${10}
TXPL=${11}

#	echo ${14}
	if [[ ( "${14}" == "sim" || "${14}" == "rig" ) && ( "$MODE" == "f" || "$MODE" == "b" || "$MODE" == "j" || "$MODE" == "G") ]] ; then
		echo "Doppler frequency shift is ON but not active in this mode (only APRS, CW, SSTV, Pacsat, and Repeater modes)"
		echo -n "TX Frequency is approximately: "
		echo -n ${7}
		echo -n " MHz, RX Frequency is: "
		echo -n ${8}
		echo " MHz"
	elif [ "${14}" = "sim" ] ; then
		echo "Simulated Doppler frequency shift enabled"
		echo -n "Current TX and RX Frequency (Hz) is: "
		cat /home/pi/CubeSatSim/frequency.txt
		echo
#		echo -n ${7}
#		echo -n " MHz, RX Frequency is: "
#		echo -n ${8}
#		echo " MHz"
	elif [ "${14}" = "rig" ] ; then
		echo "Radio Control Doppler frequency shift enabled"
		echo -n "Current TX Frequency (Hz) is: "
		head -n 1 /home/pi/CubeSatSim/frequency.txt | cut -d ' ' -f 1
		echo -n "Current RX Frequency (Hz) is: "
		head -n 1 /home/pi/CubeSatSim/frequency.txt | cut -d ' ' -f 2
	elif [[ ( "$MODE" == "f" || "$MODE" == "b" || "$MODE" == "j") ]] ; then
		echo "Doppler frequency shift is OFF"
		echo -n "TX Frequency is approximately: "
		echo -n ${7}
		echo -n " MHz, RX Frequency is: "
		echo -n ${8}
		echo " MHz"
	else
		echo "Doppler frequency shift is OFF"
		echo -n "TX Frequency is: "
		echo -n ${7}
		echo -n " MHz, RX Frequency is: "
		echo -n ${8}
		echo " MHz"

	fi

	if [ "$HAB" = "yes" ] || [ "$HAB" = "y" ]; then
		echo "Balloon mode is ON"
	else
		echo "Balloon mode is OFF"
	fi

#	echo
	echo -n "Current command count is: "
	cat /home/pi/CubeSatSim/command_count.txt
	echo
#	echo
#	echo "Current beacon transmit mode is:"
#	cat /home/pi/CubeSatSim/command_tx
#	echo

#	echo $MODE
	if [ "$MODE" = "p" ] || [ "$MODE" = "P" ] ; then
		echo "Squelch is off since Pacsat or Pacsat Ground Station mode"
	else
		echo -n "Squelch level is: "
		echo $SQUELCH
	fi
#	echo

	FILE=/home/pi/CubeSatSim/command_control
	if [ -f "$FILE" ]; then
    		
		if [[ $(arecord -l | grep card) ]]; then
			FILE=/home/pi/CubeSatSim/command_control_direwolf
			if [ -f "$FILE" ]; then
				echo "Radio DTMF/APRS command and control is ON"
			else
				if [ "$MODE" = "p" ] || [ "$MODE" = "P" ] ; then
					echo "Radio carrier command and control is off since Pacsat or Pacsat Ground Station mode"
				else
					echo "Radio carrier command and control is ON"
				fi
			fi
		else
			if [ "$MODE" = "p" ] || [ "$MODE" = "P" ] ; then
				echo "Radio carrier command and control is off since Pacsat or Pacsat Ground Station mode"
			else
				echo "Radio carrier command and control is ON"
			fi
		fi
	else 
    		echo "Radio command and control is OFF"
	fi

#	echo

	echo -n "RX PL code is: "
	echo -n $RXPL
#	echo
	echo -n " TX PL code is: "
	echo $TXPL
#	echo

	FILE=/home/pi/CubeSatSim/battery_saver
	if [ -f "$FILE" ]; then
		echo "Safe Mode! Battery saver mode is ON"
	else 
    		echo "Battery saver mode is OFF"
	fi

#	echo
	FILE=/home/pi/CubeSatSim/beacon_off
	if [ -f "$FILE" ]; then
    		echo "Transmit beacon telemetry is OFF"
	else
  		echo "Transmit beacon telemetry is ON"
	fi

	if [[ $(arecord -l | grep "USB Audio Device") ]] ; then
	  echo "USB Sound Card detected"
	  soundcard=1
	else
	  echo "No USB Sound Card detected"
	  soundcard=0
	fi  
	
	gpio -g mode 7 up
	if [[ $(gpio -g read 7 | grep 0) ]] ; then
	  echo "FM TXC is present"
	  txc=1
	else
	  echo "FM TXC not present"
	  txc=0
	fi  
	
	if [[ $(lsusb | grep "RTL") ]] ; then
	  echo "RTL-SDR detected"
	  rtl=1
	else
	  echo "No RTL-SDR detected"
	  rtl=0
	fi

#if [[ $(grep '11.' /etc/debian_version) ]]; then
if [[ $(grep 'bullseye' /etc/os-release) ]]; then
	echo "Pi OS is Bullseye (Debian 11)"
elif [[ $(grep 'bookworm' /etc/os-release) ]]; then
	echo "Pi OS is Bookworm (Debian 12)"
elif [[ $(grep 'trixie' /etc/os-release) ]]; then
	echo "Pi OS is Trixie (Debian 13)"
fi


	echo
	echo -e "Current sim.cfg configuration file:"	
#	echo
	
#	echo $1 $2 $3 $4 $5 $SQUELCH $7 $8 $HAB $RXPL $TXPL ${12} ${13} ${14}
	cat /home/pi/CubeSatSim/sim.cfg
	echo
	
	echo "To change, include an OPTION"
	echo "To see options, type config -h"
#	echo
#	set -- "-h"
# fi	


elif [ "$1" = "-i" ]; then

	restart=1

elif [ "$1" = "-a" ]; then

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

	if [ "$1" == "n" ]; then

		transmit_command "a"

	else

		echo "changing CubeSatSim to AFSK mode"
		check_restart
		sudo echo "a" > /home/pi/CubeSatSim/.mode	

	fi

elif [ "$1" = "-m" ]; then

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

	if [ "$1" == "n" ]; then

		transmit_command "m"

	else

		echo "changing CubeSatSim to CW mode"

		value=`cat /home/pi/CubeSatSim/sim.cfg`
		echo "$value" > /dev/null
		set -- $value
		if [ "${14}" = "sim" ]  || [ "${14}" = "rig" ] ; then
			DOPPLER=1
		else
			DOPPLER=0
		fi

		if [ "$DOPPLER" = "1" ] ; then
			restart=1
		else
			check_restart
		fi

		sudo echo "m" > /home/pi/CubeSatSim/.mode

	fi
 
elif [ "$1" = "-f" ]; then

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

	if [ "$1" == "n" ]; then

		transmit_command "f"

	else

		echo "changing CubeSatSim to FSK mode"
		sudo echo "f" > /home/pi/CubeSatSim/.mode
		if [ "$1" = "p" ] || [ "$1" = "P" ] ; then
			reboot=1
		else
			restart=1
		fi
	fi

elif [ "$1" = "-b" ]; then

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

	if [ "$1" == "n" ]; then

		transmit_command "b"

	else

		echo "changing CubeSatSim to BPSK mode"
		sudo echo "b" > /home/pi/CubeSatSim/.mode
		if [ "$1" = "p" ] || [ "$1" = "P" ] ; then
			reboot=1
		else
			restart=1
		fi
	fi

elif [ "$1" = "-s" ]; then

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

	if [ "$1" == "n" ]; then

		transmit_command "s"

	else

		echo "changing CubeSatSim to SSTV mode"
		check_restart
		sudo echo "s" > /home/pi/CubeSatSim/.mode

	fi

elif [ "$1" = "-t" ]; then

	echo "Setting the Simulated Telemetry setting"
	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	if [ -z "$param2" ] ; then

		if [ "$5" = "yes" ] || [ "$5" = "y" ]; then
			echo "Simulated Telemetry is ON"
		else
			echo "Simulated Telemetry is OFF"
		fi
		
		echo
		
	#	$1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${12} ${13} ${14}

		echo "Do you want Simulated Telemetry ON (y/n) "
		read sim
		echo
	else
		sim="$param2"
	fi
		
	if [ "$sim" = "y" ] || [ "$sim" = "yes" ]  ; then
		sim="yes"
		echo "Simulated Telemetry is ON"
	else
		sim="no"
		echo "Simulated Telemetry is OFF"
		echo "-1" > /home/pi/CubeSatSim/failure_mode.txt  # make sure to turn off any simulated failures
	fi

#	echo
	echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"
#	echo
	echo $1 $2 $3 $4 $sim $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}
	echo $1 $2 $3 $4 $sim $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} > /home/pi/CubeSatSim/sim.cfg
	echo

	restart=1
	
elif [ "$1" = "-c" ]; then

	echo "Setting the CALLSIGN"
	echo

#	echo -e "Current sim.cfg configuration file:"	
#	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	oldcallsign="$1"

	echo "Current value of CALLSIGN is"	 $oldcallsign
	echo
	
#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${12} ${13} ${14}

	if [ -z "$param2" ] ; then

		echo "Return keeps current value."
	
		echo "Enter callsign in all capitals: "
		read callsign

	else
		callsign="$param2"
	fi
	
	if [ -z $callsign ] ; then
		callsign="$1"
		echo "Keeping value of" $callsign
	else
		restart=1
		echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"

		echo $callsign $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}
		echo $callsign $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} > /home/pi/CubeSatSim/sim.cfg
	fi
	
elif [ "$1" = "-r" ]; then

	echo "Set the Reset Count for FoxTelem"
	echo
#	echo -e "Current sim.cfg configuration file:"	
#	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value
	
	echo
	echo "Current value of Reset Count is"	$2
	echo
	
#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}

	if [ -z "$param2" ] ; then
		echo "Return keeps current value."
		echo -e "Enter Reset Count (integer): "
		read resets
	else
		resets="$param2"
		echo "Reset Count set to" $param2
	fi

	if [ -z $resets ] ; then
		resets="$2"
		echo "Keeping value of" $resets
	fi

	if ! [[ $resets =~ ^[0-9]+$ ]] ; then
		echo "Error: not an integer!"
		resets="$2"
		echo "Keeping value of" $resets
		norestart=1
	else

		echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"

		echo $1 $resets $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}
		echo $1 $resets $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} > /home/pi/CubeSatSim/sim.cfg
	fi	

	if [ "$norestart" = "1" ]; then
		echo
	else
		restart=1
	fi
	
elif [ "$1" = "-l" ]; then

	echo "Editing latitude and longitude in the"
	echo "configuration file for CubeSatSim"
	echo "(Only used for APRS telemetry)"
	echo
#	echo -e "Current sim.cfg configuration file:"	
#	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	echo
	echo "Current value of latitude is"	$3
	echo
	
#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}

	if [ -z "$param2" ] ; then

		echo "Return keeps current value."
		echo
		echo -e "Enter latitude  (decimal degrees, positive is north): "
	
		read lat 
	
		if [ -z $lat ] ; then
	
			lat="$3"
			echo "Keeping value of" $lat
		fi
		
		echo
		echo "Current value of longitude is" $4	
		echo
	
		echo -e "Enter longitude (decimal degrees, positive is east): "
	
		read long 
	
		if [ -z $long ] ; then
	
			long="$4"
			echo "Keeping value of" $long
		fi


	else
		lat="$param2"
		echo "Changing Latitude to" $lat
		if [ -z "$param3" ] ; then
			lon="$3"
			echo "Keeping Longitude of" $lon
		else
			long="$param3"
			echo "Changing Longitude to" $long
		fi
	fi

	if ! [[ $long =~ ^[+-]?[0-9]+([.][0-9]+)?$ ]] ; then

		echo "Error: not a number!"
		long="$4"
		echo "Keeping value of" $long
	fi 

	if ! [[ $lat =~ ^[+-]?[0-9]+([.][0-9]+)?$ ]] ; then

		echo "Error: not a number!"
		lat="$3"
		echo "Keeping value of" $lat
	fi 

	echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"
	echo $1 $2 $lat $long $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}
	echo $1 $2 $lat $long $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} > /home/pi/CubeSatSim/sim.cfg
	
	if [ "$norestart" = "1" ]; then
		echo 
	else
		restart=1
	fi

elif [ "$1" = "-S" ]; then

	echo "Scan both I2C buses on the Raspberry Pi"
	echo 

	echo "I2C Bus 1"
	echo
	i2cdetect -y 1
	echo
	echo "I2C Bus 1"
	echo
	i2cdetect -y 3
	echo

elif [ "$1" = "-C" ]; then

	echo "Clear logs"
	echo 

	sudo systemctl stop cubesatsim
	sudo systemctl stop transmit
	sudo systemctl stop command
	sudo systemctl stop pacsatsim

	sudo mv -f /home/pi/CubeSatSim/telem.txt /home/pi/CubeSatSim/telem.txt.bk

	sudo journalctl --rotate
	sudo journalctl --vacuum-time=1s

	reboot=1
##	sudo systemctl reboot now

##	echo "rebooting"

elif [ "$1" = "-T" ]; then

	echo "Change command and control state"
	echo 

	if [ -z "$param2" ] ; then
		FILE=/home/pi/CubeSatSim/command_control
		if [ -f "$FILE" ]; then
	    	echo "Radio command and control is ON"
			echo
			echo "Do you want to turn command and control to OFF (y/n) "
			read reset
			echo
	
			if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
				echo "Turning command and control OFF"
				sudo rm /home/pi/CubeSatSim/command_control > /dev/null 2>&1
	#			reboot=1
				echo "restarting command and control"
				sudo systemctl restart command
				echo "restarting transmit"
				sudo systemctl restart transmit
	##			sudo reboot now	
			fi
	
		else 
	    	echo "Radio command and control is OFF"
			echo
			echo "Do you want to set command and control to ON (y/n) "
			read reset
			echo
	
			if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
	
				value=`cat /home/pi/CubeSatSim/.mode`
				echo "$value" > /dev/null
				set -- $value
		
				if [ "$1" != "n" ] ; then
					echo "Turning command and control ON"
	
					value=`cat /home/pi/CubeSatSim/sim.cfg`
					echo "$value" > /dev/null
					set -- $value
					
					echo -n "RX Frequency is: "
					echo -n ${8}
					echo " MHz"
					echo
	
					sudo touch /home/pi/CubeSatSim/command_control
					echo "restarting command and control"
	#				reboot=1
					sudo systemctl restart command
					echo "restarting transmit"
					sudo systemctl restart transmit
	##				sudo reboot now	
				else
					echo "Can't turn on Command and control in Transmit Commands mode."
				fi
			fi
	
		fi
	else
		reset="$param2"

		if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
			echo "Turning command and control ON"
			value=`cat /home/pi/CubeSatSim/sim.cfg`
			echo "$value" > /dev/null
			set -- $value
			
			echo -n "RX Frequency is: "
			echo -n ${8}
			echo " MHz"
			echo

			sudo touch /home/pi/CubeSatSim/command_control
			echo "restarting command and control"
			sudo systemctl restart command
			echo "restarting transmit"
			sudo systemctl restart transmit
		else	
			echo "Turning command and control OFF"
			sudo rm /home/pi/CubeSatSim/command_control > /dev/null 2>&1
			echo "restarting command and control"
			sudo systemctl restart command
			echo "restarting transmit"
			sudo systemctl restart transmit
		fi
	fi

elif [ "$1" = "-d" ]; then

	echo "Change command and control Direwolf state"
	echo 
	if [[ $(arecord -l | grep card) ]]; then
		:
	else
		echo "Note: No USB Sound Card is plugged in!"
		echo "Direwolf will not run unless one is plugged in."
	fi

	if [ -z "$param2" ] ; then
	
		FILE=/home/pi/CubeSatSim/command_control_direwolf
		if [ -f "$FILE" ]; then
			echo "Radio command and control with Direwolf for DTMF and APRS is ON"
			echo
			echo "Do you want to turn Direwolf OFF and do Carrier command and control (y/n) "
			read reset
			echo
	
			if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
				echo "Command and control Direwolf set to OFF"
				sudo rm /home/pi/CubeSatSim/command_control_direwolf > /dev/null 2>&1
				sudo systemctl restart command
			fi
	
		else 
	    	echo "Radio command and control with Direwolf for DTMF and APRS is OFF so carrier command and control is enabled"
			echo
			echo "Do you want to set command and control with Direwolf for DTMF and APRS to ON (y/n) "
			read reset
			echo
	
			if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
				echo "Command and control Direwolf set to ON"
				sudo touch /home/pi/CubeSatSim/command_control_direwolf
				sudo systemctl restart command
				if [[ $(arecord -l | grep card) ]]; then
					restart=1
				fi
			fi
	
		fi

		FILE=/home/pi/CubeSatSim/command_control
		if [ -f "$FILE" ]; then
			echo		
		else 
			echo "Radio command and control is OFF"
			echo
			echo "Do you want to set command and control to ON (y/n) "
			read reset
			echo
		
			if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
				echo "Command and control set to ON"
				sudo touch /home/pi/CubeSatSim/command_control
				echo "restarting command and control"
				sudo systemctl restart command
			fi		
		fi
	else
		reset="$param2"
		if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
			echo "Command and control Direwolf set to ON"
			sudo touch /home/pi/CubeSatSim/command_control_direwolf
			sudo systemctl restart command
			if [[ $(arecord -l | grep card) ]]; then
				restart=1
			fi
		else
			echo "Command and control Direwolf set to OFF"
			sudo rm /home/pi/CubeSatSim/command_control_direwolf > /dev/null 2>&1
			sudo systemctl restart command
		fi
	fi

elif [ "$1" = "-D" ]; then

	echo "Change Transmit Commands state"
	echo 

	FILE=/home/pi/CubeSatSim/transmit_dtmf
	if [ -f "$FILE" ]; then
    		echo "Transmit Commands in DTMF is set"
		echo
		echo "Do you want to Transmit Commands in APRS (y/n) "
		read reset
		echo

		if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
			echo "Transmit Commands in APRS"
			sudo rm /home/pi/CubeSatSim/transmit_dtmf > /dev/null 2>&1
		fi

	else 
    		echo "Transmit Commands in APRS is set"
		echo
		echo "Do you want to Transmit Commands in DTMF (y/n) "
		read reset
		echo

		if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
			echo "Transmit Commands in DTMF"
			touch /home/pi/CubeSatSim/transmit_dtmf
		fi
	fi

elif [ "$1" = "-R" ]; then

	echo "Reset the Commands Count in the file command_count.txt"
	echo 

	if [ -z "$param2" ] ; then

		echo "Current commands count is:"
		cat /home/pi/CubeSatSim/command_count.txt
		echo
	
		echo "Do you want to reset the commands count to zero (y/n) "
		read reset
		echo
	else
		reset="$param2"
	fi

	if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
		sudo rm /home/pi/CubeSatSim/command_count.txt > /dev/null 2>&1
		echo "Commands count reset to 0"
		echo -n "0" > /home/pi/CubeSatSim/command_count.txt
	else
		echo "Commands count not reset"
	fi

elif [ "$1" = "-B" ]; then

	echo "Manually setting Safe Mode (battery saver mode)"
	echo

	if [ -z "$param2" ] ; then
		FILE=/home/pi/CubeSatSim/battery_saver
		if [ -f "$FILE" ]; then
			echo "Safe Mode! Battery saver mode is ON."
			mode=1
	 	else 
			echo "Safe Mode is OFF."
	    		echo "Battery saver mode is OFF."
			mode=0
		fi
		
		echo
		
		echo "Do you want Safe Mode (battery saver mode) ON (y/n) "
		read saver
		echo
	else
		FILE=/home/pi/CubeSatSim/battery_saver
		if [ -f "$FILE" ]; then
			mode=1
	 	else 
			mode=0
		fi
		saver="$param2"
	fi

	if [ "$saver" = "y" ] || [ "$saver" = "yes" ]  ; then
		if [ "$mode" = "0" ] ; then
			echo "Safe Mode will be turned on! Battery saver will be turned ON"
			sudo touch /home/pi/CubeSatSim/battery_saver
			reboot=1
		fi
	else
		
		if [ "$mode" = "1" ] ; then 
			echo "Safe Mode will be turned OFF. Battery saver mode will be turned OFF"
			sudo rm /home/pi/CubeSatSim/battery_saver > /dev/null 2>&1
			reboot=1
		fi
	fi

	if [ "$reboot" = "1" ] ; then  
		value=`cat /home/pi/CubeSatSim/.mode`
		echo "$value" > /dev/null
		set -- $value
	
		if [ "$1" = "a" ] ||  [ "$1" = "s" ] ||  [ "$1" = "m" ] ; then
			reboot=1
##			echo "rebooting"
##			sudo reboot now
		else
			restart=1
##			echo "restarting"
##			sudo systemctl restart cubesatsim
		fi
	fi

elif [ "$1" = "-q" ]; then

	echo "Setting the Squelch setting"
	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	if [ -z "$param2" ] ; then

		echo
		echo "Current value of squelch is"	$6
		echo
		
	#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}
	
		echo -e "Enter squelch  (integer 1 - 8): "
	
		read sq 
	else
		sq="$param2"
	fi

	if [ -z $sq ] ; then
		sq="$6"
		echo "Keeping value of" $lat
	fi

	if ! [[ $sq =~ ^[+-]?[0-9]+([.][0-9]+)?$ ]] ; then
		echo "Error: not a number!"
		sq="$6"
		echo "Keeping value of" $sq
	fi 
	
#	echo
	echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"
#	echo
	echo $1 $2 $3 $4 $5 $sq $7 $8 $9 ${10} ${11} ${12} ${13} ${14}
	echo $1 $2 $3 $4 $4 $sq $7 $8 $9 ${10} ${11} ${12} ${13} ${14} > /home/pi/CubeSatSim/sim.cfg
	echo
	echo "Restarting CubeSatSim with new configuration file"
##	echo

#	reboot=1
##	sudo reboot now
	sudo systemctl restart transmit

elif [ "$1" = "-Q" ]; then

	echo "Reading current Squelch for 10 seconds"
	echo  "Squelch is active low (0 means squelch broken, received a signal)"	
	echo

	timeout 10 bash -c -- 'while true; do (gpio read 22 && sleep 1); done'

elif [ "$1" = "-P" ]; then

	echo "Setting the PL (Private Line) CTCSS/CDCSS setting"
	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	if [ -z "$param2" ] ; then

		echo
		echo "Current value of RX PL is" ${10}	
		echo "Current value of TX PL is" ${11}	
		echo
		
	#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}
	
		echo -e "Enter RX PL value integer 0: None, 01-38： CTCSS （analog, 39-121：CDCSS （digital）"
	
		read rxpl 
	
		if [ -z $rxpl ] ; then
	
			rxpl="${10}"
			echo "Keeping value of" $rxpl
		else
			restart=1
		fi
	
		if ! [[ $rxpl =~ ^[+-]?[0-9]+([.][0-9]+)?$ ]] ; then
	
			echo "Error: not a number!"
			rxpl="${10}"
			echo "Keeping value of" $rxpl
		else
			restart=1
		fi 
	
		echo -e "Enter TX PL value integer 0: None, 01-38： CTCSS （analog, 39-121：CDCSS （digital）"
	
		read txpl 
	
		if [ -z $txpl ] ; then
	
			txpl="${11}"
			echo "Keeping value of" $txpl
		fi
	
		if ! [[ $txpl =~ ^[+-]?[0-9]+([.][0-9]+)?$ ]] ; then
	
			echo "Error: not a number!"
			txpl="${11}"
			echo "Keeping value of" $txpl
		fi 

	else
		rxpl="$param2"
		echo "Changing RX PL to" $rxpl
		if ! [[ $rxpl =~ ^[+-]?[0-9]+([.][0-9]+)?$ ]] ; then
	
			echo "Error: not a number!"
			rxpl="${10}"
			echo "Keeping value of" $rxpl
		else
			restart=1
		fi 
		if [ -z "$param3" ] ; then
			txpl="${11}"
		else
			txpl="$param3"
			echo "Changing TX PL to" $txpl
			if ! [[ $txpl =~ ^[+-]?[0-9]+([.][0-9]+)?$ ]] ; then
				echo "Error: not a number!"
				txpl="${11}"
				echo "Keeping value of" $txpl
			fi 
		fi
	fi

#	echo
	echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"
#	echo
	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $rxpl $txpl ${12} ${13} ${14}
	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $rxpl $txpl ${12} ${13} ${14} > /home/pi/CubeSatSim/sim.cfg
	echo
##	echo "Rebooting CubeSatSim with new configuration file"
##	echo

	restart=1
##	sudo reboot now
#	sudo systemctl restart cubesatsim

elif [ "$1" = "-F" ]; then

	echo "Editing the TX and RX frequency"
	echo

	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	if [ -z "$param2" ] ; then

		echo "Current value of TX frequency is" $7 "MHz"	
		echo
	
	#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}

		echo "Return keeps current value."
	
		echo "Enter TX frequency in MHz (2m or 70cm band): "
		read tx
	
		if [ -z $tx ] ; then
	
			tx="$7"
			echo "Keeping value of" $tx "MHz"
		fi
		echo
		echo "Current value of RX frequency is" $8 "MHz"
		echo	
		echo "Enter RX frequency in MHz (70cm band only): "
		read rx
	
		if [ -z $rx ] ; then
	
			rx="$8"
			echo "Keeping RX value of" $rx "MHz"
		fi

	else
		tx="$param2"
		echo "Changing TX frequency to" $tx "MHz"
		if [ -z "$param3" ] ; then
			rx="$8"
			echo "Keeping RX value of" $rx "MHz"
		else
			rx="$param3"
			echo "Changing RX frequency to" $rx "MHz"
		fi
	fi

	echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"

	echo $1 $2 $3 $4 $5 $6 $tx $rx $9 ${10} ${11} ${12} ${13} ${14}
	echo $1 $2 $3 $4 $5 $6 $tx $rx $9 ${10} ${11} ${12} ${13} ${14} > /home/pi/CubeSatSim/sim.cfg

	rxf=$(echo "$rx * 1000000" | bc)
	txf=$(echo "$tx * 1000000" | bc)
	echo $txf $rxf
	echo $txf $rxf > /home/pi/CubeSatSim/frequency.txt

	echo
	echo "Restarting CubeSatSim with new configuration file"

#	if [[ $(sudo systemctl is-active gpsd.socket | grep inactive) ]]; then
		sudo systemctl restart transmit
#	else
#		echo
#		echo "temporarily disabling gpsd and rebooting to program FM module"
#		echo "this will take about 40 seconds"
#		sudo systemctl stop gpsd.socket
		sudo systemctl restart transmit
#		sleep 10
#		reboot=1
#	fi

elif [ "$1" = "-o" ]; then

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value
	MODE=$1

	if [ "$MODE" == "n" ]; then

		transmit_command "o"

	else
		echo	
		echo "Change telemetry beacon transmit state"
		echo 

		FILE=/home/pi/CubeSatSim/beacon_off
		if [ -f "$FILE" ]; then
			echo "Transmit beacon telemetry is off"
			echo
	
			echo "Turn beacon telemetry ON"
			sudo rm /home/pi/CubeSatSim/beacon_off > /dev/null 2>&1
			sudo systemctl restart transmit
		else 
			echo "Transmit beacon telemetry is on"
			echo
	
			echo "Turn beacon telemetry OFF"
			touch /home/pi/CubeSatSim/beacon_off
			sudo systemctl restart transmit
		fi
		sleep 3
	fi

elif [ "$1" = "-H" ]; then

	echo "Setting Balloon mode"
	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	if [ -z "$param2" ] ; then
		if [ "$9" = "yes" ] || [ "$9" = "y" ]; then
			echo "Balloon mode is ON"
		else
			echo "Balloon mode is OFF"
		fi
		
		echo
		
	#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} 
	
		echo "Do you want Balloon mode ON (y/n) "
		read hab
		echo
	else
		hab="$param2"
	fi
	if [ "$hab" = "y" ] || [ "$hab" = "yes" ]  ; then
		hab="yes"
		echo "Balloon mode is ON"
	else
		hab="no"
		echo "Balloon mode is OFF"
	fi
	
#	echo
	echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"
#	echo
	echo $1 $2 $3 $4 $5 $6 $7 $8 $hab ${10} ${11} ${12} ${13} ${14}
	echo $1 $2 $3 $4 $5 $6 $7 $8 $hab ${10} ${11} ${12} ${13} ${14} > /home/pi/CubeSatSim/sim.cfg
	echo
##	echo "Rebooting CubeSatSim with new configuration file"
##	echo

	reboot=1
##	sudo reboot now
#	sudo systemctl restart cubesatsim

elif [ "$1" = "-p" ]; then

	echo "Real-time output from the serial port from the Pico:"
	echo
#	sleep 2
	timeout 2 cat /dev/serial0 > /dev/null

	timeout 3 cat /dev/serial0

elif [ "$1" = "-v" ]; then

	echo "Real-time output from the INA219 voltage and current sensors:"
	echo
	/home/pi/CubeSatSim/venv.sh
	/home/pi/CubeSatSim/telem

elif [ "$1" = "-e" ]; then

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

	if [ "$1" == "n" ]; then

		transmit_command "e"

	else

		echo "changing CubeSatSim to Repeater mode"
		sudo echo "e" > /home/pi/CubeSatSim/.mode	
		if [ "$1" = "p" ] || [ "$1" = "P" ] ; then
			reboot=1
		else
			restart=1
		fi
	fi

elif [ "$1" = "-n" ]; then

#	echo "changing CubeSatSim to Transmit Commands mode"

	new=$2

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

	if [ "$1" = "n" ]; then
		echo "Turning Transmit Commands mode OFF"
		if [ "$new" = "a" ] || [ "$new" = "s" ] || [ "$new" = "m" ] ; then
			check_restart
			echo "Switching to mode "$new 
			sudo echo $new > /home/pi/CubeSatSim/.mode
		elif [ "$new" = "f" ] || [ "$new" = "b" ] || [ "$new" = "e" ] || [ "$new" = "j" ]  ; then
			echo "Switching to mode "$new 
			sudo echo $new > /home/pi/CubeSatSim/.mode
			restart=1
		elif  [ "$1" = "p" ] || [ "$1" = "P" ] ; then
			echo "Switching to Pacsat mode"
			sudo echo $new > /home/pi/CubeSatSim/.mode
			reboot=1
		else
			echo "Switching to BPSK mode"
			sudo echo "b" > /home/pi/CubeSatSim/.mode
			reboot=1
		fi
		sudo systemctl restart command
	else
		echo "Switching to Transmit Commands mode"
	
		value=`cat /home/pi/CubeSatSim/sim.cfg`
		echo "$value" > /dev/null
		set -- $value
		
		echo -n "TX Frequency is: "
		echo -n ${7}
		echo " MHz"
		echo

		check_restart
		sudo echo "n" > /home/pi/CubeSatSim/.mode
		sudo systemctl stop command
	fi

elif [ "$1" = "-A" ]; then

	echo "Transmit APRS Commands to control another CubeSatSim"
	echo

	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value
	
	echo -n "TX Frequency is: "
	echo -n ${7}
	echo " MHz"
	echo

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

	if [ "$1" == "f" ] || [ "$1" == "b" ]  || [ "$1" == "e" ] || [ "$1" == "j" ] ; then
		echo "The CubeSatSim/config -A command can only be run in APRS, SSTV, CW, or Transmit Commands modes."
		echo "Switch to one of these modes (a, s, m, or n) then re-run this command."
		exit
	fi

	if [ "$1" != "n" ]; then

		sudo systemctl stop cubesatsim
		sudo systemctl stop transmit
	
#		sudo python3 -u /home/pi/CubeSatSim/transmit.py x > /dev/null 2>&1 &  # Force APRS mode
	fi
	sudo systemctl stop command

	MODE="0"

	while [ "$MODE" != "x" ];
	do

		echo "Enter the mode to change: a=APRS, f=FSK, b=BPSK, s=SSTV, m=CW, e=Repeater, j=FUNcube, p=Pacsat, o=Beacon on/off x=Exit this mode"
		read MODE

		if [ "$MODE" != "x" ]; then
			transmit_command $MODE
		fi

		echo

	done

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

	if [ "$1" != "n" ] ; then
		sudo systemctl restart command
		reboot=1
	fi

elif [ "$1" = "-L" ]; then

	echo "MicVolume level for Command and Control and Pacsat"
	echo

	CARDUSB=$(aplay -l | grep "Device" | grep -oP '(?<=card )\d{1,2}')
	USBCARD=${CARDUSB:0:1}
	
	CARD=$(grep -oP '(?<=card )\d{1,2}' /etc/asound.conf)

	if [ -z $USBCARD ] ; then
		echo "No USB sound card detected"
	else
		echo "USB sound card is card" $USBCARD
		echo "asound.conf is card" $CARD
		if [ ! "$USBCARD" == "$CARD" ] ; then
			echo "Change card number in /etc/asound.conf"
			sudo sed -i "s/card $CARD/card $USBCARD/g" /etc/asound.conf
			echo "/etc/asound.conf has been updated to:"
			cat /etc/asound.conf
			echo "reinitializing alsa"
			sudo alsactl init 
		fi
	fi

	echo "Current MicVolume setting is:"
	value=`arecord -l | grep "card"` && echo "$value" > /dev/null && set -- $value && amixer -c $USBCARD sget 'MicVolume' 
	echo

	if [ -z "$param2" ] ; then
		echo -e "Enter MicVolume level (percent 0 - 100): "		
		read mic 
	else
		mic="$param2"
	fi

	if ! [ -z $mic ] &&  [[ $mic =~ ^[+]?[0-9]+([.][0-9]+)?$ ]] ; then
		echo "Updating mic level to" $mic "%"
		value=`arecord -l | grep "card"` && echo "$value" > /dev/null && set -- $value && amixer -c $USBCARD sset 'MicVolume' $mic%
	else
		echo "Not updating mic level"
	fi

elif [ "$1" = "-g" ]; then

	echo "Are you sure you want to reset the CubeSatSim configuration back to the default settings?"
	echo

	read rset
	echo
	
	if [ "$rset" = "y" ] || [ "$rset" = "yes" ]  ; then

		echo "Resetting"

		sudo systemctl stop cubesatsim
		sudo systemctl stop transmit
		sudo systemctl stop command
		sudo systemctl stop pacsatsim
		sudo systemctl stop frequency
		sudo systemctl stop pushbutton
		sudo systemctl stop OliveTin

		/home/pi/CubeSatSim/config -L 75

		echo "AMSAT 0 0 0 no 3 434.9 435 no 0 0 no 60 no" > /home/pi/CubeSatSim/sim.cfg

		echo "434900000 435000000" > /home/pi/CubeSatSim/frequency.txt
	
		sudo echo "b" > /home/pi/CubeSatSim/.mode
	
		sudo rm /home/pi/CubeSatSim/battery_saver > /dev/null 2>&1
	
		sudo rm /home/pi/CubeSatSim/command_control > /dev/null 2>&1
	
		sudo touch /home/pi/CubeSatSim/command_control_direwolf 
	
		sudo rm /home/pi/CubeSatSim/beacon_off > /dev/null 2>&1

		sudo rm /home/pi/CubeSatSim/command_count.txt
	
	    echo -n "0" > /home/pi/CubeSatSim/command_count.txt

		sudo rm /home/pi/CubeSatSim/failure_mode.txt > /dev/null 2>&1

		sudo rm /home/pi/CubeSatSim/transmit_dtmf > /dev/null 2>&1

		cp /home/pi/CubeSatSim/hotspot_ini /home/pi/CubeSatSim/hotspot.ini

		rpi-connect signout
	
		sudo mv -f /home/pi/CubeSatSim/telem.txt /home/pi/CubeSatSim/telem.txt.bk
	
		sudo journalctl --rotate
		sudo journalctl --vacuum-time=1s

		rm -rf ~/.config/chromium/Singleton* > /dev/null 2>&1

		cat /dev/null > ~/.bash_history && history -c

		sudo systemctl stop pacsatsim &>/dev/null
		sudo rm -r /home/pi/PacSat > /dev/null 2>&1

		sudo rm -r /home/pi/PacSatGround > /dev/null 2>&1

		sudo rm -r /home/pi/PacSatGroundLoop > /dev/null 2>&1

		sudo rm /home/pi/CubeSatSim/camera.jpg > /dev/null 2>&1

		if [ "$noreboot" = "0" ] ; then 
			echo "Shutting down."
			sudo shutdown now
		else
			echo
			echo "Shutdown or reboot to complete the operation."
			echo
		fi
	
#		reboot=1
	else

		echo "Not resetting"

	fi
elif [ "$1" = "-j" ]; then

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

	if [ "$1" == "n" ]; then

		transmit_command "j"

	else

		echo "changing CubeSatSim to FUNcube mode"
		sudo echo "j" > /home/pi/CubeSatSim/.mode
		restart=1
	fi

elif [ "$1" = "-M" ]; then

#	if [ $fail ]; then
#		MODE=$fail
#	else

	if [ -z "$param2" ] ; then
		FILE=/home/pi/CubeSatSim/failure_mode.txt
		if [ -f "$FILE" ]; then
			if [[ $(grep "\-1" $FILE) ]]; then
				echo "Currently, no simulated failure (0)"
			else
				fail=$(<$FILE)
				echo -n "Currently, simulated "
				case $fail in

				1)
					echo "+Y Solar Panel Unplugged (1)"
					;;			
				2)
					echo "+X Solar Panel Failure (2)"
					;;
				3)
					echo "-X Solar Panel Degredation (3)"
					;;
				4)
					echo "-Y Solar Panel Short Circuit (4)"
					;;
				5)
					echo "Failed I2C Bus 1 (5)"
					;;
				6)
					echo "Failed I2C Bus 3 (6)"
					;;
				7)
					echo "Failed Camera (7)"
					;;
				8)
					echo "Failed Payload (8)"
					;;
				9)
					echo "Failed BME Sensor (9)"
					;;
				"10")
					echo "Failed MPU Sensor (10)"
					;;
				"11")
					echo "Failed FM Audio (11)"
					;;
				*)		
					echo "Unknown Failure"
					;;
				esac
			fi
		else
			echo "Currently, no simulated failure"
		fi
	
		echo
		echo "Set simulated failure mode (or Return to turn OFF)"
		echo
	
		echo " 0  No Failure (turn OFF)"
		echo " 1  +Y Solar Panel Unplugged"
		echo " 2  +X Solar Panel Failure"
		echo " 3  -X Solar Panel Degredation"
		echo " 4  -Y Solar Panel Short Circuit"
		echo " 5  Failed I2C Bus 1"
		echo " 6  Failed I2C Bus 3"
		echo " 7  Failed Camera"
		echo " 8  Failed Payload"
		echo " 9  Failed BME Sensor"
		echo "10  Failed MPU Sensor"
		echo "11  Failed FM Audio"
		echo
	
		echo "Enter the failure number to set: 0 - 11" 
		read MODE
		echo
	else
		MODE="$param2"
#		echo "Setting Simulated Failure" $MODE
	fi

	if [ "$MODE" = "0" ]; then
		echo "Setting No Simulated Failure"
		MODE=-1
	else	
		case $MODE in
			1)
				echo "+Y Solar Panel Unplugged"
				;;			
			2)
				echo "Setting Simulated +X Solar Panel Failure"
				;;
			3)
				echo "Setting Simulated -X Solar Panel Degredation"
				;;
			4)
				echo "Setting Simulated -Y Solar Panel Short Circuit"
				;;
			5)
				echo "Setting Simulated Failed I2C Bus 1"
				;;
			6)
				echo "Setting Simulated Failed I2C Bus 3"
				;;
			7)
				echo "Setting Simulated Failed Camera"
				;;
			8)
				echo "Setting Simulated Failed Payload"
				;;
			9)
				echo "Setting Simulated Failed BME Sensor"
				;;
			"10")
				echo "Setting Simulated Failed MPU Sensor"
				;;
			"11")
				echo "Setting Failed FM Audio"
				;;
			*)
				echo "Setting No Simulated Failure"
				MODE=-1
				;;
		esac
	fi

#	echo $MODE
	echo
	sudo rm /home/pi/CubeSatSim/failure_mode.txt &>/dev/null
	echo $MODE > /home/pi/CubeSatSim/failure_mode.txt

	echo "Changing simulated failure mode to $MODE" | wall

elif [ "$1" = "-N" ]; then

	FILE=/home/pi/CubeSatSim/failure_mode.txt
	if [ -f "$FILE" ]; then
		if [[ $(grep "\-1" $FILE) ]]; then
			echo "No simulated failure"
			fail=0
		else	
			echo "Simulated failure mode"
			fail=$(<$FILE)
			echo $fail
		fi
	else
		echo "No simulated failure"
		fail=0
	fi

	if [ $fail == 0 ]; then
		echo "Changing to next mode"
	
		value=`cat /home/pi/CubeSatSim/.mode`
		echo "$value" > /dev/null
		set -- $value
	
		if [ "$1" = "a" ]; then
			echo "Current mode is APRS"
			echo "Next mode is FSK"
			/home/pi/CubeSatSim/config -f
		elif [ "$1" = "m" ]; then
			echo "Current mode is CW"
			echo "Next mode is FunCube"
			/home/pi/CubeSatSim/config -j
		elif [ "$1" = "f" ]; then
			echo "Current mode is FSK"
			echo "Next mode is BPSK"
			/home/pi/CubeSatSim/config -b
		elif [ "$1" = "b" ]; then
			echo "Current mode is BPSK"	
			echo "Next mode is SSTV"
			/home/pi/CubeSatSim/config -s
		elif [ "$1" = "s" ]; then
			echo "Current mode is SSTV"
			echo "Next mode is CW"
			/home/pi/CubeSatSim/config -m
		elif [ "$1" = "e" ]; then
			echo "Current mode is Repeater"	
			echo "Next mode is APRS"
			/home/pi/CubeSatSim/config -a
		elif [ "$1" = "j" ]; then
			echo "Current mode is FUNcube"	
			echo "Next mode is Repeater"
			/home/pi/CubeSatSim/config -e
		elif [ "$1" = "e" ]; then
			echo "Current mode is Repeater"	
			echo "Next mode is Pacsat"
			/home/pi/CubeSatSim/config -G
		elif [ "$1" = "p" ]; then
			echo "Current mode is Pacsat"	
			echo "Next mode is APRS"
			/home/pi/CubeSatSim/config -a
		else
			echo "Unknown mode"
		fi
	else
		echo "Changing to next simulated failure mode"
		fail=$((fail + 1))
#		if [ $fail == 12 ]; then
		if [ "$fail" -gt "11" ]; then
			fail=1
		fi
		echo $fail
		/home/pi/CubeSatSim/config -M $fail

	fi

elif [ "$1" = "-U" ]; then

	echo "Changing the Random Simulated Failure setting"
	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	if [ -z "$param2" ] ; then

		if [ "${12}" = "yes" ] || [ "${12}" = "y" ]; then
			echo "Random Simulated Failure is ON"
		else
			echo "Random Simulated Failure is OFF"
		fi
		
		echo
		
	#	$1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${12} ${13} ${14}
	
		echo "Do you want Random Simulated Failure ON (y/n) "
		read sim
		echo
	else
		sim="$param2"
	fi

	if [ "$sim" = "y" ] || [ "$sim" = "yes" ]  ; then
		sim="yes"
		echo "Random Simulated Failure is ON"
		echo
	

	    if [ -z "$param3" ] ; then 
	
			echo "A new random failure is selected every"
			echo  ${13} "seconds."	
			echo 
			echo "Enter a new value or Return keeps current value."
				
			echo "Enter time in seconds (integer): "
		
			read time
		else
			if [ "$sim" != "0" ] ; then
				time="$param3"
				echo "Set Random Simulated Failure Time to" $time
			else
				time=""
			fi
		fi
	
		if [ -z $time ] ; then
			time="${13}"
			echo "Keeping value of" $time "seconds"
		fi
	
		if ! [[ $time =~ ^[0-9]+$ ]] ; then
			echo "Error: not an integer!"
			time="${13}"
			echo "Keeping value of" $time
		fi
		
	else
		sim="no"
		echo "Random Simulated Failure is OFF"
		time="${13}"
#		echo "-1" > /home/pi/CubeSatSim/failure_mode.txt  # make sure to turn off any simulated failures
	fi
	
#	echo
	echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"
#	echo
	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} $sim $time ${14}
	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} $sim $time ${14} > /home/pi/CubeSatSim/sim.cfg
	echo

	if [ "${12}" != "$sim" ] || [ "${13}" != "$time" ] ; then
		restart=1
	fi

elif [ "$1" = "-u" ]; then

	echo "Change gpsd State"
	echo 
	echo "Note: this is just for a GPS module connected to J12 on the CubeSatSim Lite board."
	echo "If your GPS module is connected to J12 on the CubeSatSim Main board, always keep gpsd inactive."
	echo

	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value
	MODE=$1

	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value
	if [ "${14}" = "sim" ]  || [ "${14}" = "rig" ] ; then
		DOPPLER=1
	else
		DOPPLER=0
	fi

    if [ -z "$param2" ] ; then

		if [[ $(sudo systemctl is-active gpsd.socket | grep inactive) ]]; then
	    	echo "gpsd is inactive"

			if [ "$DOPPLER" = "0" ] ; then
				echo
				echo "Do you want to turn gpsd to ON (y/n) "
				read reset
				echo
		
				if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
					echo "gpsd set to ON"
					sudo systemctl enable gpsd
					sudo systemctl enable gpsd.socket
					echo
					echo "Here is the current gpsd configuration /etc/default/gpsd which will work with many GPS modules:"
					echo
		
					cat /etc/default/gpsd
		
					echo
					echo "To change this configuration edit the file with sudo nano /etc/default/gpsd then restart gpsd."
					echo
					echo "To monitor your gps device, use the gpsmon command."
					echo
					reboot=1
				fi
			else
				echo
				echo "gpsd cannot be turned on with Doppler mode. "
				echo "To turn on gpsd, first turn off Doppler mode."
				echo	
			fi
	
		else 
	    	echo "gpsd is active"
			echo
			echo "Do you want to turn gpsd to OFF (y/n) "
			read reset
			echo
	
			if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
				echo "gpsd set to OFF"
				sudo systemctl disable gpsd
				sudo systemctl disable gpsd.socket
				reboot=1
			fi
	
		fi
	else
		reset="$param2"
		if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
				if [ "$DOPPLER" = "0" ] ; then
					echo "gpsd set to ON"
					sudo systemctl enable gpsd
					sudo systemctl enable gpsd.socket
					echo
					echo "Here is the current gpsd configuration /etc/default/gpsd which will work with many GPS modules:"
					echo
		
					cat /etc/default/gpsd
		
					echo
					echo "To change this configuration edit the file with sudo nano /etc/default/gpsd then restart gpsd."
					echo
					echo "To monitor your gps device, use the gpsmon command."
					echo
					reboot=1
				else
					echo
					echo "gpsd cannot be turned on with Doppler mode. "
					echo "To turn on gpsd, first turn off Doppler mode."
					echo	
					sudo systemctl disable gpsd
					sudo systemctl disable gpsd.socket
				fi
		
		else
			echo "gpsd set to OFF"
			sudo systemctl disable gpsd
			sudo systemctl disable gpsd.socket
			reboot=1
		fi
	fi

elif [ "$1" = "-G" ]; then

	echo "Changing to Pacsat mode"
	value=`cat /home/pi/CubeSatSim/.mode`
	echo "$value" > /dev/null
	set -- $value

#	echo "changing CubeSatSim to Pacsat mode"

	reboot=1
	sudo echo "p" > /home/pi/CubeSatSim/.mode	

	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value
	
	if [ "${7}" != "434.9" ] && [ "${7}" != "434.900" ] ; then
		echo "The default transmit frequency of 434.9 MHz is NOT set."
		echo
		echo "To change the transmit frequency (for example to 434.9 MHz) use the CubeSatSim/config -F command."
		sleep 5

#		read input
#		echo
#
#		if [ "$input" = "y" ] || [ "$input" = "yes" ]  ; then
#			/home/pi/CubeSatSim/config -F n
#		fi
	fi

elif [ "$1" = "-I" ]; then

	echo "Changing to Pacsat Ground Station mode"
	echo
	echo "Run the Pacsat Ground Station in the Desktop"
	value=`cat /home/pi/CubeSatSim/.mode`	
	echo "$value" > /dev/null
	set -- $value

#	echo "changing CubeSatSim to Pacsat mode"

#	reboot=1
	check_restart
	sudo echo "P" > /home/pi/CubeSatSim/.mode	

	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	if [ "${7}" = "434.9" ] && [ "${8}" = "435" ]; then
		echo "The default transmit frequency of 434.9 MHz is set"
		echo
		echo "To change it (for example to 435 MHz) use the CubeSatSim/config -F command."

#		read input
#		echo
#
#		if [ "$input" = "y" ] || [ "$input" = "yes" ]  ; then
#			/home/pi/CubeSatSim/config -F n
#		fi
	fi

elif [ "$1" = "-k" ]; then

	echo "Reset Pacsat configuration"
	echo
	sudo systemctl stop pacsatsim &>/dev/null
	sudo rm -r /home/pi/PacSat
	reboot=1
#	sudo systemctl restart pacsatsim

elif [ "$1" = "-K" ]; then

	echo "Setting the Doppler frequency shift mode setting"
	echo

	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	if [ -z "$param2" ] ; then

		doppler="${14}"
	
		if [ "${14}" = "sim" ] ; then
			echo "Simulated Doppler frequency shift mode enabled"
		elif [ "${14}" = "rig" ] ; then
			echo "Radio Control Doppler frequency shift mode enabled"
		else
			echo "Doppler frequency shift is OFF"
		fi
		
		echo
		
	#	$1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${12} ${13} ${14}
	
		echo "Do you want Doppler frequency shift ON (y/n) "
		read sim
		echo
		
		if [ "$sim" = "y" ] || [ "$sim" = "yes" ]  ; then
			doppler="yes"
			echo "Do you want Simulated Doppler shift (sim) or Radio Control Doppler (rig)"
			echo "Enter sim or rig"
			read doppler
	
			if [ "$doppler" = "sim" ] ; then
				echo "Simulated Doppler frequency shift mode enabled"
			elif [ "$doppler" = "rig" ] ; then
				echo "Radio Control Doppler frequency shift mode enabled"
			else
				doppler="no"
				echo "sim or rig not entered. Doppler frequency shift is OFF"
			fi
		else
			doppler="no"
			echo " Doppler frequency shift is OFF"
		fi
	else
		doppler="$param2"
		if [ "$doppler" = "sim" ] ; then
			echo "Simulated Doppler frequency shift mode enabled"
		elif [ "$doppler" = "rig" ] ; then
			echo "Radio Control Doppler frequency shift mode enabled"
		else
			echo "Doppler frequency shift is OFF"
			doppler="no"
		fi
	fi

#	echo
	echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"
#	echo
	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} $doppler
	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} $doppler > /home/pi/CubeSatSim/sim.cfg
	echo

	value=`cat /home/pi/CubeSatSim/.mode`	
	echo "$value" > /dev/null
	set -- $value

#	if [[ ( "$doppler" == "y" || "$doppler" == "yes" ) && ( "$1" == "m" ) && ( "$doppler" == "no" ) ]] ; then
#		reboot=1
#	else
		restart=1
#	fi

elif [ "$1" = "-V" ]; then

	echo "Changing the maxcpus setting"
	echo

	if [[ $(grep 'bullseye' /etc/os-release) ]]; then
		echo "Pi OS is Bullseye (Debian 11)"
		CMDLINE=/boot/cmdline.txt
	elif [[ $(grep 'bookworm' /etc/os-release) ]]; then
		echo "Pi OS is Bookworm (Debian 12)"
		CMDLINE=/boot/firmware/cmdline.txt
	else
		echo "Pi OS is Trixie (Debian 13) or later"
		CMDLINE=/boot/firmware/cmdline.txt
	fi

	CPUS=$(cat $CMDLINE| grep -oP 'maxcpus=\K\d+')

	if [ -z "$param2" ] ; then

		if [ -z "$CPUS" ] ; then
			echo "No current maxcpus is set"
		else
			echo "Current maxcpus="$CPUS
		fi
		echo
		echo "How many CPUs do you want to set (1-3) ?"
		echo "Return keeps current value."
		echo "0 removes any maxcpus setting"

		NEWCPU="-1"
		read NEWCPU

		if [ -z $NEWCPU ] ; then
			echo "No change to maxcpus"
		elif [ $NEWCPU == "0" ] ; then
			echo "Removing maxcpus setting"
			sudo sed -i "s/maxcpus=$CPUS //g" $CMDLINE
			echo "This change will happen after you reboot"
		elif [ $NEWCPU == "1" ] || [ $NEWCPU == "2" ] || [ $NEWCPU == "3" ] ; then 
			echo "Setting maxcpus="$NEWCPU	
			if [ -z "$CPUS" ] ; then
				sudo sed -i "s/console=tty1 r/console=tty1 maxcpus=$NEWCPU r/g" $CMDLINE
			else
				sudo sed -i "s/maxcpus=$CPUS/maxcpus=$NEWCPU/g" $CMDLINE
			fi
			echo "This change will happen after you reboot"
		else
			echo "Invalid maxcpus setting. No change made."
		fi

	else

		if [ "$param2" == "1" ] || [ "$param2" == "2" ] || [ "$param2" == "3" ] ; then		
			echo "Setting maxcpus="$param2
			if [ -z "$CPUS" ] ; then
				sudo sed -i "s/console=tty1 r/console=tty1 maxcpus=$param2 r/g" $CMDLINE
			else
				sudo sed -i "s/maxcpus=$CPUS/maxcpus=$param2/g" $CMDLINE
			fi
			echo "This change will happen after you reboot"
		elif [ "$param2" == "0" ] ; then
			echo "Removing maxcpus setting"
			sudo sed -i "s/maxcpus=$CPUS //g" $CMDLINE
			echo "This change will happen after you reboot"
		else
			echo "invalid maxcpus value. No change mode."
		fi
	fi

elif [ "$1" = "-w" ]; then

	echo "Showing PiOS Boot Files"
	echo
	if [[ $(grep 'bullseye' /etc/os-release) ]]; then
		echo "Pi OS is Bullseye (Debian 11)"
		CMDLINE=/boot/cmdline.txt
		CONFIG=/boot/config.txt
	elif [[ $(grep 'bookworm' /etc/os-release) ]]; then
		echo "Pi OS is Bookworm (Debian 12)"
		CMDLINE=/boot/firmware/cmdline.txt
		CONFIG=/boot/firmware/config.txt
	elif [[ $(grep 'trixie' /etc/os-release) ]]; then
		echo "Pi OS is Trixie (Debian 13)"
		CMDLINE=/boot/firmware/cmdline.txt
		CONFIG=/boot/firmware/config.txt
	fi

	echo
	echo "cat" $CMDLINE
	echo
	echo
	cat $CMDLINE
	echo
	echo "cat" $CONFIG
	echo
	cat $CONFIG
	echo

elif [ "$1" = "-z" ]; then

	if [[ $(grep 'bullseye' /etc/os-release) ]]; then
		echo "Pi OS is Bullseye (Debian 11)"
	  	raspistill -n -o /home/pi/CubeSatSim/camera.jpg -w 86 -h 64 -q 70 &>/dev/null
	elif [[ $(grep 'bookworm' /etc/os-release) ]]; then
		echo "Pi OS is Bookworm (Debian 12)"
	  	rpicam-still -n -o /home/pi/CubeSatSim/camera.jpg --width 86 --height 64 -q 70 &>/dev/null
	elif [[ $(grep 'trixie' /etc/os-release) ]]; then
		echo "Pi OS is Trixie (Debian 13)"
	  	rpicam-still -n -o /home/pi/CubeSatSim/camera.jpg --width 86 --height 64 -q 70 &>/dev/null
	fi
	
	echo "Taking camera image and copying to pacsat img directory"
	cp /home/pi/CubeSatSim/camera.jpg /home/pi/PacSat/pacsat/img/camera.jpg

elif [ "$1" = "-h" ]; then

	echo "config OPTION"
	echo
	echo "Changes CubeSatSim mode, resets, or modifies configuration file"
	echo 
	echo "   -h     This help info"
	echo "   -a     Change to AFSK/APRS mode"
	echo "   -m     Change to CW mode"
	echo "   -f     Change to FSK/DUV mode"
	echo "   -b     Change to BPSK mode"
	echo "   -s     Change to SSTV mode"
	echo "   -e     Change to Repeater mode"
	echo "   -j     Change to FUNcube mode"
	echo "   -G     Change to Pacsat mode"
	echo "   -I     Change to Pacsat Ground Station mode"
	echo "   -n     Change to Transmit Commands mode"
	echo "   -i     Restart CubeSatsim software"
	echo "   -c     Change the CALLSIGN in the configuration file sim.cfg"
	echo "   -t     Change the Simulated Telemetry setting in sim.cfg"
	echo "   -r     Change the Resets Count in the configuration file sim.cfg"
	echo "   -l     Change the Latitude and Longitude in the configuration file sim.cfg"
	echo "   -S     Scan both I2C buses on the Raspberry Pi"
	echo "   -C     Clear logs and reboot"
	echo "   -T     Change command and control state"
	echo "   -d     Change command and control Direwolf state"
	echo "   -R     Change the Commands Count in the file command_count.txt"
	echo "   -B     Change Safe Mode (battery saver mode) manually"
	echo "   -q     Change the Squelch setting for command receiver"
    echo "   -Q     Read the current Squelch for 10 seconds"
	echo "   -F     Change the RX and TX frequency"
	echo "   -H     Change the Balloon (HAB) mode"
	echo "   -p     Display payload sensor data for 3 seconds"
	echo "   -v     Display voltage and current data"
	echo "   -P     Change the PL (Private Line) CTCSS/CDCSS codes for FM RX and TX"
	echo "   -A     Transmit APRS control packets to control another CubeSatSim"
	echo "   -D     Change Transmit Commands state APRS or DTMF"
	echo "   -o     Change telemetry beacon transmit state"
	echo "   -L     Change microphone level for command and control"
	echo "   -g     Reset configuration back to default settings"
	echo "   -M     Set simulated failure mode"
	echo "   -U     Change the random failure mode setting"
	echo "   -N     Set next mode or failure"
	echo "   -u     Change gpsd state"
	echo "   -k     Reset the Pacsat Configuration"
	echo "   -K     Change the Doppler frequency shift setting"
	echo "   -V     Set the maximum CPUs"
	echo "   -w     Show PiOS boot files"
	echo "   -z     Take camera image for PacSat"

	echo
	exit

else
#	echo
	echo "Unknown option.  Try config -h for a list of supported options."
	echo

fi

# sudo systemctl restart cubesatsim

# echo "Checking for reboot or restart"

# echo $noreboot

#reboot=0
#restart=1

if [ "$reboot" = "1" ] ; then 
	if [ "$noreboot" = "0" ] ; then 
		echo 'Reboot due to config change!' | wall
		echo "Rebooting"
		sudo systemctl stop transmit &>/dev/null
		sudo reboot now
	else
		echo "Reboot needed for changes to take effect"
	fi
fi

if [ "$restart" = "1" ] ; then  
	if [ "$reboot" = "0" ] ; then 
		echo "Restarting"
#		sudo systemctl stop transmit
		sudo systemctl restart cubesatsim &>/dev/null
		sudo systemctl restart transmit &>/dev/null
	else
		echo "Restart needed for changes to take effect"
	fi
fi
