#!/bin/sh 
#*****************************************************************************
# E.S.O. - VLT project
#
# buildGNU
#
# who       when      what
# --------  --------  ----------------------------------------------
# pforstma  26/01/96  created 
# gfilippi  30/01/96  included in DEC95
# counnas   13/06/96  GNU installation on HP-UX10.10
# gfilippi  19/06/96  included in JUN99
# gfilippi  16/09/96  NOV96
# gfilippi  14/11/96  NOV96 - added verification steps
# gfilippi  16/11/96  NOV96 - HP-UX10 and Solaris merged
# wpirani   01/04/97  Testing/hacking new installation in /vlt/local
# wpirani   09/04/97  More testing/hacking for installation in /vlt/gnu
# wpirani   03/11/97  Fine tuning for NOV97
# gfilipi   30/12/97  Fixed version of installed products
# wpirani   02/11/98  Existing /opt/gnu no longer removed on Solaris
#
# Installation of GNU products
#
# NOTES:
#
# * Now looks for the environment variable GNU_ROOT (usually /vlt/gnu/).
#   If not defined, uses /usr/local instead -- Werther Pirani
#
# PREREQUISITES:
#
# * $GNU_ROOT directory structure is created but empty:
#
# - bin
# - doc
# - etc
# - include
# - lib
# - man/man1
# - info
#
# 
# * $GNU_ROOT/bin is in $PATH
#
#
# * additional requirements for HP-UX-10:
#
#   * cc & make from HP already installed.
#     tested with 
#
#       $  what `which c89`
#       /usr/bin/c89:
#              HP92453-01 A.10.30 HP C Compiler
#              (LINT A.10.30 CXREF  A.10.30)
#              /usr/lib/libc: $Revision: 76.3 $
#
#
# * additional requirements for Solaris :
#
#    * /opt must be writable
#

if [ "$GNU_ROOT" = "" ]
then
     GNU_ROOT="/usr/local"
     export GNU_ROOT
fi

if [ "$1" = "-t" ]
then    set -x
else if [ "$1" != "" ]
     then  echo "usage: buildGNUforHP-UX-10 [-t]"
           exit 1
     fi
fi

#
# print header
#
TITLE="      Installation of GNU products"
BUILD_NAME="GNU"
. ./standardPrologue
GNU=${INSTALL_ROOT}/PRODUCTS/gnu

#
# get current operating system
#
build_OS=`uname -s`
build_OSV=`uname -r`

echo $SEPARATOR
echo " Installing on $build_OS version $build_OSV"

if [     ${build_OS}-${build_OSV} != "SunOS-5.4"     \
     -a  ${build_OS}-${build_OSV} != "SunOS-5.5"     \
     -a  ${build_OS}-${build_OSV} != "SunOS-5.5.1"   \
     -a  ${build_OS}-${build_OSV} != "HP-UX-B.10.10" \
     -a  ${build_OS}-${build_OSV} != "HP-UX-B.10.20" \
   ]
then
    echo "\n\tWARNING: this procedure has not been tested on this system\n"
fi

echo "\n--- Check current environment and prepare installation"

#
# check that $GNU_ROOT/doc exists: if not create it
# 
#   --> gcc tries to write there, but does not create it
# 
if [ ! -d $GNU_ROOT/doc ]
then
    mkdir $GNU_ROOT/doc
fi


#
# check Solaris requirements
#
# Now we no longer remove /opt/gnu.  Maybe there's other software
# already installed -- wpirani, 11/02/98
#
if [ ${build_OS} = "SunOS" ]
then
    #
    # check that /opt/gnu can be created
    #
    if [ -d /opt/gnu -o -h /opt/gnu ]
    then
    	echo "WARNING: Using existing /opt/gnu.\n"
    else
       if ln -s $GNU_ROOT/opt/gnu /opt/gnu 
       then
           true
       else
           echo "\n\t ERROR: /opt is not writable. Cannot continue.\n"
           exit 1
       fi
    fi
fi

#
# create a temporary directory used to expand the tar files and
# to compile
#
SRC="$GNU/$build_OS-$build_OSV"
echo "set temporary build directory: $SRC . . ."
if [ -d $SRC ]
then
    echo "\n\tthere is an old $SRC: start to remove it . . . "
    for name in `ls $SRC`
    do
        echo "\t   remove $SRC/$name . . . "
        rm -rf $SRC/$name
    done
    rmdir $SRC
    echo "\tdone"
fi
mkdir $SRC
echo "      . . . done"

# -------------------
# starting C compiler
# -------------------

echo $SEPARATOR
if [ ${build_OS} = "HP-UX" ] 
then
    #
    # start using the HP compiler
    #
    echo "starting using HP-C compiler: "
    what `which c89`
    echo "\n it should be"
    echo "/usr/bin/c89:"
    echo "        HP92453-01 A.10.30 HP C Compiler"
    echo "              (LINT A.10.30 CXREF  A.10.30)"
    echo "         /usr/lib/libc: \$Revision: 76.3 \$"

    # to use native compiler even if gcc is installed
    # in /usr/local/bin
    build_OLDPATH=$PATH
    echo $PATH | tee buildGNU.dummy
    PATH=`sed 's?/usr/local/bin??' buildGNU.dummy`
    export PATH
    hash -r
    echo $PATH
    rm buildGNU.dummy

    CC=""
    export CC
fi

if [ ${build_OS} = "SunOS" ]
then
    #
    # A SUN native compiler is not require. This old version of gcc,
    # available as binaries, is enough to start the installation process.
    #
    echo "installing the binary image of gcc-2.5.6: "
    cd $GNU_ROOT
    tar xf $GNU/../gcc-SUN/gcc-binaries-2.5.6.tar
    /opt/gnu/lib/gcc-lib/sparc-sun-solaris2/2.5.6/just-fixinc

    # to use this compiler
    build_OLDPATH=$PATH
    echo $PATH | tee buildGNU.dummy
    PATH=`sed 's?/usr/local/bin?usr/local/bin:/opt/gnu/bin?' buildGNU.dummy`
    export PATH
    hash -r
    echo $PATH
    rm buildGNU.dummy
    gcc -v 

    CC=gcc
    export CC

    # to be sure that all libraries will be found
    LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/ucblib:/usr/ccs/lib
    export LD_LIBRARY_PATH
fi

# ------
#  gzip
# ------

echo $SEPARATOR
cd $SRC
echo "gzip"
tar xf ${GNU}/gzip-1.2.4.tar
cd gzip-1.2.4
./configure --prefix=$GNU_ROOT
make
make install
echo "gzip done"

# ---------
#  texinfo
# ---------
#
# Ignore the following errors (that seem to depend on emacs)
#
# output from make install:
#
#       >        ./elisp-comp info.el
#       >       ./elisp-comp: emacs: not found
#        >      ./elisp-comp texinfo.el
#       >       ./elisp-comp: emacs: not found
#        >      ./elisp-comp makeinfo.el
#       >       ./elisp-comp: emacs: not found
#        >      ./elisp-comp texnfo-upd.el
#       >       ./elisp-comp: emacs: not found
#        >      ./elisp-comp texnfo-tex.el
#       >       ./elisp-comp: emacs: not found
#        >      ./elisp-comp texinfmt.el
#       >       ./elisp-comp: emacs: not found
#        >      ./elisp-comp informat.el
#       >       ./elisp-comp: emacs: not found
#       >        ./elisp-Acomp detexinfo.el
#       >       ./elisp-comp: emacs: not found
#        >      for file in info.elc makeinfo.elc texinfo.elc texnfo-upd.elc  texnfo-tex.elc texinfmt.elc informat.elc detexinfo.elc; do
#        >      .././install.sh -c -m 644 $file $GNU_ROOT/lib/emacs/site-lisp\
#         done
#       >       install:  info.elc does not exist
#       >       .       *** Error code 1
#       >       Stop.
#       >       *** Error code 1
#       >       Stop.
#
echo $SEPARATOR
cd $SRC
echo "texinfo"
$GNU_ROOT/bin/gunzip  ${GNU}/texinfo-3.6-MAY97.tar.gz -c | tar xf -
cd texinfo-3.6-MAY97
./configure --prefix=$GNU_ROOT

ONLY_FOR_SUN=""
if [ ${build_OS} = "SunOS" ]
then
    #
    # A SUN native compiler is not require. This old version of gcc,
    # available as binaries, is enough to start the installation process.
    #
    mv ./Makefile ./Makefile.orig
    sed 's/CFLAGS = +O3 -Aa -D_HPUX_SOURCE/CFLAGS = /g' ./Makefile.orig > ./Makefile
    mv ./info/Makefile ./info/Makefile.orig
    sed 's?LIBS = $(TERMLIBS) -L../libtxi -ltxi?LIBS = $(TERMLIBS) -L../libtxi -ltxi -ltermcap -lucb?g' ./info/Makefile.orig > ./info/Makefile
    mv ./libtxi/Makefile ./libtxi/Makefile.orig
    sed 's?RANLIB = ranlib?RANLIB = /bin/true?g' ./libtxi/Makefile.orig > ./libtxi/Makefile
    ONLY_FOR_SUN="CC=gcc"
fi

make $ONLY_FOR_SUN
make $ONLY_FOR_SUN install
if [ -f $GNU_ROOT/bin/makeinfo ]
then
    echo "\n   --> Ignore the above errors due to >>emacs: not found<<\n"
    echo "$GNU_ROOT/bin/makeinfo  has been correctly done!"
else
    echo "\n\t ERROR: makeinfo not in $GNU_ROOT/bin "
    echo "\t\t this may cause other error messages in the following modules "
    echo "\n\t Please chech the output"
fi

# -------------------
# binutils (gas etc.)
# -------------------
echo $SEPARATOR
cd $SRC
echo "binutils"
$GNU_ROOT/bin/gunzip ${GNU}/binutils-2.7.tar.gz -c | tar xf -
cd binutils-2.7
#
echo "\n\t -->  ld is not supported. Ignore the related warning. \n" 
./configure --prefix=$GNU_ROOT
make CFLAGS="-O"
make install

# -----------------
# NOTE NOTE NOTE NOTE: On HP 10.20 standard sed causes major problems during
# the fixincludes phase.  We need to build GNU sed first and be sure to
# use that.
#
# Werther Pirani, July 23 1997
# -----------------

# ---
# sed
# ---
echo $SEPARATOR
echo "sed"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/sed-2.05.tar.gz -c | tar xf -
cd sed-2.05
./configure --prefix=$GNU_ROOT
make clean
make CFLAGS="-O2"
make install
hash -r

# ------------
# gcc-compiler
#
# We can build gcc *NOW*
#
# Werther Pirani, July 23 1997
#
# ------------
echo $SEPARATOR
echo "GCC"
cd  $SRC
$GNU_ROOT/bin/gunzip ${GNU}/gcc-2.7.2.2-patch.tar.gz -c | tar xf -
cd  $SRC/gcc-2.7.2.2
#if [ "${build_OS}-${build_OSV}" = "HP-UX-B.10.20" ]
#then
#   echo "Yep!"
#   mkdir include
#   mkdir include/.unsupp
#fi
./configure --with-gnu-as --prefix=$GNU_ROOT

echo $SEPARATOR
echo "GCC stage 0/1/2"
if [ ${build_OS} = "SunOS" ] 
then
    #
    # patch for RTAP
    #
    sed 's/rodata/rodata1/g' ./config/sparc/sysv4.h > ./config/sparc/sysv4.h.new
    chmod u+w ./config/sparc/sysv4.h
    cp ./config/sparc/sysv4.h.new ./config/sparc/sysv4.h
    make LANGUAGES="c c++" CC=gcc CFLAGS="-O2"
else
    make LANGUAGES="c c++" CFLAGS="-O"
fi

echo "GCC stage 3"
make stage3
make all.indirect CC="stage3/xgcc -Bstage3/" CFLAGS="-O2" LANGUAGES="c c++"
make install  CC="stage3/xgcc -Bstage3/" CFLAGS="-O2" LANGUAGES="c c++"


if [ ${build_OS} = "SunOS" ] 
then
    #
    # RtapUnlockExe is capable to work only on files treated by the SUN-ld
    # The GNU-ld shall be deleted:
    mv $GNU_ROOT/sparc-sun-solaris2.4/bin/ld $GNU_ROOT/sparc-sun-solaris2.4/bin/ld.becauseOfRtapUnlockExe
    # to use the new compiler
    PATH=${build_OLDPATH}
    export PATH
    echo $PATH
    hash -r
    echo "from now using:"
    gcc -v
fi

if [ ${build_OS} = "SunOS" ]
then
    #
    # get rid of the old gcc version used at the beginning,
    #
    echo "remove gcc-2.5.6: "
    rm -rf $GNU_ROOT/opt/gnu
    rm -f  /opt/gnu

    # to use the new compiler
    PATH=${build_OLDPATH}
    export PATH
    echo $PATH
    hash -r
    echo "from now using:"
    gcc -v 

fi

# ------
# libg++
# ------
echo $SEPARATOR
echo "libg++"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/libg++-2.7.2.tar.gz -c | tar xf -
cd libg++-2.7.2
CC=gcc
export CC
./configure --prefix=$GNU_ROOT  --enable-shared

make clean
make all CC="gcc" CFLAGS="-O2" CXXFLAGS="-O2"
rm -rf $GNU_ROOT/lib/g++-include/*.h

make install

# -----
#  gdb
# -----
echo $SEPARATOR
echo "gdb"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/gdb-4.16.tar.gz -c | tar xf -
cd gdb-4.16
./configure --prefix=$GNU_ROOT
make CC="gcc" CFLAGS="-O2"
make install CC="gcc" CFLAGS="-O2"

# ------
#  make
# ------
echo $SEPARATOR
echo "make"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/make-3.75.tar.gz -c | tar xf -
cd make-3.75
./configure --prefix=$GNU_ROOT
make CFLAGS="-O2"

if [ ${build_OS} = "SunOS" ] 
then
    NATIVE_MAKE=/usr/ccs/bin/make
fi
if [ ${build_OS} = "HP-UX" ] 
then
    NATIVE_MAKE=/usr/bin/make
fi

# because a previos version of make is not there, the following error
# is produced:
#        mv $GNU_ROOT/bin/make $GNU_ROOT/bin/make.old
#   mv: $GNU_ROOT/bin/make: cannot access: No such file or directory

# because  "." is in the PATH, make install will fail because:
# >     mv -f make.new make
# >     mv: make: cannot write: Text file busy
# >     make: *** [make] Error 1
# for this reason the native make is used:
$NATIVE_MAKE install

# -------
#  bison
# -------
echo $SEPARATOR
echo "bison"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/bison-1.25.tar.gz -c | tar xf -
cd bison-1.25
./configure --prefix=$GNU_ROOT
make CFLAGS="-O2"
make install
echo "bison done"

# ------
#  flex
# ------
echo $SEPARATOR
echo "flex"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/flex-2.5.4.tar.gz -c | tar xf -
cd flex-2.5.4
./configure  --prefix=$GNU_ROOT
make CFLAGS="-O2"
make check
make install

# ------
#  gawk
# ------
echo $SEPARATOR
echo "gawk"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/gawk-3.0.2.tar.gz  -c | tar xf -
cd gawk-3.0.2  
./configure gnu --prefix=$GNU_ROOT
make CC=gcc CFLAGS="-O2"
#  make install installs only gawk: the man-page gawk.1 is missing
#  >  cp gawk $GNU_ROOT/bin/gawk && chmod 755 $GNU_ROOT/bin/gawk
#  >  cp gawk.1 $GNU_ROOT/man/man1/gawk.1 && chmod 644 $GNU_ROOT/man/man1/gawk.1
#  > cp: cannot access gawk.1: No such file or directory
#  > *** Error code 1
#  >
#  >Stop.
#
make CC=gcc CFLAGS="-O2" install
echo "\n   --> Ignore the >>cp: cannot access gawk.1<<  It is a bug in the distribution kit\n"

# -------
#  emacs
# -------
echo $SEPARATOR
echo "emacs"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/emacs-19.33.tar.gz -c | tar xf -
cd emacs-19.33
# no need specify hardware or X11 info (despite what is written in INSTALL)
./configure --prefix=$GNU_ROOT
make clean
make CFLAGS="-O2"
make install

#
# THE FOLLOWING PRODUCTS ARE REQUIRED BY CMM
#

# ---------
# diffutils
# ---------
echo $SEPARATOR
echo "diffutils"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/diffutils-2.7.tar.gz -c | tar xf -
cd diffutils-2.7
./configure --prefix=$GNU_ROOT
make clean
make CFLAGS="-O2"
make install

# ---
# sed (again, but this time using gcc -- just in case) wpirani
# ---
echo $SEPARATOR
echo "sed (using gcc)"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/sed-2.05.tar.gz -c | tar xf -
cd sed-2.05
./configure --prefix=$GNU_ROOT
make clean
make CFLAGS="-O2"
make install

# ---
# rcs
# ---
echo $SEPARATOR
echo "rcs"
cd $SRC
$GNU_ROOT/bin/gunzip ${GNU}/rcs-5.7.tar.gz -c | tar xf -
cd rcs-5.7
./configure --prefix=$GNU_ROOT
make CFLAGS="-O2"
make install


# -------------
# Verification
# -------------
echo $SEPARATOR
echo "\n                          VERIFICATION\n"
cd $SRC
#
# compare all the installed files with the reference list
#
echo $SEPARATOR
reference=$GNU/GNU.$build_OS-$build_OSV
echo "check that all expected files have been created "
echo "(reference list: $reference) "
cat  $reference | grep -v "###" | xargs -i ${INSTALL_ROOT}/INSTALL/buildCheckFileExist \{\}
echo "\n\t If missing files, please chech the output for possible errors."

#
# be sure that all new commands are known
#
hash -r

echo $SEPARATOR
if [ -f $GNU_ROOT/bin/gcc ]
then
    echo "gcc should be 2.7.2.2"
    which gcc
    gcc -v 
else
    echo "\n\t ERROR: gcc not in $GNU_ROOT/bin "
    echo "\n\t Please chech the output"
fi

echo $SEPARATOR
if [ -f $GNU_ROOT/lib/libstdc++.a ]
then
    echo "libstdc++.a should be in $GNU_ROOT/lib"
    what $GNU_ROOT/lib/libstdc++.a 
else
    echo "\n\t ERROR:  bstdc++.a not in $GNU_ROOT/lib "
    echo "\n\t Please check the output"
fi

echo $SEPARATOR
if [ -f $GNU_ROOT/bin/make ]
then
    echo "make should be 3.75"
    which make
    $GNU_ROOT/bin/make -version  
else
    echo "\n\t ERROR: make not in $GNU_ROOT/bin "
    echo "\n\t Please chech the output"
fi

echo $SEPARATOR
if [ -f $GNU_ROOT/bin/gdb ]
then
    echo "gdb should be 4.16"
    what `which gdb`
    gdb -v 
else
    echo "\n\t ERROR: gdb not in $GNU_ROOT/bin "
    echo "\n\t Please chech the output"
fi

echo $SEPARATOR
if [ -f $GNU_ROOT/bin/gawk ]
then
    echo "gawk should be GNU Awk 3.0"
    what `which gawk`
    #gawk -W  version
else
    echo "\n\t ERROR: gawk not in $GNU_ROOT/bin "
    echo "\n\t Please chech the output"
fi

echo $SEPARATOR
if [ -f $GNU_ROOT/bin/emacs ]
then
    echo "emacs should be 19.33"
    what `which emacs`
else
    echo "\n\t ERROR: emacs not in $GNU_ROOT/bin "
    echo "\n\t Please chech the output"
fi

echo $SEPARATOR
if [ -f $GNU_ROOT/bin/diff ]
then
    echo "diff should be 2.7: "
    what `which diff`
    diff -v
else
    echo "\n\t ERROR: diff not in $GNU_ROOT/bin "
    echo "\n\t Please chech the output"
fi

echo $SEPARATOR
if [ -f $GNU_ROOT/bin/sed ]
then
    echo "sed should be 2.05: "
    what `which sed`
    sed --version
else
    echo "\n\t ERROR: sed not in $GNU_ROOT/bin "
    echo "\n\t Please chech the output"
fi

echo $SEPARATOR
if [ -f $GNU_ROOT/bin/rcs ]
then
    echo "rcs should be 5.7: "
    what `which rcs`
    rcs -V
else
    echo "\n\t ERROR: rcs not in $GNU_ROOT/bin "
    echo "\n\t Please chech the output"
fi


# bye bye
#
echo $SEPARATOR
echo "\007"; sleep 1;echo "\007"; sleep 1;echo "\007"; sleep 1
echo "The installation is terminated. Please check the output"
echo "for possible errors.\n"
echo "If you want to save space, delete"
echo " \t\t $SRC"

. $CWD/standardEpilogue

#
#___oOo___
