#!/bin/bash

# -------  fin -------
function fin() {
  rm -f $outname
  mv $tmpout $outname
  chmod +x $outname
  echo    Configuration complete.  Run $outname to do the installation.
  echo    DO NOT FORGET: put $bindir at the front of your PATH
  echo ""
  exit
}

function no-gccinstall() {
  cat << EOF
  GCC-$basever not installed in standard locations.
  cannot do this type of installation
EOF
  rm -f $outname
  exit 1
}

function no-stdinstall() {
  cat << EOF
  Some GCC-$basever files were found.  They should be removed before
  using this option, or you should choose another option.
EOF
  rm -f $outname
  exit 1
}

# --------------------------- Option 1 selected -----------------------
function install-top() {
  clear
  prefix=/usr
  bindir=$prefix/bin
  libsubdir=$prefix/lib/gcc-lib/$machine/$basever
  if [ ! -d $bindir ]; then no-gccinstall; fi
  if [ ! -f $bindir/gcc ]; then no-gccinstall; fi
  top_basever=`$bindir/gcc -dumpversion`
  top_machine=`$bindir/gcc -dumpmachine`
  top_libsubdir=$prefix/lib/gcc-lib/$top_machine/$top_basever
  if [ ! -d $top_libsubdir ]; then no-gccinstall; fi
  if [ $libsubdir != $top_libsubdir ]; then \
    ln -s $libsubdir $top_libsubdir || exit 1;\
  fi
  cat << EOF
    The installation on top of GCC-$basever will install:
      In $bindir :
          gnatbind gnatbl gnatf

      In $libsubdir :
          gnat1
          libgnat.a, and libgthreads.a

      In $prefix/adainclude :
          The source files of the RTL

      In $libsubdir/adalib :
          The object files of the RTL
EOF
  echo make ins-gnatstuff >$tmpout
  fin
}

# --------------------------- Option 2 selected -----------------------
function install-std() {
  clear
  prefix=/usr
  bindir=$prefix/bin
  libsubdir=$prefix/lib/gcc-lib/$machine/$basever
  if [ -d $libsubdir ]; then no-stdinstall; fi
  cat << EOF
    The installation in the GCC standard locations will install:
      In $bindir :
          gcc gnatbind gnatbl

      In $libsubdir :
          gnat1 cc1 cpp ld libgcc.a specs
          libgnat.a, and libgthreads.a

      In $prefix/adainclude :
          The source files of the RTL

      In $libsubdir/adalib :
          The object files of the RTL
EOF
  echo make ins-all >$tmpout
  fin
}

# --------------------------- Option 3 selected -----------------------
function install-non() {
  clear
  cat << EOF


    To install GNAT is a non-standard location you need to specify a
    base directory.  All the files will be installed in subdirectories
    that are created under this directory.

    Specify the base directory you want to use for installation:

EOF
  read basedir
  while true; do
    if [ X = X`echo $basedir|sed -n -e 's%^/.*%/%p'` ]; then\
      basedir=`pwd`/$basedir;\
    fi;\
    echo "    The base directory is $basedir";\
    echo "    To accept this choice enter RETURN.";\
    echo "    Otherwise type a new name.";\
    read answer;\
    if [ X$answer = X ]; then break; fi;\
    basedir=$answer;\
  done
  clear
  prefix=$basedir
  bindir=$prefix/bin
  libsubdir=$prefix/lib/gcc-lib/$machine/$basever
  cat << EOF

    The installation of GNAT will install:
      In $bindir :
          gcc gnatbind gnatbl

      In $libsubdir :
          gnat1 cc1 cpp ld libgcc.a specs
          libgnat.a, and libgthreads.a

      In $prefix/adainclude :
          The source files of the RTL

      In $libsubdir/adalib :
          The object files of the RTL

EOF
  echo make ins-all prefix=$prefix >$tmpout

  cat << EOF >env-vals
export GCC_EXEC_PREFIX=$libsubdir/
export ADA_INCLUDE_PATH=$prefix/adainclude
export ADA_OBJECTS_PATH=$libsubdir/adalib
export C_INCLUDE_PATH=$libsubdir/include
EOF

  cat << EOF
    The file env-vals has been created.  It contains the csh commands
    to set the environment variables you need to use GNAT.  These
    commands (or their equivalent for the shell you use) should be
    placed in your .cshrc (or equivalent) file.

EOF
fin
}

basever=`./xgcc -dumpversion`
machine=`./xgcc -dumpmachine`
outname=doinstall
tmpout=tmp-doinstall

rm -f $tmpout

cat << EOF


  This script is provided to simplify the installation of the $machine
  binary version of the GNU NYU Ada Translator (GNAT).

  This script asks a few questions about how you want GNAT configured
  and then creates a file in this directory which can then be invoked
  to do the actual installation.  Running this configuration script will
  not modify anything anywhere else in your system.  You can break
  out of it or run it multiple times before doing the actual installation.

  Hit RETURN to continue.
EOF

read
clear
cat << EOF


  There are basically 3 options for installation:

  1) Install GNAT-specific files on top of an existing GCC $basever
     installation (must be in standard locations -- will likely
     require root permission).

  2) Install GCC C compiler and GNAT files in the standard  GCC locations.
     (Note: This includes directories under /usr.  On most
      systems, this requires root permission).

  3) Install GCC C compiler and GNAT files in non-standard locations
     that you will specify.


  Options 1 and 2 provide simplest and most flexible use of GNAT.
  Option 3 requires users to set some environment variables.

EOF
while true; do \
  echo Type 1, 2, or 3 "(then RETURN)" to choose an option:; \
  read answer; \
  case $answer in \
  1)\
    install-top\
    ;;\
  2)\
    install-std\
    ;;\
  3)\
    install-non\
    ;;\
  esac;\
done
