#!/bin/sh

if pgrep "gpredict" > /dev/null
then
	echo "Already running (gpredict)"
	notify-send "gpredict is already running, can't update without breaking it's config."
	exit
else
	if pgrep "updatetle.sh" > /dev/null
	then
		echo "Already running (update script)"
		notify-send "gpredict update script is already running!"
		exit
		#Now stopping, because gpredict is already running.
	else
		if pgrep "updatetrsp.sh" > /dev/null
		then
			echo "Already running (update script)"
			notify-send "gpredict update script is already running!"
			exit
			#Now stopping, because update script is already running.
		else
			echo "Stopped"
			cd UpdateTLE
			notify-send "gpredict is not running. Updating TLE & TRSP data. Do not run gpredict before we're done!"
			# Updating TLE
			notify-send "Updating TLE data."	
			./updatetle.sh
			# Updating TRSP
			notify-send "Updating TRSP data."
			./updatetrsp.sh
			notify-send "Informing gpredict about updates."
			# Now we tell gpredict the time we have now. So now warning that TLE data is too old.
			UNIXTIME=`date '+%s'`
			sed -i"" -e s/"LAST_UPDATE=[0-9]*"/LAST_UPDATE=$UNIXTIME/g ~/.config/Gpredict/gpredict.cfg
			notify-send "gpredict is now up-to-date."
			exit
			fi
		fi
fi