#!/bin/bash

echo "CubeSatSim v1.3.2 configuration tool"
echo

if [ "$2" = "-n" ]; then
	norestart=1
else
	norestart=0
fi

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"
	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
    		echo "Radio command and control is ON"
	else 
    		echo "Radio command and control is OFF"
	fi
	echo

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

	echo
	echo -e "Current sim.cfg configuration file:"	
#	echo
	
	echo $1 $2 $3 $4 $5 $6 $7 $8 $9
	echo
	
	echo "To change, include an OPTION"
	echo "To see options, type config -h"
#	echo
#	set -- "-h"
# fi	


elif [ "$1" = "-i" ]; then
	echo "Rebooting CubeSatSim"
	sudo reboot now
#	sudo systemctl restart cubesatsim
	exit 
elif [ "$1" = "-a" ]; then
	echo "changing CubeSatSim to AFSK mode"
	sudo echo "a" > /home/pi/CubeSatSim/.mode

	FILE=/home/pi/CubeSatSim/battery_saver
	if [ -f "$FILE" ]; then
    		echo "Battery saver mode is ON."
#  		if ! grep -q force_turbo=1 /boot/config.txt ; then sudo sh -c 'echo "force_turbo=1" >> /boot/config.txt'; fi
	else 
    		echo "Battery saver mode is OFF."
#    		sudo sed -i ':a;N;$!ba;s/\nforce_turbo=1//g' /boot/config.txt 
	fi
	
	echo "rebooting"
	sudo systemctl stop rpitx
	sudo reboot now
#	sudo systemctl restart cubesatsim
	exit 

elif [ "$1" = "-m" ]; then
	echo "changing CubeSatSim to CW mode"
	sudo echo "m" > /home/pi/CubeSatSim/.mode

	FILE=/home/pi/CubeSatSim/battery_saver
	if [ -f "$FILE" ]; then
    		echo "Battery saver mode is ON."
 # 		if ! grep -q force_turbo=1 /boot/config.txt ; then sudo sh -c 'echo "force_turbo=1" >> /boot/config.txt'; fi
	else 
    		echo "Battery saver mode is OFF."
 #   		sudo sed -i ':a;N;$!ba;s/\nforce_turbo=1//g' /boot/config.txt 
	fi
	echo "rebooting"
	sudo systemctl stop rpitx
	sudo reboot now
#	sudo systemctl restart cubesatsim
	exit 
elif [ "$1" = "-f" ]; then
	echo "changing CubeSatSim to FSK mode"
	sudo echo "f" > /home/pi/CubeSatSim/.mode
#  	if ! grep -q force_turbo=1 /boot/config.txt ; then sudo sh -c 'echo "force_turbo=1" >> /boot/config.txt'; fi
	echo "rebooting"
	sudo systemctl stop rpitx
	sudo reboot now
#	sudo systemctl restart cubesatsim
	exit 
elif [ "$1" = "-b" ]; then
	echo "changing CubeSatSim to BPSK mode"
	sudo echo "b" > /home/pi/CubeSatSim/.mode
#  	if ! grep -q force_turbo=1 /boot/config.txt ; then sudo sh -c 'echo "force_turbo=1" >> /boot/config.txt'; fi
	echo "rebooting"
	sudo systemctl stop rpitx
	sudo reboot now
#	sudo systemctl restart cubesatsim
	exit 
elif [ "$1" = "-s" ]; then
	echo "changing CubeSatSim to SSTV mode"
	sudo echo "s" > /home/pi/CubeSatSim/.mode

	FILE=/home/pi/CubeSatSim/battery_saver
	if [ -f "$FILE" ]; then
    		echo "Battery saver mode ON."
#  		if ! grep -q force_turbo=1 /boot/config.txt ; then sudo sh -c 'echo "force_turbo=1" >> /boot/config.txt'; fi
	else 
    		echo "Battery saver mode is OFF."
#    		sudo sed -i ':a;N;$!ba;s/\nforce_turbo=1//g' /boot/config.txt 
	fi
	echo "rebooting"
	sudo systemctl stop rpitx
	sudo reboot now
#	sudo systemctl restart cubesatsim
	exit 

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
	
#	echo $1 $2 $3 $4 $5 $6 $7 $8 $9

	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
	echo $1 $2 $3 $4 $sim $6 $7 $8 $9 > /home/pi/CubeSatSim/sim.cfg
	echo
	echo "Rebooting CubeSatSim with new configuration file"
	echo

	sudo reboot now
#	sudo systemctl 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

	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
		echo $callsign $2 $3 $4 $5 $6 $7 $8 $9 > /home/pi/CubeSatSim/sim.cfg
	fi
	
	if [ "$norestart" = "1" ]; then
		echo 
	else
		echo
		echo "Rebooting CubeSatSim with new configuration file"
		echo
		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

	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
		echo $1 $resets $3 $4 $5 $6 $7 $8 $9 > /home/pi/CubeSatSim/sim.cfg
	fi	

	if [ "$norestart" = "1" ]; then
		echo
	else
		echo
		echo "Rebooting CubeSatSim with new configuration file"
		echo
		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

	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
	echo $1 $2 $lat $long $5 $6 $7 $8 $9 > /home/pi/CubeSatSim/sim.cfg
	
	if [ "$norestart" = "1" ]; then
		echo 
	else
		echo
		echo "Rebooting CubeSatSim with new configuration file"
		echo
		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 rpitx

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

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

	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
			echo "rebooting"
			sudo systemctl stop rpitx
			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 "rebooting"
			sudo systemctl stop rpitx
			sudo reboot now	
		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
		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 battery saver mode"
	echo
	
	FILE=/home/pi/CubeSatSim/battery_saver
	if [ -f "$FILE" ]; then
    		echo "Battery saver mode is ON."
		mode=1
 	else 
    		echo "Battery saver mode is OFF."
		mode=0
	fi
	
	echo
	
	echo "Do you want Battery saver mode ON (y/n) "
	read saver
	echo
	
	if [ "$saver" = "y" ] || [ "$saver" = "yes" ]  ; then
		echo "Battery saver mode is now ON"
		touch /home/pi/CubeSatSim/battery_saver
#		if ["$mode" = "0"] ; then sudo systemctl restart rpitx
	else
		echo "Battery saver mode is now OFF"
		sudo rm /home/pi/CubeSatSim/battery_saver
#		if ["$mode" = "1"] ; then sudo systemctl restart rpitx
	fi

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

	if [ "$1" = "a" ] ||  [ "$1" = "s" ] ||  [ "$1" = "m" ] ; then
		echo "rebooting"
#		sudo systemctl restart cubesatsim
		sudo reboot now
	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

	echo -e "Enter squelch  (integer 0 - 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
	echo $1 $2 $3 $4 $4 $sq $7 $8 $9 > /home/pi/CubeSatSim/sim.cfg
	echo
	echo "Rebooting CubeSatSim with new configuration file"
	echo

	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

	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
		echo $1 $2 $3 $4 $5 $6 $tx $rx $9 > /home/pi/CubeSatSim/sim.cfg
#	fi
	
#	if [ "$norestart" = "1" ]; then
#		echo 
#	else
		echo
		echo "Rebooting CubeSatSim with new configuration file"
		echo
		sudo reboot now
#		sudo systemctl restart cubesatsim
#	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

	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
	echo $1 $2 $3 $4 $5 $6 $7 $8 $hab > /home/pi/CubeSatSim/sim.cfg
	echo
	echo "Rebooting CubeSatSim with new configuration file"
	echo

	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" = "-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 "   -i     Reboots 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 "   -R     Change the Commands Count in the file command_count.txt"
	echo "   -B     Change battery saver mode manually"
	echo "   -q     Change the Squelch setting for command receiver"
	echo "   -F     Change the rx and tx frequency"
	echo "   -H     Chnage the Balloon mode"
	echo "   -p     Display payload sensor data"
	echo "   -v     Display voltage and current data"
	echo
	exit

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

fi

# sudo systemctl restart cubesatsim
