#!/bin/bash

#############################################################
#   Script to configure, compile, and install PREDICT.      #
# Give an argument to this script if you wish to override   #
#  the default installation directory of /usr/local/bin.    #
#                ie: ./configure /usr/bin                   #
#############################################################
#    Written by John A. Magliacane, KD2BD on 12-Apr-2020    #
#                Last updated on 06-Jul-2026                #
#############################################################

export TERM=linux
whoiam=`whoami`

if [ $whoiam == "root" ]; then
	echo "You need to be 'root' to install PREDICT, but not to build it."
	echo "Re-run this script as a regular user to compile it."
	exit -1
else
	oldterm=$TERM
	export TERM=linux
fi

version=$(cat .version)
path=`pwd`'/'

if [ $# == "0" ]; then
	bindestination="/usr/local/bin"
else
	bindestination=$1
fi

clear
echo -en "\e[=3l"	# 80x25 color text mode
echo -e "\e[44m" 	# Set the background color to blue
echo -e "\e[2J"		# Erase and paint the background blue
echo -e "\e[1m"		# Bold on

echo -e "                  \e[41m                                           \e[44m"
 
echo -e "                  \e[41m         --== PREDICT  v$version ==--         \e[44m"

echo -e "                  \e[41m   Released by John A. Magliacane, KD2BD   \e[44m"
echo -e "                  \e[41m                 July 2026                 \e[44m"

echo -e "                  \e[41m                                           \e[44m"

echo -e "\n\e[33m"
cat <<_EOF_
  PREDICT is a satellite tracking and orbital prediction program created
  for Linux and similar operating systems by John A. Magliacane, KD2BD.
  PREDICT is free software.  You can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free
  Software Foundation, either version 2 of the License or any later version.

  PREDICT is distributed in the hope that it will useful, but WITHOUT ANY
  WARRANTY, without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  details.

_EOF_

echo -en "\e[37m  Do you accept these conditions and wish to install this software? [Y/N] "

ans=""

while [[ $ans != "y" && $ans != "Y"  && $ans != 'n' && $ans != 'N' ]]; 
do
	read -s -n 1 ans
done

echo -e "\e[0m"		# Clear all text attributes
clear			# Clear the terminal

if [[ $ans == "n" || $ans == "N" ]]; then
	echo "Bye!"
	exit -1
fi

# Test the compiler and that the required libraries are in place.

rm -f gcctest

echo "int main()" > gcctest.c
echo "{" >> gcctest.c
echo "return 0;" >> gcctest.c
echo "}" >> gcctest.c

echo -n "Testing compiler... "

cc gcctest.c -o gcctest

if [ -x gcctest ]; then

	rm -f gcctest

	echo -ne "\nTesting pthreads library... "

	cc gcctest.c -lpthread -o gcctest

	if [ -x gcctest ]; then

		rm -f gcctest
		echo -ne "\nTesting ncurses library... "
		cc gcctest.c -lncurses -o gcctest

		if [ -x gcctest ]; then

			rm -f gcctest
			echo -ne "\nTesting ALSA sound library... "
			cc gcctest.c -lasound -o gcctest

			if [ -x gcctest ]; then

				rm -f gcctest
				echo -ne "\nTesting CURL library... "
				cc gcctest.c -lcurl -o gcctest

				if [ -x gcctest ]; then

					rm -f gcctest
				else
					echo -e "\n\n*** Error: Compiler test for CURL library failed.  :-(\n"
					export TERM=$oldterm
					exit -1
				fi
			else
				echo -e "\n\n*** Error: Compiler test for ALSA library failed.  :-(\n"
				export TERM=$oldterm
				exit -1
			fi
		else

			echo -e "\n\n*** Error: Compiler test for ncurses library failed.  :-(\n"
			export TERM=$oldterm
			exit -1
		fi
	else
		echo -e "\n\n*** Error: Compiler test for pthreads library failed.  :-(\n"
		export TERM=$oldterm
		exit -1

	fi

else
	echo -e "\n\n*** Error: Compiler test failed.  :-(\n\nAre you sure you have a functioning C Compiler installed?\n"
	cc --version
	export TERM=$oldterm
	exit -1
fi

rm -f gcctest.c

# If we made it this far, we're good to compile the software.

echo
echo

# Compile PREDICT...
./build

# Compile earthtrack...
#rm -if $bindestination/earthtrack2
cd clients/earthtrack
./build
cd ../..

# Compile moontracker...
cd utils/moontracker
./build
cd ../..

echo
echo "A system-wide installation requires 'root' authority."
echo "Please enter the root password to complete the installation below."
echo

su root -c ./install

export TERM=$oldterm

