#!/bin/sh

clear
cat <<EOF

  This script is provided to simplify the installation of GNAT-AJIS,
  the GNAT Ada-Java Interfacing Suite.

  You will be asked for confirmation before the actual installation is
  done. You can break out of this script at any time before this.

  Hit RETURN to continue.
EOF

read x

current_dir=`/bin/pwd`

   ###############
   # ask_gnatdir #
   ###############

## Read the base directory (absolute path name)
## Sets the variable  $basedir
ask_gnatdir() {
   clear
   default_dir=`(type gnat | cut -d' ' -f3) 2>/dev/null`
   default_dir=`dirname $default_dir 2>/dev/null`

   if [ "$default_dir" != "" -a "$default_dir" != "." -a "$default_dir" != "/usr/bin" ]; then   	
      default_dir=`cd $default_dir/..; pwd`

      cat <<EOF

  GNAT has been found in $default_dir.
  Do you want to use this GNAT installation? Hit RETURN if yes or enter
  the name of another directory.

EOF
   else
     default_dir=/opt/gnat
     cat <<EOF
  Enter the full path where you want to install GNAT-AJIS

EOF
   fi

   while [ "$basedir " = " " ]; do
      printf "[$default_dir] "
      read basedir

      if [ " $basedir" = " " ]; then
         basedir="$default_dir"
      fi

      if echo $basedir | egrep "^[/~]" >/dev/null; then
         true
      else
         basedir=`pwd`/$basedir
      fi
   done

   # Suppress the final / in basedir
   basedir=`echo $basedir | sed -e 's/\/$//'`

   # Check that we have permission to write in $basedir
   if test -d $basedir; then
     if test -w $basedir; then
        if [ -x $basedir/eclipse ]; then
           echo "  $basedir/eclipse found."
           printf "  Do you want to overwrite existing installation [Y/n] ? "
           read x
           if [ " $x" = " n" -o " $x" = " N" ]; then
              echo "Aborting the installation process"
	      exit 1
           fi
        fi
     else
        echo "You do not have permission to write in $basedir"
        echo "Please check that you have the appropriate privileges to"
        echo "install in that directory."
        echo "Aborting the installation process"
        exit 1
     fi
   else
     echo ""
     echo "  Directory $basedir does not exist."
     printf "  Do you want to create it [Y/n] ? "
     read x
     if [ " $x" = " n" -o " $x" = " N" ]; then
        echo "Aborting the installation process"
	exit 1
     fi
     mkdir -p $basedir
   fi
}

##################################
## Do the actual installation
##################################

install_binaries() {

  echo "Installing the binaries ...."

  cd "$basedir"

  # Do the installation through tar, to preserve symbolic links
  # We also explicitly work on specific directories to avoid
  # copying junk files

  (cd "$current_dir"; tar cf - bin lib include share) | tar xf -

  echo "Compiling the libraries ...."

  (PATH=$basedir/bin:$PATH
   export PATH
   gnatmake -a -p -P jni -XObject_Dir="$current_dir/tmp_build_obj" -XBuild=Production -XExternal_Build=false &&
   gnatmake -p -P ajis -XObject_Dir="$current_dir/tmp_build_obj" -XBuild=Production -XExternal_Build=false &&
   exit $?)
}

##
##  Write the end message
##
end_message() {
   clear
   cat <<EOF

   The GNAT Ada-Java Interfacing suite has now been installed on your machine.

   Make sure that GNAT-AJIS tools are on your path by typing one of the
   following commands:

   for csh and tcsh shells:
      setenv PATH $basedir/bin:\$PATH
      setenv LD_LIBRARY_PATH $basedir/lib/:\$LD_LIBRARY_PATH
      setenv CLASSPATH $basedir/lib/ajis.jar:\$CLASSPATH
   for sh, bash, ksh and zsh:
      PATH=$basedir/bin:\$PATH
      LD_LIBRARY_PATH=$basedir/lib/:\$LD_LIBRARY_PATH
      CLASSPATH=$basedir/lib/ajis.jar:\$CLASSPATH
      export PATH LD_LIBRARY_PATH CLASSPATH

EOF
}

##
## Write failure message
##
fail_message() {
    cat <<EOF

    Installation failed. Please report to report@adacore.com

EOF
}

## Main program

ask_gnatdir
install_binaries
if [ $? -eq 0 ]; then
    end_message
else
    fail_message
fi
