#!/bin/bash

function transmit_command_aprs {

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

	 	echo "Stopping command and control"
		sudo systemctl stop command

		echo "Transmit DTMF start"
		gpio write 28 0 # ptt
		gpio write 2 1 # tx LED
		timeout 3 direwolf -c /home/pi/CubeSatSim/direwolf/direwolf-transmit-dtmf-aprs.conf  -t 0l 
		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="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=a"
		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 APRS "
		echo $STRING
		sudo touch /home/pi/CubeSatSim/ready
		sleep 3

	fi

	exit
}

function transmit_command_fsk {

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

	 	echo "Stopping command and control"
		sudo systemctl stop command

		echo "Transmit DTMF start"
		gpio write 28 0 # ptt
		gpio write 2 1 # tx LED
		timeout 3 direwolf -c /home/pi/CubeSatSim/direwolf/direwolf-transmit-dtmf-fsk.conf  -t 0l 
		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="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=f"
		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 FSK"
		echo $STRING
		sudo touch /home/pi/CubeSatSim/ready
		sleep 3

	fi

	exit
}

function transmit_command_bpsk {

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

	 	echo "Stopping command and control"
		sudo systemctl stop command

		echo "Transmit DTMF start"
		gpio write 28 0 # ptt
		gpio write 2 1 # tx LED
		timeout 3 direwolf -c /home/pi/CubeSatSim/direwolf/direwolf-transmit-dtmf-bpsk.conf  -t 0l 
		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="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=b"
		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 BPSK"
		echo $STRING
		sudo touch /home/pi/CubeSatSim/ready
		sleep 3

	fi

	exit
}

function transmit_command_sstv {

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

	 	echo "Stopping command and control"
		sudo systemctl stop command

		echo "Transmit DTMF start"
		gpio write 28 0 # ptt
		gpio write 2 1 # tx LED
		timeout 3 direwolf -c /home/pi/CubeSatSim/direwolf/direwolf-transmit-dtmf-sstv.conf  -t 0l 
		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="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=s"
		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 SSTV"
		echo $STRING
		sudo touch /home/pi/CubeSatSim/ready
		sleep 3

	fi

	exit
}

function transmit_command_cw {

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

	 	echo "Stopping command and control"
		sudo systemctl stop command

		echo "Transmit DTMF start"
		gpio write 28 0 # ptt
		gpio write 2 1 # tx LED
		timeout 3 direwolf -c /home/pi/CubeSatSim/direwolf/direwolf-transmit-dtmf-cw.conf  -t 0l 
		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="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=m"
		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 CW"
		echo $STRING
		sudo touch /home/pi/CubeSatSim/ready
		sleep 3

	fi

	exit
}

function transmit_command_beacon {

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

	 	echo "Stopping command and control"
		sudo systemctl stop command

		echo "Transmit DTMF start"
		gpio write 28 0 # ptt
		gpio write 2 1 # tx LED
		timeout 3 direwolf -c /home/pi/CubeSatSim/direwolf/direwolf-transmit-dtmf-beacon.conf  -t 0l 
		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="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=o"
		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 toggle Beacon"
		echo $STRING
		sudo touch /home/pi/CubeSatSim/ready
		sleep 3

	fi

	exit
}

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

sudo modprobe snd-aloop

# if [ "$2" = "n" ] ; then
if [ -z "$2" ] ; then
	noreboot=0
else
	noreboot=1
	echo "Reboot disabled"
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

	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" = "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
	else
		echo 
	fi
	echo
	value=`cat /home/pi/CubeSatSim/sim.cfg`
	echo "$value" > /dev/null
	set -- $value
	
	if [ "$5" = "y" ] || [ "$5" = "yes" ] ; then
#		sim="yes"
		echo "Simulated Telemetry is ON"
	else
#		sim="no"
		echo "Simulated Telemetry is OFF"
	fi
	echo

	if [ "$9" = "yes" ] || [ "$9" = "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 -n "Squelch level is: "
	echo $6
	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
				echo "Radio carrier command and control is ON"
			fi
		else
			echo "Radio carrier command and control is ON"
		fi
	else 
    		echo "Radio command and control is OFF"
	fi

	echo

	echo -n "RX PL code is: "
	echo -n ${10}
#	echo
	echo -n " TX PL code is: "
	echo ${11}
	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

	echo
	echo -e "Current sim.cfg configuration file:"	
#	echo
	
	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11}
	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_aprs

	else

		echo "changing CubeSatSim to AFSK mode"
		sudo echo "a" > /home/pi/CubeSatSim/.mode	
		if [ "$1" == "f" ] || [ "$1" == "b" ]  || [ "$1" == "e" ] ; then
			FILE=/home/pi/CubeSatSim/battery_saver
			if [ -f "$FILE" ]; then
				restart=1
			else 
		    		reboot=1
			fi	
		else
			restart=1
		fi	
	fi

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

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

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

		transmit_command_cw

	else

		echo "changing CubeSatSim to CW mode"
		sudo echo "m" > /home/pi/CubeSatSim/.mode
		if [ "$1" == "f" ] || [ "$1" == "b" ]  || [ "$1" == "e" ] ; then
			FILE=/home/pi/CubeSatSim/battery_saver
			if [ -f "$FILE" ]; then
				restart=1
			else 
		    		reboot=1
			fi	
		else
			restart=1
		fi
	fi
 
elif [ "$1" = "-f" ]; then

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

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

		transmit_command_fsk

	else

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

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

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

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

		transmit_command_bpsk

	else

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

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

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

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

		transmit_command_sstv

	else

		echo "changing CubeSatSim to SSTV mode"
		sudo echo "s" > /home/pi/CubeSatSim/.mode
		if [ "$1" == "f" ] || [ "$1" == "b" ]  || [ "$1" == "e" ] ; then

			FILE=/home/pi/CubeSatSim/battery_saver
			if [ -f "$FILE" ]; then
				restart=1
			else 
		    		reboot=1
			fi		
		else
			restart=1
		fi
	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 "Simualted Telemetry is ON"
	else
		echo "Simualted Telemetry is OFF"
	fi
	
	echo
	
#	$1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} 

	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"
	fi
	
#	echo
	echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"
#	echo
	echo $1 $2 $3 $4 $sim $6 $7 $8 $9 ${10} 
	echo $1 $2 $3 $4 $sim $6 $7 $8 $9 ${10}  > /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

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

	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} 
		echo $callsign $2 $3 $4 $5 $6 $7 $8 $9 ${10}  > /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} 

	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} 
		echo $1 $resets $3 $4 $5 $6 $7 $8 $9 ${10}  > /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} 

	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}
	echo $1 $2 $lat $long $5 $6 $7 $8 $9 ${10} ${11} > /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 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	
	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 "Command and control set to OFF"
			sudo rm /home/pi/CubeSatSim/command_control > /dev/null 2>&1
#			reboot=1
			echo "restarting command and control"
			sudo systemctl restart command
##			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
			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 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/commands_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\n" > /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}

	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}
	echo $1 $2 $3 $4 $4 $sq $7 $8 $9 ${10} ${11} > /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}

	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
	echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $rxpl $txpl > /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" = "-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}

	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
#	else
	
		echo -e "\nCubeSatSim configuration sim.cfg file updated to: \n"

		echo $1 $2 $3 $4 $5 $6 $tx $rx $9 ${10} ${11}
		echo $1 $2 $3 $4 $5 $6 $tx $rx $9 ${10} ${11} > /home/pi/CubeSatSim/sim.cfg
#	fi
	
#	if [ "$norestart" = "1" ]; then
#		echo 
#	else
		echo
		echo "Restarting CubeSatSim with new configuration file"
##		echo
#		reboot=1
##		sudo reboot now
		sudo systemctl restart transmit
#	fi

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

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

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

		transmit_command_beacon

	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 "Do you want to turn beacon telemetry ON (y/n) "
#			read reset

			reset="y"
			echo
	
			if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
				echo "Turn beacon telemetry ON"
				sudo rm /home/pi/CubeSatSim/beacon_off > /dev/null 2>&1
				sudo systemctl restart transmit
	#			restart=1
			fi
	
		else 
	    		echo "Transmit beacon telemetry is on"
#			echo
#			echo "Do you want to turn beacon telemetry OFF (y/n) "
#			read reset
			reset="y"
			echo
	
			if [ "$reset" = "y" ] || [ "$reset" = "yes" ]  ; then
				echo "Turn beacon telemetry OFF"
				touch /home/pi/CubeSatSim/beacon_off
				sudo systemctl restart transmit
	#			restart=1
			fi
		fi
	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}

	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}
	echo $1 $2 $3 $4 $5 $6 $7 $8 $hab ${10} ${11} > /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

	echo "changing CubeSatSim to Repeater mode"
	sudo echo "e" > /home/pi/CubeSatSim/.mode

	restart=1

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

	echo "changing CubeSatSim to Transmit Commands mode"

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

	if [ "$1" = "n" ]; then
		echo "Turning Transmit Command and Control mode OFF"
		echo "Switching to BPSK mode"
		sudo echo "b" > /home/pi/CubeSatSim/.mode
		reboot=1
	else
		echo "Turning Transmit Command and Control mode ON"
		sudo echo "n" > /home/pi/CubeSatSim/.mode
		restart=1
	fi

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

	echo "Transmit APRS control packets to control another CubeSatSim"
	echo

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

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

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

	sim="y"

	while [ "$sim" = "y" ]
	do

		echo "Enter the mode number to change: 1=APRS, 2=FSK, 3=BPSK, 4=SSTV, 5=CW"
		read MODE
	
		case $MODE in
	
			1)
				echo "Mode 1 is APRS"
				STRING="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=a"
				;;
			2)
				echo "Mode 2 is FSK"
				STRING="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=f"
				;;
			3)
				echo "Mode 3 is BPSK"
				STRING="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=b"
				;;
			4)
				echo "Mode 4 is SSTV"
				STRING="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=s"
				;;
			5)
				echo "Mode 5 is CW"
				STRING="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=m"
				;;
			*)
				echo "Unknown mode"
				STRING="AMSAT-11>APCSS:=3901.40N\07704.39WShi hi MODE=?"
				;;
		esac
		sudo rm /home/pi/CubeSatSim/t.txt > /dev/null 2>&1
		echo $STRING > /home/pi/CubeSatSim/t.txt
		echo
		echo -n "Sending APRS packet "
		echo $STRING
		sudo touch /home/pi/CubeSatSim/ready
		sleep 5
		sudo touch /home/pi/CubeSatSim/ready

		echo
		echo "Do you want to send another APRS command packet (y/n) "
		read sim
		echo
	done
	sudo rm /home/pi/CubeSatSim/t.txt > /dev/null 2>&1
#	sudo systemctl restart cubesatsim

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

		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" > /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 rm /home/pi/CubeSatSim/command_control_direwolf > /dev/null 2>&1
	
		sudo rm /home/pi/CubeSatSim/beacon_off > /dev/null 2>&1
	
	        sudo echo "0" > /home/pi/CubeSatSim/command_count.txt

		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
	
		reboot=1
	else

		echo "Not resetting"

	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 "   -n     Change to Transmit Commands mode"
	echo "   -e     Change to Repeater 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"
	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 "   -F     Change the RX and TX frequency"
	echo "   -H     Change the Balloon (HAB) mode"
	echo "   -p     Display payload sensor data"
	echo "   -v     Display voltage and current data"
	echo "   -P     Change the PL (Private Line) CTCSS/CDCSS codes for 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
	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
