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

# This script is used to install Linux single user distribution of Maple 7 
# We have to be on a Linux box in order for this installation to work
OS_name=`/bin/uname -s`

if [ $OS_name != "Linux" ];then
	echo
	echo "ERROR: You have to run this on computer running Linux Operating System"
	echo
	exit 1
fi

LOGFILE=.install.log

CALLP=`dirname $0`

INIWD=`pwd`

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

CD_ROOT="$CALLP/.."

# We need clean ls output 
unalias ls >/dev/null 2>&1
# Let us find proper capitalization 
UNIX=`ls -1 $CD_ROOT|fgrep -i unix` 
LINUX_SU=`ls -1 $CD_ROOT|fgrep -i linuxsingleuser`
COMMON=`ls -1 $CD_ROOT/$UNIX|fgrep -i common.tar`
LINUX_SU_BIN=`ls -1 $CD_ROOT/$LINUX_SU|fgrep -i bin_ibm_intel_linux_su.tar`

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

echo
echo "   Maple 7 Linux Single User 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_su
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
    echo
    echo " An existing installation was found in $MAPLE."
    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

###
# if this is a new install to an existing directory, warn user, 
#  and then remove everything
###


    NUMLINES=`ls -1 $MAPLE | wc -l `
    if [ $NUMLINES -gt 0 ]
    then
	echo "WARNING"
	echo " The installation directory $MAPLE contains 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
      /bin/rm -rf $MAPLE/$X
    done

###
# Get serial number
###
SN=
cd $MAPLE
tar xf $CD_ROOT/$LINUX_SU/$LINUX_SU_BIN bin.IBM_INTEL_LINUX/dectool >/dev/null 2>&1
if [ -f ./bin.IBM_INTEL_LINUX/dectool ]
then
	while [  x"$SN" = x ]
	do
		echo
		echo "Please enter your Serial Number:"
		read SN
		./bin.IBM_INTEL_LINUX/dectool $SN > ./bin.IBM_INTEL_LINUX/license.tmp
		if [ $? != 1 ]
		then
			echo " ERROR: Serial Number you specified is not valid:" $SN
			SN=
		fi
	done
else
	echo 
	echo "Please check installation media, permitions and free space on your disk,"
        echo "and try again"
	echo
        echo " Aborting install..."
        echo
        exit 1
fi
###
#install the Maple 7 Linux_SU
###

cd $MAPLE
echo
echo " Maple will now install the base distribution."
sleep 1  #in case we scroll


if [ -f $CD_ROOT/$UNIX/$COMMON ]
then
	TAR_OPTS=xvf
        tar $TAR_OPTS $CD_ROOT/$UNIX/$COMMON 2>> $LOGFILE >> $LOGFILE
else
	echo
	echo " Could not find common.tar" $CD_ROOT $UNIX $COMMON
	echo " The installation media seems to be corrupted."
	echo
	echo " Aborting install..."
        echo
        exit 1
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

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

if [ -d $MAPLE/license ]
then
	mv -f $MAPLE/bin.IBM_INTEL_LINUX/license.tmp $MAPLE/license/license.dat
	rm -rf $MAPLE/bin.IBM_INTEL_LINUX
else
        echo
        echo "Please check free space on your disk"
        echo "and try again"
        echo
        echo " Aborting install..."
        echo
        exit 1
fi 

###
# Install the appropriate executables
###

cd $MAPLE

echo
echo " Installing binaries."
if [ -f $CD_ROOT/$LINUX_SU/$LINUX_SU_BIN ]
    then
       tar $TAR_OPTS $CD_ROOT/$LINUX_SU/$LINUX_SU_BIN 2>> $LOGFILE >> $LOGFILE
       sleep 1
    else
        echo
        echo " Could not find bin_ibm_intel_linux_su.tar"
        echo " The installation media seems to be corrupted."
        echo
        echo " Aborting install..."
        echo
        exit 1

fi


###
# 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
