#! /bin/sh

##
## Copyright 1999,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

while test -n "$1"; do 
    case $1 in 
    -n )    noexec=1 ;;
    * )    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: directory '$start' 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/libpthread.a
new=/tmp/newrc.$$
sed=/tmp/pgi.sed.$$

# PGI software needs the gcc libraries, so look for the gcc/g77 libs
# and include files (if any)
type gcc > /dev/null 2>&1
status=$?
if test $status -eq 1 ; then
    echo ""
    echo "WARNING: gcc not found;"
    echo "   successful use of compilers requires existence of gcc libraries"
    echo ""
    GCCDIR='""'
    GCCINC='""'
else
    gcclib=`gcc -v 2>&1 | awk '/Reading/ {print $4}'`
    GCCDIR=`dirname $gcclib`
    gcc1=`dirname $GCCDIR`
    gccrel=`basename $gcc1`
    GCCINC=/usr/$gccrel/include
    # find the linker
    LINKER=`grep dynamic $gcclib | sed -e 's/.*:-dynamic-linker //' -e 's/}} .*//'`
fi

type g77 >/dev/null 2>&1
status=$?
if test $status -eq 1 ; then
    echo ""
    echo "WARNING: g77 not found; this may cause problems during use"
    echo ""
    G77DIR='""'
else
    g77lib=`g77 -v 2>&1 | awk '/Reading/ {print $4}'`
    G77DIR=`dirname $g77lib`
fi

if test "$LINKER" != "/lib/ld-linux.so.1" ; then 
    cat > $sed << EOF
s@/lib/ld-linux.so.1@$LINKER@
EOF
fi

# GLIBC issues

if test -x "/lib/libc.so.6" ; then
    GLIBC="yes"
    version=`/lib/libc.so.6 | head -1 | sed -e 's/.* version //' -e 's/,.*//'`
    case "$version" in
    2.0.* )
	# default
        ;;
    2.1.1 )
        LIBGLIBC=$start/linux86/lib-glibc-211
        ;;
    2.1.2 )
        LIBGLIBC=$start/linux86/lib-glibc-212
        ;;
    2.1.3 )
        LIBGLIBC=$start/linux86/lib-glibc-212
        ;;
    esac
else
    GLIBC="no"
fi

cat >> $sed << EOF1
s@GCCDIR=.*@GCCDIR=$GCCDIR@
s@GCCINC=.*@GCCINC=$GCCINC@
s@G77DIR=.*@G77DIR=$G77DIR@
EOF1

if test -n "$noexec" ; then
    echo "Directory: $start"
    echo "Linker: $LINKER"
    echo "GCCDIR: $GCCDIR"
    echo "GCCINC: $GCCINC"
    echo "G77DIR: $G77DIR"
    echo "glibc 2.1: $GLIBC"
    echo "glibc libs: $LIBGLIBC"
else
    if test -n "$LIBGLIBC" -a "$GLIBC" = "yes" ; then
#	( cd $LIBGLIBC; for i in * ; do \
#            rm -f $start/linux86/lib/$i; \
#            mv $i $start/linux86/lib; \
#	    done )
        ( cd $LIBGLIBC; \
	     tar cf - * | ( cd $start/linux86/lib; tar xpf - ))
        cat >> $sed << EOF2
s@LCRTB=.*@LCRTB=$GCCDIR/crtbegin.o@
s@LCRTE=.*@LCRTE=$GCCDIR/crtend.o@
s@PGIRPATH=.*@PGIRPATH="-rpath \$PGI/linux86/lib"@
EOF2
    else
        rm -rf $start/linux86/include/sys
    fi
    sed -f $sed < $rc > $new
    mv -f $rc $rc.FCS
    mv -f $new $rc
    chmod 0644 $rc
fi

rm -f $sed

exit 0

