#!/bin/bash

DEFAULT_LIB_DIR="/usr/local/lib"
DEFAULT_INCLUDE_DIR="/usr/local/include"
COMPILE_OPTIMIZED=true
COMPILE_DYNAMIC=false
SYSTEM_INSTALL=false



#============================================================================
get_choice()
#============================================================================
# $1 keypress for choice 1
# $2 return value for choice 1
# $3 keypress for choice 2
# $4 return value for choice 2
# $5 placeholder for the return value
{
   echo \(\'$1\' = $2\)
   echo \(\'$3\' = $4\)
   echo \(\'q\' = quit\)

   retval=
   while [ ! $retval ]
   do
      read choice
      if [ $choice ] && [ $choice = $1 ]
      then
	 retval=$2
      elif [ $choice ] && [ $choice = $3 ]
      then
         retval=$4
      elif [ $choice ] && [ $choice = q ]
      then
         exit 0
      else 
	 echo Wrong option, try again \($1/$3/q\).
      fi
   done

   export $5=$retval
}

#============================================================================
check_compiler()
#============================================================================
{
    g++ -v 2> /dev/null
}

#============================================================================
# MAIN SCRIPT STARTS HERE
#============================================================================

START_MESSAGE=\
"\
This script will compile and install the 'gotools_implicitization' \
library on your linux system.  "

clear
echo $START_MESSAGE
echo Press \'c\' to continue or any other key to quit.
read choice
if [ -z $choice ] || [ ! $choice == c ]
then
   exit 0
fi

# check for LINUX
UNAME=`uname`
if [ "$UNAME" != "Linux" ] 
then
echo "Your system was not found to be a linux system.  This setup script does "
echo "not support your system."
exit 1
else 
echo "Linux system OK."
fi

# check for necessary components (g++, boost library)
#COMPILER_OK=`check_compiler`
g++ -v 2> /dev/null
COMPILER_OK=$?
if [ $COMPILER_OK = "0" ]
then
    echo "Compiler OK"
else
    echo "Could not find compiler g++."
    echo "Aborting installation."
    exit 1
fi

#check_boost, by trying to compile with a typical boost header file
TMP_FILENAME=niu0dqpponifsfds`date`.C
echo "#include <boost/shared_ptr.hpp>" > "$TMP_FILENAME" 
echo "int main() {return 0;} " >> "$TMP_FILENAME" 
g++ "$TMP_FILENAME" -o/dev/null 2> /dev/null
BOOST_OK=$?
if [ $BOOST_OK = "0" ] 
then
    echo "Boost library OK"
    rm "$TMP_FILENAME"
else
    echo "Could not find 'boost' library."
    echo "You can download and install it from:"
    echo http://www.boost.org/
    echo "Aborting installation."
    rm "$TMP_FILENAME"
    exit 1
fi

echo Before compiling the library, you need to answer a few questions.
echo

echo Do you want to compile in DEBUG or OPTIMIZED mode?
get_choice "d" "DEBUG" "o" "OPTIMIZED" MODE

echo MODE set to $MODE
echo

echo Do you want to generate statically or dynamically linked libraries\?
get_choice "s" "STATIC" "d" "DYNAMIC" LINK

echo LINK set to $LINK
echo

echo Do you want to install the compiled files/headers to your system 
echo \(requires root privileges\).
get_choice "y" "YES" "n" "NO" SYSINSTALL

if [ $SYSINSTALL == "YES" ] 
then
   echo "Proposed system directory for header files is: " $DEFAULT_INCLUDE_DIR
   echo Do you want to change this\?
   get_choice "y" "YES" "n" "NO" TMP
   if [ $TMP == YES ] 
   then
	echo Enter catalog name for header files:
	read DEFAULT_INCLUDE_DIR
   fi
   while [ ! -d $DEFAULT_INCLUDE_DIR ] || [ ! -w $DEFAULT_INCLUDE_DIR ] 
   do
      echo Proposed directory $DEFAULT_INCLUDE_DIR does not exist, or you might
      echo not have write privileges to it. 
      echo Please specify another \(or \^C to quit\).
      read DEFAULT_INCLUDE_DIR
   done

   echo "Proposed system directory for library files is: " $DEFAULT_LIB_DIR
   echo Do you want to change this\?
   get_choice "y" "YES" "n" "NO" TMP
   if [ $TMP == YES ] 
   then
	echo Enter catalog name for library files:
	read DEFAULT_LIB_DIR
   fi
   while [ ! -d $DEFAULT_LIB_DIR ] || [ ! -w $DEFAULT_LIB_DIR ] 
   do
      echo Proposed directory $DEFAULT_LIB_DIR does not exist, or you might not
      echo have write privileges ot it. 
      echo Please specify another \(or \^C to quit\).
      read DEFAULT_LIB_DIR
   done
fi

if [ $MODE = "DEBUG" ] 
then 
COMPILE_OPTIMIZED=false 
fi
if [ $LINK = "DYNAMIC" ]
then 
COMPILE_DYNAMIC=true
fi
if [ $SYSINSTALL = "YES" ]
then
SYSTEM_INSTALL=true
fi

make OPTIMISED=$COMPILE_OPTIMIZED DYNLINK=$COMPILE_DYNAMIC \
    SYSINSTALL=$SYSTEM_INSTALL \
    SYS_INCLUDE_DIR=$DEFAULT_INCLUDE_DIR SYS_LIB_DIR=$DEFAULT_LIB_DIR
