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

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

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

prefix="/usr/local/gnat"
xprefix=""
: ${TMPDIR:=`pwd`}
tmpout="${TMPDIR}/install.log"
tmpvalue="${TMPDIR}/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*|[0-9].[0-9].[0-9])
	qualifier="Pro"
	long_qualifier="GNAT Pro"
	;;
    [0-9][0-9][0-9][0-9]*)
	qualifier="GPL"    
	long_qualifier="GNAT GPL Edition" 
	;;
    esac
}

###########################################
#                                         #
# Ask for the base installation directory #
#                                         #
###########################################

ask_base_prefix() {

   cat << EOF

    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 the installation directory of the cross system #
#                                                    #
######################################################

ask_xprefix () {

    check_dir=$1
    name="$2"
    if [ "$name" = "" ] ; then
        name=$machine
    fi
    clear
    check="nok"
    while [ "$check" != "ok" ]; do
	cat <<EOF


  The installation could not find the original 
  $name 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
   touch $tmpout &&
     (make ins-all prefix=$prefix 2>&1; echo $? > $tmpvalue ) | tee $tmpout

   # Check that installation was OK

   error_exit=false

   if [ ! -f "$tmpout" ]; then
      error_exit=true
      cat << EOF


   An error occurred during installation. The installation log file,
   $tmpout, could not be written.
EOF

   elif [ ! -f "$tmpvalue" -o `cat $tmpvalue` -ne 0 ]; then
      error_exit=true
      cat << EOF


   An error occurred during installation. You can find a complete log
   of the installation in $tmpout.
EOF
   fi

   if $error_exit; then
      cat << EOF
   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
   fi
   
   rm -f $tmpvalue

   case $machine in
      *darwin*)
         # On Mac OS, gdb needs to be setgid procmod.
         clear

         cat << EOF
   On Mac OS X, the debugger needs to have special permissions in
   order to be able to function.

   This requires administrator privileges; you need to be root or
   be listed in the sudoers file in order to do this now.

   If this is not the case, this can be done at any time without
   affecting the rest of the tools.

   Would you like to set these permissions now ? [yY|nN]: 
EOF

         read carryon
         case $carryon in
            [yY]*)
               cd $prefix/bin
               sudo chgrp procmod gdb
               sudo chmod g+s gdb
               ;;
            *)
            cat << EOF
    The permissions for gdb have not been set. In order to use the
    debugger, please ask an administrator to run the following
    commands as root:
    
        cd $prefix/bin
        chgrp procmod gdb
        chmod g+s gdb

EOF
	    ;;
         esac
      
      ;;
      *) clear ;; 
   esac

   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
}

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

set_qualifier
check_env

# This avoids some problems when cd prints the new directory when CDPATH
# is defined
unset CDPATH

clear
cat << EOF
                                                                                
                                                                                
   This script is provided to simplify the installation of the 
     $machine
   binary version of the $long_qualifier Ada (2005, 95, and 83)
   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"
curdir=`pwd`
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.
EOF
    ask_base_prefix                                                                           
    while [ "$prefix" = "$curdir" ]; do
       #target base directory cannot be the same as the current dir
       cat << EOF

    The target base directory cannot be the current directory.
    Please enter another directory name.
EOF
       ask_base_prefix
    done

    # 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*|i386-elf-lynxos5)
	if [ "x$ENV_PREFIX" != "x" ]; then
	    xprefix=$ENV_PREFIX
	else
	    ask_xprefix lib/thread
	fi
	;;
    *elinos-linux)
        if [ "x$ELINOS_PROJECT" != "x" ]; then
            xprefix=$ELINOS_PROJECT
        else
            ask_xprefix rtai "ELinOS/Xenomai kernel project"
        fi
        ;;
    *-elf-pikeos)
        os="PikeOS"
        if [ "$PIKEOS_PREFIX" != "" ]; then
            xprefix=$PIKEOS_PREFIX
        else
            ask_xprefix cdk/ppc/oea/bin "PikeOS"
        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*|i386-elf-lynxos5)
	# 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
	# Setup up soft links to the correct versions of the system
	# libraries in the -mthreads and -mthreads/-msoft-float cases
	for file in $xprefix/lib/thread/*.a; do
	    b=`basename $file`
	    rm -f lib/gcc/$machine/$basever/thread/$b
	    ln -s $file lib/gcc/$machine/$basever/thread
	    if [ -d lib/gcc/$machine/$basever/thread/soft-float ]; then
		rm -f lib/gcc/$machine/$basever/thread/soft-float/$b
		ln -s $file lib/gcc/$machine/$basever/thread/soft-float
	    fi
	done
	;;
    *-wrs-vxworksae|*-wrs-vxworksmils)
	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
            case `uname` in
               SunOS) echo $xprefix/target/usr/lib/pentium/PENTIUM/common \
                         >> 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;;
            esac
        fi
        ;;
    *-elinos-linux)
        elinos_cdk=`(source $xprefix/ELINOS.sh >/dev/null 2>&1; echo $ELINOS_CDK)`
        elinos_target=`(source $xprefix/ELINOS.sh >/dev/null 2>&1; echo $ELINOS_TARGET)`
        xenomai=`(cd $xprefix/xenomai-[0-9.]* ; pwd)`

        elinos_lib=$elinos_cdk/$elinos_target
        xenomai_lib=$xenomai/xenomai-build/usr/realtime/lib

        if [ ! -d $elinos_lib/lib ]; then
            cat <<EOF
WARNING:
  The installation script was unable to find the ELinOS libraries.
  This directory does not exist:

     $elinos_lib/lib

  These libraries are needed by the debugger. Please contact your system
  administrator if necessary.

  Press Return to continue.
EOF
            read dummy
        fi
        if [ ! -d $xenomai_lib ]; then
            cat <<EOF
WARNING:
  The installation script was unable to find the Xenomai libraries.
  This directory does not exist:

     $elinos_lib

  These libraries are needed by the run-time and by the debugger. Please
  contact your system administrator if necessary.

  Press Return to continue.
EOF
            read dummy
        fi
        echo adalib > lib/gcc/$machine/$basever/rts-xenomai/ada_object_path
        echo $xenomai_lib \
            >> lib/gcc/$machine/$basever/rts-xenomai/ada_object_path
        echo "set solib-absolute-prefix $elinos_lib" > bin/$machine-gdbinit
        echo "set solib-search-path $xenomai_lib:$elinos_lib/lib" \
            >> bin/$machine-gdbinit
        ;;
   *-elf-pikeos)
        mkdir -p powerpc-elf-pikeos/bin
        mkdir -p powerpc-elf-pikeos/lib
        cp -p $xprefix/cdk/ppc/oea/powerpc-unknown-elf/bin/* powerpc-elf-pikeos/bin
        cp -p $xprefix/target/ppc/oea/scripts/app-ld-script powerpc-elf-pikeos/lib
	for f in powerpc-elf-pikeos/bin/*; do
            cp $f bin/powerpc-elf-pikeos-`basename $f`
        done
        echo "adalib" >> lib/gcc/$machine/$basever/rts-ravenscar-sfp/ada_object_path
        echo "$xprefix/target/ppc/oea/lib" >> lib/gcc/$machine/$basever/rts-ravenscar-sfp/ada_object_path
        echo "adalib" >> lib/gcc/$machine/$basever/rts-ravenscar-apex/ada_object_path
        echo "$xprefix/target/ppc/oea/apex/lib" >> lib/gcc/$machine/$basever/rts-ravenscar-apex/ada_object_path
        echo "$xprefix/target/ppc/oea/lib" >> lib/gcc/$machine/$basever/ada_object_path
        ;;
esac

# Do the real installation
standard_installation


