#!/bin/bash

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

path=`pwd`'/'
whoiam=`whoami`

# System-wide installation requires "root".

if [ $whoiam == "root" ]; then

	# Install PREDICT's symlinks and man pages

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

	echo
	echo "Linking PREDICT binaries to "$bindestination":"

	lncmd=`pwd`"/predict $bindestination/predict"
	ln -sf $lncmd

	if [ -L $bindestination/predict ]; then
		echo "predict... Done!"
	else
		echo "Oops! 'ln -sf $lncmd':  Didn't work!  :-("
	fi

	lncmd=`pwd`"/xpredict $bindestination/xpredict"
	ln -sf $lncmd

	if [ -L $bindestination/xpredict ]; then
		echo "xpredict... Done!"
	else
		echo "Oops! 'ln -sf $lncmd':  Didn't work!  :-("
	fi

	# Install earthtrack and earthtrack2

	echo
	echo "Linking earthtrack and earthtrack2 to "$bindestination":"
	lncmd=`pwd`"/clients/earthtrack/earthtrack $bindestination/earthtrack"
	ln -sf $lncmd

	if [ -L $bindestination/earthtrack ]; then
		echo "earthtrack... Done!"
	else
		echo "Oops! 'ln -sf $lncmd':  Didn't work!  :-("
	fi

	lncmd=`pwd`"/clients/earthtrack/earthtrack $bindestination/earthtrack2"
	ln -sf $lncmd

	if [ -L $bindestination/earthtrack2 ]; then
		echo "earthtrack2... Done!"
	else
		echo "Oops! 'ln -sf $lncmd':  Didn't work!  :-("
	fi

	# Install moontracker

	echo
	echo "Linking moontracker to "$bindestination":"
	lncmd=`pwd`"/utils/moontracker/moontracker $bindestination/moontracker"
	ln -sf $lncmd

	if [ -L $bindestination/moontracker ]; then
		echo "moontracker... Done!"
	else
		echo "Oops! 'ln -sf $lncmd':  Didn't work!  :-("
	fi
	
	# Man pages

	rm -if /usr/local/man/man1/predict.1

	echo
	echo "Installing PREDICT's man page in /usr/local/man/man1:"
	lncmd="`pwd`/docs/man/predict.1 > /usr/local/man/man1"
	gzip -c `pwd`/docs/man/predict.1 > /usr/local/man/man1/predict.1.gz 

	if [ -e /usr/local/man/man1/predict.1.gz ]; then
		echo "... Done!"
	else
		echo "Oops! 'gzip -c `pwd`/docs/man/predict.1 > /usr/local/man/man1/predict.1.gz': Didn't work!  :-("
	fi

	echo

	# Test for UDP Availability on Port 1210

	response1=$(grep -c -i -e 1210/udp /etc/services)
	response2=$(grep -c -i -e PREDICT /etc/services)
	result=$((response1+response2))

	if [ $result == 2 ]; then
		echo "Good!  Your /etc/services file appears to be configured for PREDICT:"
	        echo
		echo -n "     " 
		grep -e 1210/udp -e PREDICT /etc/services
		echo
	else
		echo "Please insert the following line into your /etc/services file:"
		echo
		echo "     predict         1210/udp   #PREDICT's UDP socket port"
		echo
	fi

	echo "Don't forget to cd into the 'clients' and 'utils' directories for additional"
	echo "applications, 'docs' for documentation in various forms, and visit"
	echo "https://www.qsl.net/kd2bd/predict.html for the latest news."
	echo
	echo "Happy Tracking!"
	echo

else
	echo "You need to be 'root' to perform a system-wide installation."
	echo "However, PREDICT can run from THIS directory with limited capabilities."
	fi

