#! /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.
## 

# XXX UPDATE this whenever updating the distribution
RELNUM="3.2-4"

PATH=/usr/bin:/bin:$PATH

quit() {
    echo "Exiting..."
    exit 1
}

# Tarfile source
SRC=`dirname $0`

# Display legal license
more $SRC/LICENSE
echo
echo "Do you accept these terms? [accept,decline]"
read ans
if test "$ans" != "accept" ; then
    quit
fi

# What type of target?
type uname > /dev/null 2>&1
if test $? -ne 0 ; then
    echo "install: uname not found in \$PATH environment variable"
    quit
fi
target=`uname -s | tr '[A-Z]' '[a-z]'`

# GNU uname < 1.16 doesn't support -p option
case $target in 
linux )
    target=linux86
    subdirs="$target/bin $target/include $target/lib $target/lib-glibc"
    if test ! -f $SRC/linux86/bin/fixlinux86 ; then
	echo "install: internal error (fixlinux86 not found)"
	quit
    fi
    ;;
sunos )
    processor=`uname -p | tr '[A-Z]' '[a-z]'`
    case "$processor" in
    *86 )
        target=solaris86
	;;
    * )
        echo
        echo "install: products can only be installed on Linux or Solaris86"
        echo "         operating systems."
        quit
	;;
    esac
    subdirs="$target/bin $target/include $target/lib"
    ;;
windows_nt )
    target=interix86
    subdirs="$target/bin $target/include $target/lib"
    ;;
esac

# Sanity check: make sure $SRC/$target exists.
if test ! -d $SRC/$target ; then
    echo
    echo "install: system is $target; but $SRC/$target does not exist."
    echo "         Incorrect tarfile downloaded?"
    quit
fi

echo
echo "This script installs PGI Workstation products for the"
echo "Linux and Solaris86 operating systems.  Select the product"
echo "below that you purchased, and the directory where the software"
echo "is to be installed."
echo
echo "  1    PGCC Workstation or PGCC Server"
echo "  2    PGF77 Workstation or PGF77 Server"
echo "  3    PGHPF Workstation or PGHPF Server"
echo "  4    PGF77/PGCC Workstation or PGF77/PGCC Server"
echo "  5    PGI Workstation or PGI Server"
echo
echo "  (PGI, PGHPF, PGCC, and PGF77 are all Reg. U.S. Patent"
echo "  and Trademark Office to The Portland Group, Inc.)"
echo
echo "Enter a number between 1 and 5:"
read ans

# Get list of files for the specified product.
case $ans in 
1 ) PART="$SRC/.parts/$target-CC" ;;
2 ) PART="$SRC/.parts/$target-F77" ;;
3 ) PART="$SRC/.parts/$target-HPF" ;;
4 ) PART="$SRC/.parts/$target-F77-CC" ;;
5 ) PART="$SRC/.parts/$target-HPF-CC"
    subdirs="$target"
    ;;
* ) echo "install: Unknown product '$ans'.  Please run the install script again"
    echo "and select a number between 1 and 5."
    quit
    ;;
esac

# If this happens the tarfiles were improperly built
if test ! -f $PART ; then
    echo "install: internal error (missing parts file $PART)"
    quit
fi
if test ! -f $SRC/.parts/license.dat ; then
    echo "install: internal error (missing license.dat file)"
    quit
fi

# Where to install?  Try to create dir if doesn't exist.  Warn about installs
# that overlay previously-installed directory.  Quit if cannot create or
# write to installation directory.
INSTALL_DIR=/usr/pgi
echo
echo "Please specify the directory path under which the software will be"
echo "installed.  The default directory is $INSTALL_DIR, but you may"
echo "install anywhere you wish, assuming you have permission to do so."
echo
echo "Installation directory? [$INSTALL_DIR] "
read ans
if test -n "$ans" ; then
    INSTALL_DIR=$ans
fi

echo
if test "$INSTALL_DIR/.parts/license.dat" = "`pwd`/.parts/license.dat"; then
    echo "install: cannot install products in same directory as unpacked ftp tarfile"
    quit
fi
if test -d $INSTALL_DIR ; then
    touch $INSTALL_DIR/write_test > /dev/null 2>&1
    if test $? -eq 1 ; then
        echo "install: no write permission in installation directory ($INSTALL_DIR)"
        quit
    else
        rm -f $INSTALL_DIR/write_test
    fi
else
    mkdir -p $INSTALL_DIR > /dev/null 2>&1
    if test $? -eq 0 ; then
        echo "Note: install created $INSTALL_DIR directory"
    else
        echo "install: mkdir $INSTALL_DIR: permission denied"
        echo "install: installation directory ($INSTALL_DIR) not created"
        quit
    fi
fi
if test -f "$INSTALL_DIR/license.dat" ; then
    echo "Note: $INSTALL_DIR seems to contain a previous installation."
    echo "      This installation may fail to install files properly."
    echo "      You may see messages from tar about file permissions, etc."
    echo
fi

# Install software.
echo "Installing software into $INSTALL_DIR (this may take some time)."
trap "" 2 3
cd $SRC
for i in `cat $PART` ; do
    tar cf - $i | (cd $INSTALL_DIR; tar xf - )
done
echo

# Linux distributions using glibc 2.x require changes to linux86rc and 
# certain other settings.  See fixlinux86 for details.
if test $target = "linux86" ; then
    $INSTALL_DIR/linux86/bin/fixlinux86 -x $INSTALL_DIR
fi

if test -f $INSTALL_DIR/.parts/release ; then 
    mv $INSTALL_DIR/.parts/release $INSTALL_DIR/.release
fi

# Collect FLEXlm information that will be used below.
PGI=$INSTALL_DIR
export PGI
unset LM_LICENSE_FILE # otherwise pglicinfo gets wrong license file
flexlm=`$INSTALL_DIR/$target/bin/lmutil hostid | awk -F\" '/The FLEXlm/ {print $2}'`
hname=`uname -n`
cat > $INSTALL_DIR/info << EOF_info
        FLEXlm hostid: $flexlm
        Hostname: $hname
        Installation: $INSTALL_DIR
        PGI Release: $RELNUM

EOF_info

# Save installation information to file, then print it.
echo
echo "If you don't already have permanent keys for this product/release, a"
echo "fifteen-day evaluation license can be created now."
echo
echo "Create an evaluation license?  [y/n]"
read ans
if test "$ans" != "n" ; then
    more $INSTALL_DIR/TRIAL_LICENSE
    echo
    echo "Do you accept these terms? [accept,decline]"
    read ans
    echo
    if test "$ans" = "accept" ; then
        mv $INSTALL_DIR/.parts/license.dat $INSTALL_DIR/license.dat
        echo "Creating temporary license."
        ( cd $INSTALL_DIR; ./$target/bin/mkinst )
        echo 
        # save installation info to file then print it
        ( cd $INSTALL_DIR; \
            $target/bin/pglicinfo 2>&1 | sed -e '/Hostid:/r info' > license.info )
        cat $INSTALL_DIR/license.info
        echo 
        echo "The above information was saved to $INSTALL_DIR/license.info."
    fi
else
    # Mimic the text from pglicinfo above, excluding PGI= hostid info.
    cat  > $INSTALL_DIR/license.info << EOF_perm
For a permanent license, please read the order acknowledgement
that you received.  Connect to https://www.pgroup.com/License with
the username and password in the order acknowledgement.

EOF_perm
    cat $INSTALL_DIR/info >> $INSTALL_DIR/license.info
    rm -f $INSTALL_DIR/TRIAL_LICENSE
fi

# Cleanup
rm -f $INSTALL_DIR/install
rm -f $INSTALL_DIR/README
rm -rf $INSTALL_DIR/.parts
rm -f $INSTALL_DIR/info

echo
echo "Do you want the files in the install directory to be read-only? [y,n]"
read ans
if test "$ans" = "y" -o "$ans" = "yes" ; then
    chmod -R a-w $INSTALL_DIR/*
fi

echo
echo "Installation complete."

exit 0

