#!/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.2 configuration tool"
echo
# echo $1
# echo $2
# echo

sudo modprobe snd-aloop

# if [ "$2" = "-n" ] ; then
if [ -z "$2" ] ; then
	noreboot=0
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") ]] ; then
		echo "Doppler frequency shift is ON but not active in this mode (only APRS, CW, SSTV, 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 "rigctld Doppler frequency shift enabled"
		echo -n "Current TX and RX Frequency (Hz) is: "
		cat /home/pi/CubeSatSim/frequency.txt
		echo
	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

	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	
	echo "Editing the Simulated Telemetry setting in"
	echo  "the configuration file for CubeSatSim"	
	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	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
	
	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
##	echo "Rebooting CubeSatSim with new configuration file"
##	echo

	reboot=1
##	sudo reboot now
#	sudo  restart cubesatsim
	
elif [ "$1" = "-c" ]; then

	echo	
	echo "Editing the CALLSIGN in the"
	echo  "configuration file for CubeSatSim"	
	echo
	echo "Return keeps current value."
#	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"	
	echo $oldcallsign
	echo
	
#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${12} ${13} ${14}

	echo "Enter callsign in all capitals: "
	read callsign

	if [ -z $callsign ] ; then

		callsign="$1"
		echo "Keeping value of" $callsign
		norestart=1
	else
	
		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
	
	if [ "$norestart" = "1" ]; then
		echo 
	else
		echo
##		echo "Rebooting CubeSatSim with new configuration file"
##		echo
		reboot=1
##		sudo reboot now
#		sudo systemctl restart cubesatsim
	fi
	
elif [ "$1" = "-r" ]; then

	echo	
	echo "Editing the Reset Count in the"
	echo  "configuration file for CubeSatSim"	
	echo
	echo "Return keeps current value."
#	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"	
	echo $2
	echo
	
#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}

	echo -e "Enter Reset Count (integer): "

	read resets

	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
##		echo
##		echo "Rebooting CubeSatSim with new configuration file"
##		echo
		reboot=1
##		sudo reboot now
#		sudo systemctl restart cubesatsim
	fi
	
elif [ "$1" = "-l" ]; then

	echo	
	echo "Editing latitude and longitude in the"
	echo "configuration file for CubeSatSim"
	echo "(Only used for APRS telemetry)"
	echo
	echo "Return keeps current value."
#	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"	
	echo $3
	echo
	
#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}

	echo -e "Enter latitude  (decimal degrees, positive is north): "

	read lat 

	if [ -z $lat ] ; then

		lat="$3"
		echo "Keeping value of" $lat
	fi

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

		echo "Error: not a number!"
		lat="$3"
		echo "Keeping value of" $lat
	fi 
	
	echo
	echo "Current value of longitude is"	
	echo $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

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

		echo "Error: not a number!"
		long="$4"
		echo "Keeping value of" $long
	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
##		echo
##		echo "Rebooting CubeSatSim with new configuration file"
##		echo
		reboot=1
##		sudo reboot now
#		sudo systemctl restart cubesatsim
	fi

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

	echo	
	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	
	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 

	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

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

	echo	
	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

	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
#			reboot=1
##			echo "rebooting"
			sudo systemctl restart command
##			sudo reboot now	
		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
##			echo "rebooting"
#			reboot=1
			sudo systemctl restart command
			if [[ $(arecord -l | grep card) ]]; then
				restart=1
###				echo "restarting cubesatsim software"
###				sudo systemctl restart cubesatsim
			fi
##			sudo reboot now	
		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"
#			reboot=1
			sudo systemctl restart command
##			sudo reboot now	
		fi

	fi

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

	echo	
	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	
	echo "Reset the Commands Count in the file command_count.txt"
	echo 

	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
	
	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 "0" > /home/pi/CubeSatSim/command_count.txt
	else
		echo "Commands count not reset"
	fi

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

	echo	
	echo "Manually setting Safe Mode (battery saver mode)"
	echo
	
	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

##	reboot=0

	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	
	echo "Editing the Squelch setting in"
	echo  "the configuration file for CubeSatSim"	
	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	echo
	echo "Current value of squelch is"	
	echo $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 

	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	
	echo "Reading current Squelch for 10 seconds"
	echo  "Squelch is active low (0 means squelch broken)"	
	echo

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

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

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

	echo
	echo "Current value of RX PL is"	
	echo ${10}
	echo "Current value of TX PL is"	
	echo ${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 
	
#	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	
	echo "Editing the tx and rx frequency in the"
	echo  "configuration file for CubeSatSim"	
	echo
	echo "Return keeps current value."
#	echo -e "Current sim.cfg configuration file:"	
#	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	echo "Current value of tx is"	
	echo $7
	echo
	echo "Current value of rx is"	
	echo $8
	echo	

#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14}

	echo "Enter tx frequency as 4XX.XXXX: "
	read tx

	if [ -z $tx ] ; then

		tx="$7"
		echo "Keeping value of" $tx
	fi

	echo "Enter rx frequency as 4XX.XXXX: "
	read rx

	if [ -z $rx ] ; then

		rx="$8"
		echo "Keeping value of" $rx
	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 "$8 * 1000000" | bc)
	txf=$(echo "$7 * 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	
	echo "Editing the Balloon mode setting in"
	echo  "the configuration file for CubeSatSim"	
	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	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
	
	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/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	
	echo "Setting microphone level for command and control"
	echo

	echo -e "Enter microphone level in percentage  (integer 0 - 100): "

	read mic 

	if ! [ -z $mic ] &&  [[ $mic =~ ^[+-]?[0-9]+([.][0-9]+)?$ ]] ; then
		echo "Updating mic level"
		value=`arecord -l | grep "card"` && echo "$value" > /dev/null && set -- $value && amixer -c ${2:0:1} set Mic $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"

		echo "AMSAT 0 0 0 no 3 434.9 435 no 0 0 no 60 no" > /home/pi/CubeSatSim/sim.cfg
	
		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 echo "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

		sudo systemctl stop cubesatsim
		sudo systemctl stop transmit
		sudo systemctl stop command
	
		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

		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

		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
	fi

	if [ "$MODE" = "0" ]; then
		echo "Setting No Simulated Failure"
		MODE=-1
#    elif [ "$MODE" = "12" ]; then
	
#		if [ "$norestart" = "1" ]; then
#			echo
#		else
#			reboot=1
#		fi

	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
	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
		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	
	echo "Changing the Random Simulated Failure setting in"
	echo "the configuration file for CubeSatSim"	
	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value

	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
	
	if [ "$sim" = "y" ] || [ "$sim" = "yes" ]  ; then
		sim="yes"
		echo "Random Simulated Failure is ON"
		echo	
		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
	
		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
		reboot=1
	fi

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

	echo	
	echo "Change gpsd state"
	echo 

	if [[ $(sudo systemctl is-active gpsd.socket | grep inactive) ]]; then
    	echo "gpsd is inactive"
		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 "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

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

	echo "Changing to PacSatSim 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
	echo "Reset PacSatSim 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	
	echo "Editing the Doppler frequency shift setting in"
	echo  "the configuration file for CubeSatSim"	
	echo
	
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value
	doppler="${14}"

	if [ "${14}" = "sim" ] ; then
		echo "Simulated Doppler frequency shift mode enabled"
	elif [ "${14}" = "rig" ] ; then
		echo "rigctld 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 or rigctld Doppler (sim/rig)"
		read doppler

		if [ "$doppler" = "sim" ] ; then
			echo "Simulated Doppler frequency shift mode enabled"
		elif [ "$doppler" = "rig" ] ; then
			echo "rigctld 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
	
#	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" = "-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 "   -j     Change to FUNcube mode"
  	echo "   -n     Change to Transmit Commands mode"
	echo "   -e     Change to Repeater mode"
	echo "   -j     Change to FUNcube mode"
	echo "   -G     Change to PacSatSim mode"
	echo "   -I     Change to PacSat Ground Station 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 PacSatSim Configuration"
	echo "   -K     Change the Doppler frequency shift setting"

	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
		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
		sudo systemctl restart transmit
	else
		echo "Restart needed for changes to take effect"
	fi
fi
