#!/bin/bash -f

echo " "
echo "This script installs an ESP-r 11.11 X11 or GTK distribution on"
echo "a Mac OSX 10.5+ computer which already has the X11, developers"
echo "environment and the fink environment setup and running. On an"
echo "Intel Mac this should run natively."
echo " "
echo "It includes ESP-r executables, example models and standard"
echo "databases as well as some helper applications. "
echo " "
echo "You should run this installer with a sudo command so that"
echo "it will have administrative privileges."
echo " "
echo "The installation will be to a standard location, which"
echo "is /opt/esru and a link will be created to this from "
echo "/usr/esru if it does not already exist."
echo " "
echo "Note: if you want to install Radiance R3.9 on OSX you will"
echo "need to do this separately. Typically it is installed into"
echo "/usr/local/bin and /usr/local/lib/ray."
echo " "

DSP1=" \n
 You should have administrative access or the installer might not work.\n"
DSP2=" \n
 Hit return to start reading the licence\n
 \n"
DSP3=" \n
 The type of computer does not match this precompiled\n
 distribution of ESP-r. Aborting...\n"
DSP4=" \n
 The PATH environment variable is being updated. The .cshrc\n
 file the path= line should include the following...\n
 /opt/esru/esp-r/bin /usr/local/bin /sw/bin\n
 \n
 and the Radiance RAYPATH environment variable has also\n
 been set to /usr/local/lib/ray.\n
 \n
 Once you have completed the install logout and then\n
 login as a normal user and start up an X terminal.\n
 This loads environmental variables.\n
  \n
 Next issue the command 'which prj' (without the quotes).\n
 If the installation was successful you should find\n
 that there is a response '/opt/esru/esp-r/bin/prj'. You may then\n
 start the Project Manager via the command 'prj'.\n
 If the Project Manager application starts you no longer\n
 need this run file and it can be removed.\n
 If it does not start then you might need to look at\n
 the .cshrc file in your home folder.\n"
DSP5=" \n
    ESP-r is free software; you can redistribute it and/or modify\n
    it under the terms of the GNU General Public License as published by\n
    the Free Software Foundation; either version 2 of the License, or\n
    (at your option) any later version.\n
    \n
    This program is distributed in the hope that it will be useful,\n
    but WITHOUT ANY WARRANTY; without even the implied warranty of\n
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n
    GNU General Public License for more details.\n
    \n
    You should have received a copy of the GNU General Public License\n
    along with this program; if not, write to the Free Software\n
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n
    \n"
    

ISD=`pwd`

USR=`whoami`
echo " User is currently $USR"


# Check that fink has been installed.
echo " Checking for existance of /sw/bin"
if [ ! -d /sw/bin ]; then
	echo -e " Probably the fink environment has not been installed\n
                  so this installation will probably not work. Exiting\n"
	exit
fi

if [ ! -d /usr/X11R6/bin ]; then
	echo -e " Probably the X11 environment has not been installed\n
                  so this installation will not work. Exiting\n"
	exit
fi

echo
echo -n " proceed to install (y/n) ? "
read x

if [[ "$x" != "y" ]]
then
	echo -e " You have chosen to terminate the installation\n"
	exit
fi

echo -e $DSP2

read x
echo -e $DSP5 
echo " "
echo -n " Do you agree to the above license terms? [y/n]: "
read x

if [[ $x != 'y' ]] ; then
	echo " If you do not agree to the license, you can't install this software !"
	exit
fi

# proceed to install, first ask which version
echo " "
echo " ESP-r is available in two versions:"
echo " a) the standard X11 impementation"
echo " b) an implementation using the GTK graphics"
echo "    library (some functions have not yet been ported)"
echo " "
echo " selection option [a/b]:"
read which
if [[ $which = 'a' ]] ; then
  echo "proceeding with the X11 version..."
elif [[ $which = 'b' ]] ; then
  echo "proceeding with the GTK version..."
else
  echo "did not understand your response....aborting."
  exit
fi

# proceed to install
          
#-----------------------------------------------------------
# Detect architecture
#-----------------------------------------------------------
echo " Checking computer operating system..."
architecture=`uname -s`      # machine type
if [ $architecture = "Darwin" ]; then
 echo "OSX detected - continuing..."
else
 echo -e $DSP2
 exit
fi


echo " This may take a few minutes ..."

# Check that necessary folders exist.
if [ ! -d /opt/esru ]; then
  sudo mkdir /opt/esru
fi
  
cd /opt
if [[ $which = 'a' ]] ; then
  echo " Extracting X11 ESP-r distribution ..."
  sudo /sw/bin/zcat $ISD/esp-r_X11_11.11_intel_osx.tar.gz | tar xf -
elif [[ $which = 'b' ]] ; then
  echo " Extracting GTK ESP-r distribution ..."
  sudo /sw/bin/zcat $ISD/esp-r_GTK_11.11_intel_osx.tar.gz | tar xf -
fi
echo " Extracting ESP-r distribution ...done."
echo " "
echo "Setting folder permissions... "
chmod a+x /opt/esru
chmod a+x /opt/esru/bin
chmod a+x /opt/esru/esp-r
chmod a+x /opt/esru/esp-r/bin
chmod a+r /opt/esru
chmod a+r /opt/esru/bin
chmod a+r /opt/esru/esp-r
chmod a+r /opt/esru/esp-r/bin

echo "copy .cshrc...to /tmp"
sudo cp $ISD/cshrc /tmp/.cshrc
echo "  "
echo "NOTICE: a sample .cshrc file has been placed in /tmp"
echo "for you to copy into your own home folder in case you"
echo "do not have one. The command sequence to see if you"
echo "have this file is...."
echo "  cd"
echo "  ls -al"
echo "  "
echo "The command sequence to copy the sample file is..."
echo "  cd"
echo "  cp /tmp/.cshrc ."
echo " "
echo "See the instructions for further information."
echo " "

echo "...done. "
echo -e $DSP4

cd $ISD

echo " "
echo -n " Please wait while the installer performs cleanup..."
for i in `ls /opt/esru/esp-r/bin`; do echo -n .; sleep 0.5; done;


