#! /bin/sh
#
# GNAT binary distribution installation script
# Copyright (c) 2003, Free Software Foundation
#

####################
#                  #
# Global variables #
#                  #
####################

basever=`./xgcc -dumpversion`
machine=`./xgcc -dumpmachine`
gnatver="3.17w"

prefix="/usr/local/gnat"
xprefix=""
tmpout="install.log"
tmpvalue="install.$$"

#####################################################
#                                                   #
# Says that a given command was not found and exits #
#                                                   #
#####################################################

command_not_found () {
    cmd="$1"
    cat << EOF

The $cmd command could not be found on your PATH. It is required to have
it in your PATH in order to install successfully $long_qualifier. Please
add the directory were $cmd can be found on your PATH or contact your
system administrator to have it installed in a standard location.


EOF
    exit 1
}

##########################
#                        #
# Checks the environment #
#                        #
##########################

check_env () {

    # type returns 0 on success, >0 on failure
    for cmd in make tee cat clear; do
	type_out=`type $cmd 2>&1`
	type_rv=$?
	if [ $type_rv -ne 0 ]; then
	    command_not_found $cmd
	fi
    done 
}

################################
#                              #
# Adds the correct description #
#                              #
################################

set_qualifier () {
    case $gnatver in
    *w|*a*)
	qualifier="Pro"
	long_qualifier="GNAT Pro"
	;;
    [0-9][0-9][0-9][0-9]*)
	qualifier="GPL"    
	long_qualifier="GNAT GPL Edition" 
	;;
    esac
}

######################################################
#                                                    #
# Ask the installation directory of the cross system #
#                                                    #
######################################################

ask_xprefix () {

    check_dir=$1
    clear
    while [ "x$check" != "xok" ]; do
	cat <<EOF


  The installation could not find the original 
  $machine installation directory.
  Please specify the base $os installation dir:

EOF
	read xprefix
	if [ -d $xprefix/$check_dir ]; then
	    check="ok"
	fi
    done
}

is_vxworks_6 () {
    if [ "x`echo $machine | grep "\-wrs\-vxworks$"`" != "x" \
        -a -d lib/gcc/$machine/$basever/rts-rtp ]; then
        return 0
    else
        return 1
    fi
}

#######################################
#                                     #
# Install GNAT to a standard location #
#                                     #
#######################################

standard_installation () {
 
   # Check if there's a former installation of GNAT
   if [ -d $prefix/lib/gcc-lib/$machine/$basever -o \
        -d $prefix/lib/gcc/$machine/$basever ]; then
      clear	
      cat << EOF


   Some gcc-$basever files were found in 
      $prefix
   If you want to exit the installation process and cleanup this
   directory manually, press 'N'. If you want to proceed with
   installation, press 'Y'.

   Do you want to proceed anyway ? [yY|Nn]:

EOF
      read carryon
      case $carryon in
      [yY]*)
	 ;;
      *)
	 echo "  Installation interrupted on user's request"
	 exit 1
	 ;;
      esac
   fi

   # Makes the real installation
   (make ins-all prefix=$prefix 2>&1; echo $? > $tmpvalue ) | tee $tmpout

   # Check that installation was OK
   if [ `cat $tmpvalue` -ne 0 ]; then
      cat << EOF


   An error occurred during installation. You can find a complete log
   of the installation in $tmpout.
   Don't hesitate to send a message to report@adacore.com
   with you customer number on the subject line if you have any
   question about this installation process.

EOF
      exit 1
   else
      rm -f $tmpvalue
      clear
      cat << EOF


   GNAT $qualifier is now installed. To launch it, you must put
      $prefix/bin 
   in front of your PATH environment variable. The following
   commands enable you to do this:
      PATH=$prefix/bin:\$PATH; export PATH  (Bourne shell)
      setenv PATH $prefix/bin:\$PATH        (C shell)
   Thank you for installing $long_qualifier!

EOF
   fi
}

######################
#                    #
# Start installation #
#                    #
######################

set_qualifier
check_env

clear
cat << EOF
                                                                                
                                                                                
   This script is provided to simplify the installation of the 
     $machine
   binary version of the $long_qualifier Ada 95 environment maintained by
   AdaCore. For information on commercial support please contact 
   sales@adacore.com.

   This script will ask you how you would like to configure GNAT.
   Confirmation is required before any write action is taken.

   Please press RETURN to continue.

EOF

read dummy

# Ask information for non-standard installation
confirm="KO"
while [ "x$confirm" != "xOK" ]; do

    # Ask the base directory for installation
    clear
    cat << EOF
									    
   									    
    To install GNAT, you need to specify a base directory.  
    All the files will be installed in subdirectories
    that are created under this directory.

    Important Note: You should not use ~ or ~username wildcards
    when specifying this directory name.
                                                                                
    In which directory do you want to install GNAT? [$prefix]: 
EOF
    read ans_prefix

    if [ "x$ans_prefix" != "x" ]; then
	prefix=$ans_prefix
    fi

    # Ask confirmation
    cat << EOF


    The $long_qualifier installation directory will be:
      $prefix
    Is this correct ? Type 'Y' if so, otherwise type 'N' and you'll
    be prompted for another directory name.

    Do you want to continue ? [yY|nN]:

EOF
    read confirm
    case $confirm in
    [yY])
	confirm="OK"
	;;
    *)
    	confirm="KO"
	;;
    esac
done

# Prepare installation of some cross compilers
case $machine in
    powerpc-*-lynxos)
	if [ "x$ENV_PREFIX" != "x" ]; then
	    xprefix=$ENV_PREFIX
	else
	    ask_xprefix lib/thread
	fi
	;;
    *-wrs-vxworksae)
	if [ "x$WIND_BASE" != "x" ]; then
	    xprefix=$WIND_BASE
	else
	    ask_xprefix host/resource/tcl
	fi
	;;
    *-wrs-vxworks)
        if is_vxworks_6; then 
            os="VxWorks 6"
            if [ "x$WIND_BASE" != "x" ]; then
                xprefix=$WIND_BASE
            else
                ask_xprefix target/usr/lib/
            fi
        fi
	;;
esac

# Ask confirmation
clear
cat << EOF
                                                                                
                                                                                
   $long_qualifier is now about to be installed in 
      $prefix.
   Type 'Y' if you want to proceed with installation or any other key
   if you wish to abort.

   Do you want to proceed with installation ? [yY|nN]:
                                                                                
EOF
read proceed
case $proceed in
[yY]*)
    ;;
*)
    echo "Aborting installation on user request"
    exit 0
    ;;
esac

# Prepare installation of some cross compilers
case $machine in
    powerpc-*-lynxos)
	# Setup soft links for lib and include from in CDK install
	# These are copied into install prefix by make ins-all.
	if [ -d $machine/lib ]; then rmdir $machine/lib; fi
	rm -f $machine/lib $machine/include
	ln -s $xprefix/lib $machine/lib
	ln -s $xprefix/usr/include $machine/include
	;;
    *-wrs-vxworksae)
	cp gdb.tcl $xprefix/host/resource/tcl > /dev/null 2>&1
	if [ $? -ne 0 ]; then
	    cat <<EOF


WARNING:
  The installation script was unable to copy the following files to
  $xprefix/host/resource/tcl:
                                                                                
    gdb.tcl
                                                                                
  This file is needed for the debugger. Please contact your system
  administrator if necessary.
                                                                                
  Press Return to continue.

EOF
	    read dummy
	fi
	;;
    powerpc-wrs-vxworks)
        if is_vxworks_6; then
            echo adalib \
                > lib/gcc/$machine/$basever/rts-rtp/ada_object_path
            echo $xprefix/target/usr/lib/ppc/PPC32/common \
                >> lib/gcc/$machine/$basever/rts-rtp/ada_object_path
        fi
        ;;
    i586-wrs-vxworks)
        if is_vxworks_6; then
            echo adalib \
                > lib/gcc/$machine/$basever/rts-rtp/ada_object_path
            echo $xprefix/target/usr/lib/simpentium/SIMPENTIUM/common \
                >> lib/gcc/$machine/$basever/rts-rtp/ada_object_path
        fi
        ;;
esac

# Do the real installation
standard_installation


