#!/bin/bash

echo "CubeSatSim v1.2 configuration tool"

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 "APRS mode is set"
	elif [ "$1" = "m" ]; then
		echo "CW mode is set"
	elif [ "$1" = "f" ]; then
		echo "FSK mode is set"
	elif [ "$1" = "b" ]; then
		echo "BPSK mode is set"	
	elif [ "$1" = "s" ]; then
		echo "SSTV mode is set"
	else
		echo 
	fi
	
	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
	echo -e "Current sim.cfg configuration file:"	
#	echo
	
	echo $1 $2 $3 $4 $5
	echo
	
#	echo
#	echo "To change, include an OPTION"
#	echo
	set -- "-h"
fi	

if [ "$1" = "-i" ]; then
	echo "Restarting CubeSatSim"
	sudo systemctl restart cubesatsim
	exit 
elif [ "$1" = "-a" ]; then
	echo "changing CubeSatSim to AFSK mode"
	sudo echo "a" > /home/pi/CubeSatSim/.mode	
	sudo systemctl restart cubesatsim
	exit 
elif [ "$1" = "-m" ]; then
	echo "changing CubeSatSim to CW mode"
	sudo echo "m" > /home/pi/CubeSatSim/.mode	
	sudo systemctl restart cubesatsim
	exit 
elif [ "$1" = "-f" ]; then
	echo "changing CubeSatSim to FSK mode"
	sudo echo "f" > /home/pi/CubeSatSim/.mode	
	sudo systemctl restart cubesatsim
	exit 
elif [ "$1" = "-b" ]; then
	echo "changing CubeSatSim to BPSK mode"
	sudo echo "b" > /home/pi/CubeSatSim/.mode	
	sudo systemctl restart cubesatsim
	exit 
elif [ "$1" = "-s" ]; then
	echo "changing CubeSatSim to SSTV mode"
	sudo echo "s" > /home/pi/CubeSatSim/.mode	
	sudo systemctl restart cubesatsim
	exit 
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     Restarts 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
	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

	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 configuraation sim.cfg file updated to: \n"
	echo
	echo $1 $2 $3 $4 $sim
	echo $1 $2 $3 $4 $sim > /home/pi/CubeSatSim/sim.cfg
	echo
	echo "Restarting CubeSatSim with new configuraation file"
	echo

	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

	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 configuraation sim.cfg file updated to: \n"

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

	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 configuraation sim.cfg file updated to: \n"

		echo $1 $resets $3 $4 $5
		echo $1 $resets $3 $4 $5 > /home/pi/CubeSatSim/sim.cfg
	fi	

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

	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 configuraation sim.cfg file updated to: \n"
	echo $1 $2 $lat $long $5
	echo $1 $2 $lat $long $5 > /home/pi/CubeSatSim/sim.cfg
	
	if [ "$norestart" = "1" ]; then
		echo 
	else
		echo
		echo "Restarting CubeSatSim with new configuraation file"
		echo
		sudo systemctl restart cubesatsim
	fi
fi

# sudo systemctl restart cubesatsim
