#! /bin/sh

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

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

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

list_products() {
    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    C/C++"
    echo "  2    F77"
    echo "  3    HPF/F90/F77"
    echo "  4    F77/C/C++"
    echo "  5    HPF/F90/F77/C/C++"
    echo
}

get_product() {
    echo "Enter a number between 1 and 5:"
    read ans
}

change_install_dir() {
    echo
    echo "Default installation directory is $INSTALL_DIR."
    echo "Do you want to change the install location? [y,n]"
    read ans
}

get_install_dir() {
    echo "Specify a directory to install the product:"
    read ans
}

chmod_install_dir() {
    echo
    echo "Do you want the files in the install directory to be read-only? [y,n]"
    read ans
}

# tarfile source
SRC=`dirname $0`

# display legal license
more $SRC/LICENSE
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

if test "$target" != "interix86" ; then
    list_products
    get_product
else
    # There is only one Interix package and it contains C/F77.
    ans=4
fi

# Get partfile
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.
INSTALL_DIR=/usr/pgi
change_install_dir
if test "$ans" = "y" -o "$ans" = "yes" ; then
    get_install_dir
    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 $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
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
# libs.  See fixlinux86 for details.
if test $target = "linux86" ; then
    $INSTALL_DIR/linux86/bin/fixlinux86 $INSTALL_DIR
fi

# 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 "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 
        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
        FLEXlm hostid: $flexlm
        Hostname: $hname
        Installation: $INSTALL_DIR
        PGI Release: $RELNUM
EOF
        # 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."
        echo "You will need to send it to PGI or your authorized PGI distributor"
        echo "in order to receive your permanent license."
    fi
else
    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

chmod_install_dir
if test "$ans" = "y" -o "$ans" = "yes" ; then
    chmod -R a-w $INSTALL_DIR/*
fi

echo
echo "Installation complete."

exit 0

