#!/bin/sh
# Copyright (c) 1996-2001 by Waterloo Maple Inc.
# All rights reserved. Unauthorized duplication prohibited.

# This script is used to install the distribution of Maple 7 

LOGFILE=.install.log

CALLP=`dirname $0`

INIWD=`pwd`

if [ "`echo $CALLP | sed 's;\(.\).*;\1;'`" != "/" ]
then
	CALLP=$INIWD/$CALLP
fi

echo
echo "   Maple 7 installation"
echo
echo


###
# ask for the install directory
###

echo
echo " Enter the directory that you would like to install"
echo " Maple 7 in or upgrade an existing"
echo " installation."

DEFMAPLE=/usr/local/maple
MAPLE=

while [ x"$MAPLE" = x ]
do
	echo " Installation directory (absolute path): [$DEFMAPLE]"
	read MAPLE
	if [ x"$MAPLE" = x ]
	then
		MAPLE=$DEFMAPLE
	fi
	if [ "`echo $MAPLE | sed 's;\(.\).*;\1;'`" != "/" ]
	then
		echo
		echo " You need to specify an absolute path (starting with /)"
		MAPLE=
	elif [ -d $MAPLE ]
	then
		if [ -f $MAPLE -o ! -r $MAPLE -o ! -w $MAPLE -o ! -x $MAPLE ]
		then
			echo
			echo " Sorry, you do not have access permissions to $MAPLE"
			MAPLE=
		fi
	else
		echo
		echo " The directory $MAPLE does not exist. Do you want it created? (y/n)"
		read Q
		if [ "$Q" = "y" -o "$Q" = "Y" -o "$Q" = "yes" ]
		then
			mkdir $MAPLE
			chmod 755 $MAPLE
			if [ ! -d $MAPLE ]
			then
				echo
				echo " Failed to create directory"
				MAPLE=
			fi
		else
			MAPLE=
		fi
	fi
done

LOGFILE=$MAPLE/$LOGFILE
touch $LOGFILE 2> /dev/null > /dev/null

echo "" >> $LOGFILE
echo " Starting INSTALL" >> $LOGFILE
date >> $LOGFILE
echo "" >> $LOGFILE


###
# determine whether this is a new install
###

if [ ! -d $MAPLE/bin ]
then
    NEWI=y
else
    if [ -d $MAPLE/FLEXlm ]
    then
        echo
        echo " Are you adding a platform to an existing Maple 7"
        echo " installation? (y/n)"
        read Q 
        if [ $Q = "y" -o $Q = "Y" -o $Q = "yes" ]
        then
            NEWI=n
        else
            echo
            echo " Do you wish to reinstall Maple 7? (y/n)"
            read Q
            if [ $Q = "y" -o $Q = "Y" -o $Q = "yes" ]
            then
                NEWI=y
            else
                echo
                echo "Aborting install..."
                echo
                exit 1
            fi
        fi
    else
	echo " An existing installation was found in $MAPLE."
	echo " Do you want to overwrite it? (y/n)"
	read Q
	if [ $Q = "y" -o $Q = "Y" -o $Q = "yes" ]
	then
		NEWI=y
	else
		echo
		echo "Aborting install..."
		echo
		exit 1
	fi
    fi
fi


###
# if this is a new install to an existing directory, look for and preserve
# any expected Maple 6 license keys, and then remove everything
###

if [ $NEWI = y -a -d $MAPLE/bin ]
then
    if [ -f $MAPLE/license/license.dat ]
    then
        LF1=y
        echo
        echo "license.dat found, will preserve."
    else   
        LF1=n
    fi

    #look for network.lic
    if [ -f $MAPLE/FlexLM/network.lic ]  #look first in 6.0 dir
    then
        LF2=y1
        echo
        echo "network.lic found in $MAPLE/FlexLM, will preserve."
    elif [ -f $MAPLE/FLEXlm/network.lic ]  #look in 6.01 dir
    then
        LF2=y2
        echo
        echo "network.lic found in $MAPLE/FLEXlm, will preserve."
    else
        LF2=n
        echo
        echo " Could not find network.lic in $MAPLE/FLEXlm"
	echo " or $MAPLE/FlexLM"
        echo
        echo " If you have an existing network license file somewhere"
        echo " below $MAPLE, you must copy it to safe"
	echo " location, so that it will not be erased by this"
	echo " installation of Maple 7."
        echo
	echo " Proceed with installation? (y/n)"
        read Q
        if [ "$Q" = "y" -o "$Q" = "Y" ]
        then
	    echo
	else
            echo
            echo " Aborting install..."
            echo
            exit 1
        fi
    fi

    #if there was anything to preserve
    if [ "$LF1" = "y" -o "$LF2" = "y1" -o "$LF2" = "y2" ]
    then

        #create a temporary directory in the install directory
        TMPN=tmp.$$
        mkdir $MAPLE/$TMPN
        if [ ! -d $MAPLE/$TMPN ]
        then
            echo
            echo " Could not write to $MAPLE. You may be out of disk space?"
            echo " Aborting install..."
            echo
            exit 1
        fi

        if [ $LF1 = y ]
        then
            cp $MAPLE/license/license.dat $MAPLE/$TMPN/license.dat
            if [ ! -f $MAPLE/$TMPN/license.dat ]
            then
                echo
                echo " Could not preserve license.dat"
		echo " You may be out of disk space?"
                echo " Aborting install..."
                echo
                exit 1
            fi
        fi
        if [ $LF2 = y1 ]
        then
            cp $MAPLE/FlexLM/network.lic $MAPLE/$TMPN/network.lic
            if [ ! -f $MAPLE/$TMPN/network.lic ]
            then
                echo
                echo " Could not preserve network.lic"
		echo " You may be out of disk space?"
                echo " Aborting install..."
                echo
                exit 1
            fi
        elif [ $LF2 = y2 ]
        then
            cp $MAPLE/FLEXlm/network.lic $MAPLE/$TMPN/network.lic

            if [ ! -f $MAPLE/$TMPN/network.lic ]
            then
                echo
                echo " Could not preserve network.lic"
		echo " You may be out of disk space?"
                echo " Aborting install..."
                echo
                exit 1
            fi
        fi

    fi

    NUMLINES=`ls -l $MAPLE | wc -l | sed 's;\ *;;'`
    if [ ! "$NUMLINES" = "2" ]
    then
	echo "WARNING"
	echo " The installation directory $MAPLE contains existing files"
	echo " which will be erased."
	echo
	echo " Proceed? (y/n)"
	read Q
	if [ "$Q" = "y" -o "$Q" = "Y" -o "$Q" = "yes" ]
	then
		echo " Erasing existing files in $MAPLE..."
	else
		echo
		echo " Aborting install..."
		echo
		exit 1
	fi
    fi

    for X in `ls $MAPLE`
    do
        if [ ! "$X" = "$TMPN" ]
        then
            /bin/rm -rf $MAPLE/$X
        fi
    done

fi


###
# if this is a new install, install the maple 7 base and copy FLEXlm
###

if [ $NEWI = y ]
then
    cd $MAPLE
    echo
    echo " Maple will now install the base distribution."
    sleep 1  #in case we scroll

    # HP-UX tar doesn't support p option.
    if [ `uname` = HP-UX ] ; then POPT= ; else POPT=p ; fi
    
    # Determine if tar supports the -o option.
    TAR_OPTS=xvo${POPT}f
    
    if [ -f $CALLP/common.tar ]
    then
        tar $TAR_OPTS $CALLP/common.tar bin/maple 2>/dev/null >/dev/null
    elif [ -f $CALLP/COMMON.TAR ]
    then
	tar  $TAR_OPTS $CALLP/COMMON.TAR bin/maple 2>/dev/null >/dev/null
    else
	echo
	echo " Could not find common.tar"
	echo " The installation media seems to be corrupted."
	echo
	echo " Aborting install..."
        echo
        exit 1
    fi
    
    if [ ! -f ./bin/maple ]
    then
        TAR_OPTS=xv${POPT}f
    fi

    HTO=y
    if [ -f $CALLP/common.tar ]
    then
    	tar $TAR_OPTS $CALLP/common.tar 2>> $LOGFILE >> $LOGFILE
    elif [ -f $CALLP/COMMON.TAR ]
    then
	tar $TAR_OPTS $CALLP/COMMON.TAR 2>> $LOGFILE >> $LOGFILE
    fi

    # Change the maple script to reflect the install location.
    for F in maple mint updtsrc
    do
        cat $MAPLE/bin/$F | sed -e 's!MAPLE=/usr/local/maple!MAPLE='$MAPLE'!' > $MAPLE/bin/$F.new
        mv -f $MAPLE/bin/$F.new $MAPLE/bin/$F
    done
    chmod 755 $MAPLE/bin/maple $MAPLE/bin/mint $MAPLE/bin/updtsrc 2>> $LOGFILE >> $LOGFILE
    chmod 444 $MAPLE/lib/maple.* 2>> $LOGFILE >> $LOGFILE


    echo
    echo " Installing license manager tools for all platforms"
    if [ -f $CALLP/../FLEXlm.tar ]
    then
	tar $TAR_OPTS $CALLP/../FLEXlm.tar 2>> $LOGFILE >> $LOGFILE
    elif [ -f $CALLP/../flexlm.tar ]
    then
	tar $TAR_OPTS $CALLP/../flexlm.tar 2>> $LOGFILE >> $LOGFILE
    elif [ -f $CALLP/../FLEXLM.TAR ]
    then
	tar $TAR_OPTS $CALLP/../FLEXLM.TAR 2>> $LOGFILE >> $LOGFILE
    else
    	mkdir $MAPLE/FLEXlm
    	cp -r $CALLP/../FLEXlm/* $MAPLE/FLEXlm
    fi

   chmod -R ug+rwX $MAPLE/FLEXlm 2>> $LOGFILE >> $LOGFILE
fi


###
# if this is a new install to an existing directory, retrieve any preserved
# Maple 6 license keys
###

if [ $NEWI = y ]
then
    if [ "$LF1" = "y" -o "$LF2" = "y1" -o "$LF2" = "y2" ]
    then
        if [ $LF1 = y ]
        then
            echo
            echo "Restoring license.dat"
            /bin/cp $MAPLE/$TMPN/license.dat $MAPLE/license/license.dat
        fi
        if [ $LF2 = y1 -o $LF2 = y2 ]
        then
            echo
            echo "Restoring network.lic to $MAPLE/FLEXlm"
            /bin/cp $MAPLE/$TMPN/network.lic $MAPLE/FLEXlm/network.lic
        fi

        rm -rf $MAPLE/$TMPN 

    fi
fi

###
# ask user which platforms are to be installed
###

echo
echo " The install will now ask which binaries you need"
echo " installed for your particular site."
echo
echo " Please answer each question with a y for yes or"
echo " an n for no."

# DEC Alpha
if [ -f $CALLP/bin_DEC_ALPHA_UNIX.tar -o -f $CALLP/bin_dec_alpha_unix.tar -o -f $CALLP/BIN_DEC_ALPHA_UNIX.TAR ]
then
	echo
	echo " Do you wish to install the binaries required for a DEC Alpha"
	echo " machine running Digital UNIX V4.0* or Compaq Tru64 UNIX V5.* (y/n)"
	read DEC_ALPHA
else
	DEC_ALPHA=n
fi

# HP-UX 
if [ -f $CALLP/bin_HP_RISC_UNIX.tar -o -f $CALLP/bin_hp_risc_unix.tar -o -f $CALLP/BIN_HP_RISC_UNIX.TAR ]
then
	echo
	echo " Do you wish to install the binaries required for an HP RISC"
	echo " machine running HP-UX 11 (y/n)"
	read HPUX
else
	HPUX=n
fi

# Linux/x86 
if [ -f $CALLP/bin_IBM_INTEL_LINUX.tar -o -f $CALLP/bin_ibm_intel_linux.tar -o -f $CALLP/BIN_IBM_INTEL_LINUX.TAR ]
then
	echo
	echo " Do you wish to install the binaries required for an Intel/x86"
	echo " machine running Linux 2.2 RedHat or SUSE (y/n)"
	read LINUX
else
	LINUX=n
fi


# IBM RS/6000
if [ -f $CALLP/bin_IBM_RISC_UNIX.tar -o -f $CALLP/bin_ibm_risc_unix.tar -o -f $CALLP/BIN_IBM_RISC_UNIX.TAR ]
then
	echo
	echo " Do you wish to install the binaries required for an IBM RS/6000"
	echo " machine running AIX 4.3? (y/n)"
	read RS6000
else
	RS6000=n
fi

# SGI Irix
if [ -f $CALLP/bin_SGI_MIPS_UNIX.tar -o -f $CALLP/bin_sgi_mips_unix.tar -o -f $CALLP/BIN_SGI_MIPS_UNIX.TAR ]
then
	echo
	echo " Do you wish to install the binaries required for an SGI"
	echo " machine running IRIX 6.5? (y/n)"
	read SGI
else
	SGI=n
fi

# SunOS 5.x on SPARC
if [ -f $CALLP/bin_SUN_SPARC_SOLARIS.tar -o -f $CALLP/bin_sun_sparc_solaris.tar -o -f $CALLP/BIN_SUN_SPARC_SOLARIS.TAR ]
then
	echo
	echo " Do you wish to install the binaries required for a SUN SPARC"
	echo " machine running Solaris 2.6, 2.7 or 2.8? (y/n)"
	read SUNOS
else
	SUNOS=n
fi


###
# Install the appropriate executables
###

cd $MAPLE

if [ "$HTO" != "y" ]
then
    if [ `uname` = HP-UX ] ; then POPT= ; else POPT=p ; fi
    TAR_OPTS=xvo${POPT}f
    HTO=n
fi

if [ $DEC_ALPHA = "y" -o $DEC_ALPHA = "Y" -o $DEC_ALPHA = "yes" ]
then
    echo
    echo " Installing DEC Alpha binaries."
    ARCHIVE=
    if [ -f $CALLP/bin_DEC_ALPHA_UNIX.tar ]
    then
	ARCHIVE=bin_DEC_ALPHA_UNIX.tar
    elif [ -f $CALLP/bin_dec_alpha_unix.tar ]
    then
	ARCHIVE=bin_dec_alpha_unix.tar
    elif [ -f $CALLP/BIN_DEC_ALPHA_UNIX.TAR ]
    then
	ARCHIVE=BIN_DEC_ALPHA_UNIX.TAR
    fi

    if [ x"$ARCHIVE" != x ]
    then
        if [ $HTO = n ]
        then
            tar $TAR_OPTS $CALLP/$ARCHIVE bin.DEC_ALPHA_UNIX/cmaple 2>/dev/null
            if [ ! -f ./bin.DEC_ALPHA_UNIX/cmaple ]
            then
                TAR_OPTS=xv${POPT}f
            fi
            HTO=y
        fi
        tar $TAR_OPTS $CALLP/$ARCHIVE 2>> $LOGFILE >> $LOGFILE
    fi
    sleep 1
fi

if [ $HPUX = "y" -o $HPUX = "Y" -o $HPUX = "yes" ]
then
    echo
    echo " Installing HP-UX binaries."
    ARCHIVE=
    if [ -f $CALLP/bin_HP_RISC_UNIX.tar ]
    then
	ARCHIVE=bin_HP_RISC_UNIX.tar
    elif [ -f $CALLP/bin_hp_risc_unix.tar ]
    then
	ARCHIVE=bin_hp_risc_unix.tar
    elif [ -f $CALLP/BIN_HP_RISC_UNIX.TAR ]
    then
	ARCHIVE=BIN_HP_RISC_UNIX.TAR
    fi

    if [ x"$ARCHIVE" != x ]
    then
        if [ $HTO = n ]
        then
            tar $TAR_OPTS $CALLP/$ARCHIVE bin.HP_RISC_UNIX/cmaple 2>/dev/null
            if [ ! -f ./bin.HP_RISC_UNIX/cmaple ]
            then
                TAR_OPTS=xv${POPT}f
            fi
            HTO=y
        fi
	tar $TAR_OPTS $CALLP/$ARCHIVE 2>> $LOGFILE >> $LOGFILE
    fi
    sleep 1
fi

if [ $LINUX = "y" -o $LINUX = "Y" -o $LINUX = "yes" ]
then
    echo
    echo " Installing Linux/x86  binaries."
    ARCHIVE=
    if [ -f $CALLP/bin_IBM_INTEL_LINUX.tar ]
    then
	ARCHIVE=bin_IBM_INTEL_LINUX.tar
    elif [ -f $CALLP/bin_ibm_intel_linux.tar ]
    then
	ARCHIVE=bin_ibm_intel_linux.tar
    elif [ -f $CALLP/BIN_IBM_INTEL_LINUX.TAR ]
    then
        ARCHIVE=BIN_IBM_INTEL_LINUX.TAR
    fi

    if [ x"$ARCHIVE" != x ]
    then
        if [ $HTO = n ]
        then
            tar $TAR_OPTS $CALLP/$ARCHIVE bin.IBM_INTEL_LINUX/cmaple 2>/dev/null
            if [ ! -f ./bin.IBM_INTEL_LINUX/cmaple ]
            then
                TAR_OPTS=xv${POPT}f
            fi
            HTO=y
        fi
	tar $TAR_OPTS $CALLP/$ARCHIVE 2>> $LOGFILE >> $LOGFILE
    fi
    sleep 1
fi

if [ $RS6000 = "y" -o $RS6000 = "Y" -o $RS6000 = "yes" ]
then
    echo
    echo " Installing RS/6000 binaries."
    if [ -f $CALLP/bin_IBM_RISC_UNIX.tar ]
    then
	ARCHIVE=bin_IBM_RISC_UNIX.tar
    elif [ -f $CALLP/bin_ibm_risc_unix.tar ]
    then
        ARCHIVE=bin_ibm_risc_unix.tar
    elif [ -f $CALLP/BIN_IBM_RISC_UNIX.TAR ]
    then
        ARCHIVE=BIN_IBM_RISC_UNIX.TAR
    fi

    if [ x"$ARCHIVE" != x ]
    then
        if [ $HTO = n ]
        then
            tar $TAR_OPTS $CALLP/$ARCHIVE bin.IBM_RISC_UNIX/cmaple 2>/dev/null
            if [ ! -f ./bin.IBM_RISC_UNIX/cmaple ]
            then
                TAR_OPTS=xv${POPT}f
            fi
            HTO=y
        fi
	tar $TAR_OPTS $CALLP/$ARCHIVE 2>> $LOGFILE >> $LOGFILE
    fi
    sleep 1
fi

if [ $SGI = "y" -o $SGI = "Y" -o $SGI = "yes" ]
then
    echo
    echo " Installing SGI binaries."
    if [ -f $CALLP/bin_SGI_MIPS_UNIX.tar ]
    then
	ARCHIVE=bin_SGI_MIPS_UNIX.tar
    elif [ -f $CALLP/bin_sgi_mips_unix.tar ]
    then
        ARCHIVE=bin_sgi_mips_unix.tar
    elif [ -f $CALLP/BIN_SGI_MIPS_UNIX.TAR ]
    then
        ARCHIVE=BIN_SGI_MIPS_UNIX.TAR
    fi

    if [ x"$ARCHIVE" != x ]
    then
        if [ $HTO = n ]
        then
            tar $TAR_OPTS $CALLP/$ARCHIVE bin.SGI_MIPS_UNIX/cmaple 2>/dev/null
            if [ ! -f ./bin.SGI_MIPS_UNIX/cmaple ]
            then
                TAR_OPTS=xv${POPT}f
            fi
            HTO=y
        fi
	tar $TAR_OPTS $CALLP/$ARCHIVE 2>> $LOGFILE >> $LOGFILE
    fi
    sleep 1
fi

if [ $SUNOS = "y" -o $SUNOS = "Y" -o $SUNOS = "yes" ]
then
    echo
    echo " Installing SPARC Solaris binaries."
    if [ -f $CALLP/bin_SUN_SPARC_SOLARIS.tar ]
    then
        ARCHIVE=bin_SUN_SPARC_SOLARIS.tar
    elif [ -f $CALLP/bin_sun_sparc_solaris.tar ]
    then
        ARCHIVE=bin_sun_sparc_solaris.tar
    elif [ -f $CALLP/BIN_SUN_SPARC_SOLARIS.TAR ]
    then
        ARCHIVE=BIN_SUN_SPARC_SOLARIS.TAR
    fi

    if [ x"$ARCHIVE" != x ]
    then
        if [ $HTO = n ]
        then
            tar $TAR_OPTS $CALLP/$ARCHIVE bin.SUN_SPARC_SOLARIS/cmaple 2>/dev/null
            if [ ! -f ./bin.SUN_SPARC_SOLARIS/cmaple ]
            then
                TAR_OPTS=xv${POPT}f
            fi
            HTO=y
        fi
	tar $TAR_OPTS $CALLP/$ARCHIVE 2>> $LOGFILE >> $LOGFILE
    fi
    sleep 1
fi

cd $INIWD


###
# finish install
###

echo
echo " Installation is complete."
echo " You may need to edit the scripts in the bin directory"
echo " to configure Maple to run on your machine(s)."
echo
