#!/bin/bash

##############################################################################
# Simple script to configure, compile (and if you're 'root', install) SPLAT! #
# and associated utilities.  Written by John A. Magliacane, KD2BD May 2002   #
#                          Last update: March 2009                           #
##############################################################################

ans=""
whoami=`whoami`

until [ "$ans" = "2" ] || [ "$ans" = "3" ] || [ "$ans" = "4" ] || [ "$ans" = "5" ] || [ "$ans" = "6" ] || [ "$ans" = "7" ] || [ "$ans" = "8" ]; do

	clear
	echo
	echo -e "\t  ******************************************************"
	echo -e "\t  **   SPLAT! Standard Resolution Mode Configuration  **"
	echo -e "\t  ******************************************************\n"
	echo -e "\t   Please select the Maximum Analysis Region capability"
	echo -e "\t   you would like SPLAT! to possess when operating in"
	echo -e "\t   3 arc-second (standard) resolution mode based on"
	echo -e "\t   your available memory resources:\n"
	echo -e "\t   Maximum Analysis Region       RAM + Swap Requirement"
	echo -e "\t   ====================================================\n"
	echo -e "\t   (2)  2 x 2 Degrees  ---------   25 Megabytes minimum"
	echo -e "\t   (3)  3 x 3 Degrees  ---------   52 Megabytes minimum"
	echo -e "\t   (4)  4 x 4 Degrees  ---------   95 Megabytes minimum"
	echo -e "\t   (5)  5 x 5 Degrees  ---------  145 Megabytes minimum"
	echo -e "\t   (6)  6 x 6 Degrees  ---------  210 Megabytes minimum"
	echo -e "\t   (7)  7 x 7 Degrees  ---------  285 Megabytes minimum"
	echo -e "\t   (8)  8 x 8 Degrees  ---------  370 Megabytes minimum\n"
	echo -n "Your choice: "
	read ans
done

# Build std-parms.h file that will be copied into splat.h at compilation time

echo "/*" > std-parms.h
echo "  Parameters for 3 arc-second standard resolution mode of operation" >> std-parms.h
echo -e "  Generated by $0 by "$whoami" on "`date`"\n*/" >> std-parms.h

echo "#define HD_MODE 0" >> std-parms.h

if [ "$ans" = "2" ]; then
	echo "#define MAXPAGES 4" >> std-parms.h
fi

if [ "$ans" = "3" ]; then
	echo "#define MAXPAGES 9" >> std-parms.h
fi

if [ "$ans" = "4" ]; then
	echo "#define MAXPAGES 16" >> std-parms.h
fi

if [ "$ans" = "5" ]; then
	echo "#define MAXPAGES 25" >> std-parms.h
fi

if [ "$ans" = "6" ]; then
	echo "#define MAXPAGES 36" >> std-parms.h
fi

if [ "$ans" = "7" ]; then
	echo "#define MAXPAGES 49" >> std-parms.h
fi

if [ "$ans" = "8" ]; then
	echo "#define MAXPAGES 64" >> std-parms.h
fi

ans=""

until [ "$ans" = "0" ] || [ "$ans" = "1" ] || [ "$ans" = "2" ] || [ "$ans" = "3" ] || [ "$ans" = "4" ] || [ "$ans" = "5" ] || [ "$ans" = "6" ] || [ "$ans" = "7" ] || [ "$ans" = "8" ]; do

	clear
	echo
	echo -e "\t  ******************************************************"
	echo -e "\t  **   SPLAT! HD High Resolution Mode Configuration   **"
	echo -e "\t  ******************************************************\n"
	echo -e "\t   Please select the Maximum Analysis Region capability"
	echo -e "\t   you would like SPLAT! to possess when operating in"
	echo -e "\t   1 arc-second (HD) resolution mode based on your"
	echo -e "\t   available memory resources (select 8x8 with caution):\n"
	echo -e "\t   Maximum Analysis Region       RAM + Swap Requirement"
	echo -e "\t   ====================================================\n"
	echo -e "\t   (1)  1 x 1 Degrees  ---------   52 Megabytes minimum"
	echo -e "\t   (2)  2 x 2 Degrees  ---------  225 Megabytes minimum"
	echo -e "\t   (3)  3 x 3 Degrees  ---------  468 Megabytes minimum"
	echo -e "\t   (4)  4 x 4 Degrees  ---------  855 Megabytes minimum"
	echo -e "\t   (5)  5 x 5 Degrees  --------- 1305 Megabytes minimum"
	echo -e "\t   (6)  6 x 6 Degrees  --------- 1890 Megabytes minimum"
	echo -e "\t   (7)  7 x 7 Degrees  --------- 2565 Megabytes minimum"
	echo -e "\t   (8)  8 x 8 Degrees  --------- 3330 Megabytes minimum"
	echo -e "\t   (0)  None of the above.  Do not build SPLAT! HD.\n"
	echo -n "Your choice: "
	read ans
done

if [ "$ans" != "0" ]; then

	# Build hd-parms.h file that will be copied into splat.h at compilation time

	echo "/*" > hd-parms.h
	echo "  Parameters for 1 arc-second high resolution mode of operation" >> hd-parms.h
	echo -e "  Generated by $0 by "$whoami" on "`date`"\n*/" >> hd-parms.h

	echo "#define HD_MODE 1" >> hd-parms.h

	if [ "$ans" = "1" ]; then
		echo "#define MAXPAGES 1" >> hd-parms.h
	fi

	if [ "$ans" = "2" ]; then
		echo "#define MAXPAGES 4" >> hd-parms.h
	fi

	if [ "$ans" = "3" ]; then
		echo "#define MAXPAGES 9" >> hd-parms.h
	fi

	if [ "$ans" = "4" ]; then
		echo "#define MAXPAGES 16" >> hd-parms.h
	fi

	if [ "$ans" = "5" ]; then
		echo "#define MAXPAGES 25" >> hd-parms.h
	fi

	if [ "$ans" = "6" ]; then
		echo "#define MAXPAGES 36" >> hd-parms.h
	fi

	if [ "$ans" = "7" ]; then
		echo "#define MAXPAGES 49" >> hd-parms.h
	fi

	if [ "$ans" = "8" ]; then
		echo "#define MAXPAGES 64" >> hd-parms.h
	fi

	else
		rm -r hd-parms.h
fi

clear
echo -e "\n\n            *****************************************************"
 
echo "            *  Now building SPLAT! and associated utilities...  *"
echo "            *****************************************************"
echo
./build all
if [ "$whoami" = "root" ]; then
	echo -e "\nNow installing SPLAT! and associated utilities..."
	echo
	./install all
else
	echo
	echo "To install SPLAT! and its associated utilities, please"
	echo "su to 'root' and execute the install script as follows:"
	echo
	echo -e "\t./install all"
fi
echo
echo "Don't forget to read the documentation under the docs directory"
echo "as well as the various README files in the splat and splat/utils"
echo -e "directories.  Enjoy the program!  John, KD2BD\n\n"

