#! /bin/sh

##
## Copyright 2000 The Portland Group, Incorporated.
## All rights reserved.
##
## THE PORTLAND GROUP, INCORPORATED PROPRIETARY INFORMATION
## This software is supplied under the terms of a license agreement
## or nondisclosure agreement with The Portland Group and may not be
## copied or disclosed except in accordance with the terms of that
## agreement.
## 

# Just in case
PATH=/usr/bin:/bin:$PATH

sed_line() {
    cat >> $sed << EOF
$1 $2
EOF
}

noexec=1
cdk=0

while test -n "$1"; do 
    case $1 in 
    -n )   noexec=1 ;;
    -x )   noexec=0 ;;
    * )    start=$1 ;;
    esac
    shift
done

if test -z "$start" ; then
    echo "$0: no installation directory specified"
    exit 1
fi

if test ! -d $start ; then
    echo "$0: $start: directory not found"
    exit 1
fi

target=`uname -s | tr '[A-Z]' '[a-z]'`
if test "$target" != "linux" ; then
    echo
    echo "$0: `uname -n` not running Linux"
    exit 1
fi

rc=$start/linux86/bin/linux86rc
threads=$start/linux86/bin/libpgthread.a
new=/tmp/newrc.$$
sed=/tmp/pgi.sed.$$

# Which gcc?  PGI software needs the gcc libraries, so look for the 
# gcc libs and include files (if any)
type gcc > /dev/null 2>&1
if test $? -eq 1 ; then
    echo "ERROR: gcc not found;"
    echo "   successful use of compilers requires existence of gcc libraries"
    exit 1
fi

gccversion=`gcc --version`

# Gcc 2.96 on RH 7.0 has problems.  Use egcs instead.
#case  "$gccversion" in
#    2.96 )
#    type egcs > /dev/null 2>&1
#    if test $? -eq 1 ; then
#        echo "ERROR: egcs not found;"
#        echo "   successful use of compilers requires existence of gcc libraries"
#        exit 1
#    fi
#    gcc=egcs
#    ;;
#    * )
    gcc=gcc
#    ;;
#esac

gcclib=`$gcc -v 2>&1 | awk '/Reading/ {print $4}'`
GCCDIR=`dirname $gcclib`

# Check and see whether directory that gcc claims to exist actually
# does exist.
if test ! -d $GCCDIR ; then
    echo "ERROR: directory $GCCDIR: not found;"
    echo "   successful use of compilers requires existence of gcc libraries"
    exit 1
fi

gcc1=`dirname $GCCDIR`
gccrel=`basename $gcc1`
GCCINC=/usr/$gccrel/include

# Find the linker
LINKER=`grep dynamic $gcclib | sed -e 's/.*:-dynamic-linker //' -e 's/}} .*//'`

# Which g77?  PGI software needs the g77 libraries, so look for the g77 libs.
type g77 >/dev/null 2>&1
if test $? -eq 1 ; then
    echo "WARNING: g77 not found; this may cause problems during use"
    G77DIR='""'
else
    out=/tmp/pgi_install.$$
    g77 -v 2>$out 1>/dev/null
    g77lib=`cat $out | awk '/Reading/ {print $4}'`
    G77DIR=`dirname $g77lib`
    f2c=`cat $out | grep lf2c`
    g2c=`cat $out | grep lg2c`
    if test -n "$f2c" ; then
    G77LIB="-lf2c"
    fi
    if test -n "$g2c" ; then
    G77LIB="-lg2c"
    fi
    rm -f $out
fi

if test -n "$G77LIB" ; then
    sed_line s@LFC=\"-lf2c\"@LFC=\"$G77LIB\"@
fi

# Which dynamic linker?
if test "$LINKER" != "/lib/ld-linux.so.1" ; then 
    sed_line s@/lib/ld-linux.so.1@$LINKER@
fi

# CDK uses its libmpich.a.
if test -d "$start/cdk" ; then
    sed_line s@MPILIB=\"/usr/local/mpi/lib@MPILIB=\"$start/linux86/lib@
    MPILIB=$start/linux86/lib/libmpich.a
fi

# GLIBC issues.  Find the version of GLIBC used on this system so that
# the appropriate files are copied from lib-glibc-N to lib.
if test -L "/lib/libc.so.6" ; then
    GLIBC=`ls -l /lib/libc.so.6 | sed -e 's/.* -> libc-//' -e 's/.so//'`
elif test -x "/lib/libc.so.6" ; then
    GLIBC=`/lib/libc.so.6 | head -1 | sed -e 's/.* version //' -e 's/,.*//'`
fi

if test -n "$GLIBC" ; then
    case "$GLIBC" in
    2.0* )
        LIBGLIBC=$start/linux86/lib-glibc-20
        ;;
    2.1.1 )
        LIBGLIBC=$start/linux86/lib-glibc-211
        ;;
    2.1* )
        LIBGLIBC=$start/linux86/lib-glibc-212
        ;;
    2.2* )
        LIBGLIBC=$start/linux86/lib-glibc-22
        INCGLIBC=$start/linux86/include-glibc-22
        ;;
    esac
else
    unset GLIBC
fi

sed_line s@GCCDIR=.*@GCCDIR=$GCCDIR@
sed_line s@GCCINC=.*@GCCINC=$GCCINC@
sed_line s@G77DIR=.*@G77DIR=$G77DIR@

if test "$noexec" -eq 1 ; then
    echo "Directory: $start"
    echo "Linker: $LINKER"
    echo "GCCDIR: $GCCDIR"
    echo "GCCINC: $GCCINC"
    echo "G77DIR: $G77DIR"
    echo "G77LIB: $G77LIB"
    if test -n "$GLIBC" ; then 
        echo "Glibc 2: $GLIBC"
        echo "Glibc libs: $LIBGLIBC"
        echo "Glibc headers: $INCGLIBC"
    else
        echo "Glibc 2: no"
    fi
    if test -n "$MPILIB" ; then
    echo "MPILIB: $MPILIB"
    fi
else
    if test -n "$GLIBC" ; then 
        ( cd $LIBGLIBC; tar cf - * | ( cd $start/linux86/lib; tar xpf - ))
        case "$GLIBC" in 
        2.0* )
            rm -rf $start/linux86/include/sys
            ;;
        2.1* )
            # sed_line s@LCRTB=.*@LCRTB=$GCCDIR/crtbegin.o@
            # sed_line s@LCRTE=.*@LCRTE=$GCCDIR/crtend.o@
            sed_line s@PGIRPATH=.*@PGIRPATH=\"-rpath \$PGI/linux86/lib\"@
            ;;
        2.2* )
            ( cd $INCGLIBC; \
		tar cf - * | ( cd $start/linux86/include; tar xpf - ))
            # sed_line s@LCRTB=.*@LCRTB=$GCCDIR/crtbegin.o@
            # sed_line s@LCRTE=.*@LCRTE=$GCCDIR/crtend.o@
            sed_line s@PGIRPATH=.*@PGIRPATH=\"-rpath \$PGI/linux86/lib\"@
            sed_line s@THROW=.*@THROW=\"-D__THROW\\\\=\"@
            ;;
        esac
    fi

    # Use the virgin, compressed linux86rc if present.
    # Note that GNU sed must be v3.0 or higher.
    if test -f $rc.gz ; then 
        zcat $rc.gz | sed -f $sed > $new
    else 
        sed -f $sed < $rc > $new
    fi
    # Timestamp 
    echo \# fixlinux86 `date` >> $new
    if test -f $rc ; then
        mv -f $rc $rc.bak
    fi
    mv -f $new $rc
    chmod -f 0644 $rc $rc.bak
fi

rm -f $sed

exit 0

