#! /bin/sh
# Shell script for installing Geomview binary distribution

cpu=linux
version="1.5"
machtype=linux

dir=`expr "$0" : "\(.*\)/[^/]*"`
if [ "$dir" != "" ] ; then 
  cd $dir ;
fi

if [ -n "$BASH" ]; then
    # This trash enables the "-e" flag for all "echo" commands in bash.
    aliasecho() { echo() { unset echo; echo -e "$@"; aliasecho; } }
    aliasecho
fi

if [ -r ./mathematica/OOGL.m ] ; then
  havemma="yes"
else
  havemma="no"
fi

case "$cpu" in
  "next")
    # If possible, get default notion of geomroot from ./geomrootpath,
    # which is created by NeXT Installer (script Geomview.post_install)
    # and contains the pathname specified by the person who installed
    # Geomview.app.  This is because that pathname may be more canonical
    # that what /bin/pwd returns.
    if [ -f "./geomrootpath" ] ; then
      geomroot=`head -1 geomrootpath` ;
    else
      geomroot=`/bin/pwd`
    fi
    bindir=/usr/local/bin
    mandir=/usr/man
    mmadir=
    mapledir=
    ;;
  "sgi")
    geomroot=`/bin/pwd`
    bindir=/usr/local/bin
    mandir=/usr/catman/local
    mmadir=
    mapledir=
    ;;
  "sun4"|*)
    geomroot=`/bin/pwd`
    bindir=/usr/local/bin
    mandir=/usr/local/man
    mmadir=
    mapledir=
    ;;
esac

########################################################################

noop() {
 :
}

abort() {
  trap noop 1 2 3 13 15 ;
  echo "" ;
  echo "Installation aborted." ;
  echo "" ;
  exit 1 ;
}

trap abort 1 2 3 13 15

########################################################################

checkdir()
{
  dir=$1
  if [ -d "$dir" -a ! -w "$dir" ] ; then
    echo '
You do not have permission to write to directory '$dir'.
Please run the "install" script again and specify a directory
that you can write to.
' ;
    abort ;
  fi
}

echo '
This is the installation script for the binary distribution of
Geomview.  Hit control-C at any time to abort the installation.

You will be asked to specify several directories into which various
components of Geomview will be installed.  For each directory, enter
the pathname on a line by itself with no spaces before or after it.'

if [ "$dir" != "" -a "$dir" != "." ] ; then 
  echo 'This script is running in the directory 
	'$dir'
All relative pathnames you enter will be relative to this directory.'
fi

########################################################################

case "$cpu" in
  "next")
    echo '

1. Specify the *absolute* pathname of the installed Geomview root
   directory; this is the directory called "Geomview.app".  Geomview
   needs to know where this directory is in order to run.  If you move
   this directory you will have to re-run this install script in order
   for Geomview to work.  Be sure to specify an absolute pathname!
   Enter the directory on the next line, or hit <return> without
   typing anything to use '$geomroot':'
     ;;

  "sgi"|"sun4"|*)
    echo '

1. Specify the *absolute* pathname of the installed Geomview root
   directory; this is the directory called "Geomview" that was created
   when you unpacked the Geomview distribution.  Geomview needs to
   know where this directory is in order to run.  If you move
   this directory you will have to re-run this install script in order
   for Geomview to work.  Be sure to specify an absolute pathname!
   Enter the directory on the next line, or hit <return> without
   typing anything to use '$geomroot':'
     ;;
esac

read input
if [ "$input" != "" ] ; then
  geomroot=$input
fi

if [ ! -d $geomroot ] ; then
  echo ''
  echo 'The directory '$geomroot' does not exist.  Try again.'
  abort
fi

########################################################################

echo '
2. There are several executable files associated with Geomview.  These
   include a shell script called "geomview" which invokes geomview,
   plus several auxiliary programs.  Please specify the directory
   where you want to install these executable files.  This directory
   should be on user'"'"'s $path.  Enter the directory on the next
   line, or hit <return> without typing anything to use
      '$bindir':'

read input
if [ -n "$input" ] ; then
  bindir=$input
fi

checkdir $bindir

########################################################################

echo '
3. Specify the directory where you want to install the Unix manual
   page files.  This should be a directory that is searched by the
   "man" command; it should have subdirectories "man1", "cat1", etc.
   Enter the directory on the next line, or hit <return> without
   typing anything to use '$mandir':'

read input
if [ -n "$input" ] ; then
  mandir=$input
fi

checkdir $mandir

########################################################################

if [ "$havemma" = "yes" ] ; then

echo '

4. Specify the directory where you want to install the Mathematica
   packages.  If you do not have Mathematica, hit <return> without
   typing anything.  This should be a directory that Mathematica looks
   in for packages that it loads (it should be in the list of
   directories given by the value of Mathematica'"'"'s $Path variable).
   Enter the directory on the next line, or hit <return> without
   typing anything if you do not want to install the Mathematica
   packages:'

read mmadir

if [ -n "$mmadir" ] ; then
  checkdir $mmadir
fi

fi

########################################################################


echo '

5. Specify the Maple library directory where you want to install the gvplot
   package.  If you do not have Maple, hit <return> without typing anything.
   This should be a directory where Maple searches for packages,
   i.e. it should be one of the directories printed when you type
	libname;
   in Maple.  (You can place a src/init file in Maple'"'"'s default library
   directory to add other directories to "libname"; see its documentation.)
   Enter the directory on the next line, or hit <return> without
   typing anything if you do not want to install the Maple packages:'

read mapledir

if [ -n "$mapledir" ] ; then
  checkdir $mapledir
fi

echo "
You have entered:
        Geomview root directory: $geomroot
         install executables in: $bindir
           install man pages in: $mandir"

if [ "$havemma" = "yes" ] ; then
  if [ "$mmadir" != "" ] ; then
    echo "install Mathematica packages in: $mmadir"
  else
    echo "Mathematica packages will not be installed."
  fi
fi

if [ -n "$mapledir" ]; then
    echo "      install Maple packages in: $mapledir"
else
    echo "Maple packages will not be installed."
fi

echo '
NOTE: the installation process will overwrite any existing Geomview
files which are already in these directories.'
creating=""
n="
"
test -d "$bindir" || creating="    $bindir"
test -d "$mandir" || creating="$creating$n    $mandir"
test -d "${mmadir:-.}" || creating="$creating$n    $mmadir"
test -d "${mapledir:-.}" || creating="$creating$n    $mapledir"

if [ -n "$creating" ]; then
  echo "
The following directories do not exist and will be created
during installation:
$creating"

fi

echo ''
echo 'Is this correct? [Y/n] \c'
read ans
echo ''
case "$ans" in
  [yY]*|"")
    echo ''
    echo 'Installing ...'
    ;;
  *)
    abort
    ;;
esac

make_log()
{
  trap noop ;
  /bin/rm -f install.log
  ( echo "GEOMROOT=$geomroot" ;
    echo "BINDIR=$bindir" ;
    echo "MANDIR=$mandir" ;
    echo "MACHTYPE=$machtype" ;
    if [ "$mmadir" != "" ] ; then
      echo "MMAPACKAGEDIR=$mmadir" ;
    else
      echo '# [Mathematica packages not installed.]' ;
    echo ''
    echo "MAPLE_LIB=$mapledir" ;
    fi ) > install.log
  cat $$.log  >> install.log
  /bin/rm -f $$.log
}

partial_abort()
{
  trap noop 1 2 3 13 15 ;
  echo '' ;
  echo 'Installation aborted after some files were installed.' ;
  echo 'See the file "install.log" for a record.' ;
  echo '' ;
  make_log ;
  ( echo '' ;
    echo 'This installation was aborted before completion.' ;
    echo '' ; ) >> install.log
  exit 1 ;
}

trap partial_abort 1 2 3 13 15

make -f tools/Makefile.install-bin \
	GEOMROOT="$geomroot" BINDIR="$bindir" MANDIR="$mandir" \
	MMAPACKAGEDIR="$mmadir" MAPLE_LIB="$mapledir" MACHTYPE="$machtype"\
	install_all 2>&1 | tee $$.log

if grep -i error $$.log >&- 2>&- ; then
  success="no"
  echo ''
  echo 'Installation not completed due to errors.  Re-run the'
  echo 'install script after fixing the problem(s).'
else
  success="yes"
  echo ''
  echo 'Geomview installation complete.'
fi | tee -a $$.log

make_log

echo ''
echo 'A record of this installation is in the file "install.log"'

if grep -i error install.log >&- 2>&- ; then
  success="no"
else
  success="yes"
fi

if [ "$success" != "yes" ] ; then
  exit 1
fi

########################################################################

reg_addr="register@geom.umn.edu"
reg_file="REGISTER"

do_send_msg()
{
  echo 'Please enter the real life name of a contact person at your site: \c' ;
  read contact_name ;
  echo 'Please enter the e-mail address of the contact person: \c' ;
  read contact_addr ;
  echo '
The geomview-users mailing list is used to send information about
updates, future releases, bug fixes, and to allow you to
communicate with other geomview users.
'
echo 'Do you wish to be added to the geomview-users mailing list? [Y/n] \c'
  read add_to_list ;
  echo ''

  echo "Sending mail to $reg_addr"
(
  case "$cpu" in
    "next")
	echo "NeXTStep binary distribution ${version}"
	echo ""
	if [ -x /usr/bin/arch ] ; then
	    echo "/usr/bin/arch: `/usr/bin/arch`"
	else
	    echo "(apparently using NeXTStep 3.0)"
	fi ;
      ;;
    "sgi") echo "SGI binary distribution $version" ;;
    "sun4") echo "Sun4 binary distribution $version" ;;
    *) echo "$cpu binary distribution $version" ;;
  esac
    echo ""
    echo "Contact person: $contact_name
         email: $contact_addr
"

    case "$add_to_list" in
      [yY]*|"")
        echo "Please add to geomview-users list."
	echo ""
        ;;
    esac ;
  case "$cpu" in
    "next") hostinfo  ;;
    "sgi")  uname -a ; echo ''; hinv ;;
    *) uname -a ;;
  esac

  ) | Mail -s 'New Geomview Installation' $reg_addr
  echo ''
}

echo '
It is important for us to know who is using Geomview.

If you do not object, an email message will be sent to
'$reg_addr' mentioning this installation.
If you do not wish such a message to be sent, enter "no".
But please, do send the message.  It is very helpful to us.
'
echo 'Send a message? [Y/n] \c'

read ans
echo ''
case "$ans" in
  [yY]*|"")
    do_send_msg ;
    ;;
esac

echo 'It is very useful for us to know what our users are doing with
Geomview.  Please read the file '$reg_file' which explains
how to keep us informed.  Thank you, and happy geomviewing!
'

exit 0
