#!/bin/sh
#
#  MathReader 4.2 Installer for Linux-PPC
#
#  This file and all scripts and messages accessed from this file
#  Copyright (c) 2002 Wolfram Research, Inc.  All rights reserved.
#
#

ProgramName=`type $0 | awk '{print $3}'`  # search for executable on path
LayoutDir=`dirname $ProgramName`          # extract directory of program
ProgramName=`basename $ProgramName`       # base name of program
cd "$LayoutDir"                           # go to that directory
LayoutDir=`pwd`

# default installation paths

DefaultTopDir="/usr/local/Wolfram"
DefaultScriptDir="/usr/local/bin"

Arch=Linux-PPC


LicenseAgreement="${LayoutDir}/Files/SystemFiles/Installation/TextResources/English/LicenseAgreement.txt"

# determine method of preventing a newline with echo

echo "test\c" | grep -s c > /dev/null 2>&1
if [ ! "$?" = "0" ]; then
  n=""
  c="\c"
else
  n="-n"
  c=""
fi


# CreateDirectory () -- ask if we should create $1, and attempt to do so if the
# response is yes.  Return 0 if the directory is successfully created;
# otherwise, return 1

CreateDirectory()
{
    echo $n "   Create directory \"$1\"? ([y]es/[n]o) [y]: $c"
    read answer
    echo

    answer=`echo "${answer}" | tr '[a-z]' '[A-Z]'`
    [ "$answer" ] || answer="Y";

    if [ "$answer" = "Y" -o "$answer" = "YES" ]; then

	mkdir -p "$1" > /dev/null 2>&1

	if [ $? -eq 0 ]; then
	    echo "   Directory \"$1\" successfully created."
	    echo
	    return 0
	else
	    echo "   Failed to create directory \"$1\"."
	fi
    else
	echo "   Not attempting to create directory \"$1\"."
    fi
    return 1
}

# ensure that the License is there

if [ ! -f "$LicenseAgreement" ]; then
    echo
    echo "Unable to read License Agreement.  Please contact Wolfram Research"
    echo "Technical Support: support@wolfram.com"
    echo
    exit 1
fi

# determine what tool we should use to display the License

command=""

if [ -n "$PAGER" ]; then           # use $PAGER, if it is defined and exists;
    type $PAGER > /dev/null 2>&1
    [ $? -eq 0 ] && command=$PAGER
fi

if [ -z "$command" ]; then         # or, use more if it exists;
    type more > /dev/null 2>&1
    [ $? -eq 0 ] && command="more"
fi

[ -z "$command" ] && command="cat" # or, use cat

# print the License

$command $LicenseAgreement

# prompt for acceptance

echo
echo "To accept the terms and conditions of this agreement enter \"[a]ccept\"."
echo "To decline the terms and conditions of this agreement enter \"[d]ecline\"."
echo

response=""
while [ -z "$response" ]
do
    echo $n "Please type \"[a]ccept\" or \"[d]ecline\" and press return: $c"
    read response
    response=`echo "${response}" | tr '[a-z]' '[A-Z]'`
    if [ "$response" = "A" -o "$response" = "ACCEPT" ]; then
	response="accept"
    elif [ "$response" = "D" -o "$response" = "DECLINE" ]; then
	response="decline"
    else
	response=""
    fi
done

# decline, if that's what we want

if [ "$response" = "decline" ]; then
    echo
    echo "License not accepted ... aborting installation."
    echo
    exit 0
fi

# License is accepted, now get on with the install...

# print the welcome banner

echo
echo "-----------------------------------------------------------------------------"
echo "                MathReader 4.2 Installer for Linux-PPC"
echo "-----------------------------------------------------------------------------"

# prompt for $TopDir

echo
echo "The installer will create a directory named \"MathReader\" in the directory you"
echo "specify and copy relevant files into that directory. The installation"
echo "will require approximately 30 MB of disk space."

# read and validate the response

r=0;
while [ $r -ne 1 ]
do
    echo
    echo    "   Enter MathReader installation directory"
    echo $n "   [$DefaultTopDir]: $c"
    read response
    echo

    [ "$response" ] || response="$DefaultTopDir"; # assign default, if necessary

    # if the response ends with a forward slash, remove the slash

    lastchar=`echo $response | awk '{ if (length($0) > 1) \
	print substr ($0, length($0), 1); else print "A"}'`
    [ $lastchar = "/" ] && response=`echo $response | awk \
	'{print substr ($0, 1, length($0)-1)}'`

    # is there a file with that name?

    if [ -f "$response" ]; then
	echo "   A file named \"$response\" already exists."
    
    # the directory doesn't exist

    elif [ ! -d "$response" ]; then
	echo "   Directory \"$response\" does not exist."

	if (CreateDirectory "$response"); then
	    TopDir=$response
	    r=1
	fi

    # the directory *does* exist

    else
	if [ -w "$response" ]; then # do we have write permission?
	    TopDir=$response
	    r=1
	else
	    echo "   You do not have write permission for \"$response\"."
	fi
    fi

    # is there already a MathReader directory in $TopDir?

    if [ -d "$response/MathReader" -a $r -eq 1 ]; then
	echo "   There is already a directory \"$response/MathReader\"."
	echo "   Please either move the existing directory, or specify a"
	echo "   different MathReader installation directory."
	r=0
    
    # is there a file by this name?

    elif [ -f "$response/MathReader" -a $r -eq 1 ]; then
	echo "   The directory \"$TopDir\" contains a file named \"MathReader\"."
	echo "   Please either move the existing file, or specify a different"
	echo "   MathReader installation directory."
	r=0

    elif [ -f "$response/mathreader" -a $r -eq 1 ]; then
	echo "   The directory \"$TopDir\" contains a file named \"mathreader\"."
	echo "   Please either move the existing file, or specify a different"
	echo "   MathReader installation directory."
	r=0
    fi

done

# prompt for $ScriptDir

echo "An executable script for MathReader is automatically created during"
echo "installation.  The executable script should be placed in a directory that is"
echo "listed in your Unix Path, or you can append the directory you specify below"
echo "to your Unix Path.  Please make sure that you have write permission for the"
echo "directory you specify."

# read and validate response

r=0
while [ $r -ne 1 ]
do
    echo
    echo    "   Enter directory for MathReader executable script"
    echo $n "   [$DefaultScriptDir]: $c"
    read response
    echo

    [ "$response" ] || response="$DefaultScriptDir" # assign default, if necessary

    # if the response ends with a forward slash, remove the slash

    lastchar=`echo $response | awk '{ if (length($0) > 1) \
	print substr ($0, length($0), 1); else print "A"}'`
    [ $lastchar = "/" ] && response=`echo $response | awk \
	'{print substr ($0, 1, length($0)-1)}'`

    # is there a file with that name?

    if [ -f "$response" ]; then
	echo "   \"$response\" is a file; you must specify a directory"
	echo "   into which the MathReader executable script will be placed."
    	
    # is the response the same as the TopDir?

    elif [ "$response" = "$TopDir/MathReader" ]; then
	ScriptDir=$response
	r=1

    # if the directory doesn't exist

    elif [ ! -d "$response" ]; then
	echo "   Directory \"$response\" does not exist."

	# should we try to create the directory?

	if (CreateDirectory "$response"); then
	    ScriptDir=$response
	    r=1
	fi

    # the directory *does* exist

    else
	if [ -w "$response" ]; then # do we have write permission?
	    ScriptDir=$response
	    r=1
	else
	    echo "   You do not have write permission for \"$response\"."
	fi
    fi

    # is there a MathReader file or directory in $ScriptDir?

    if [ $r -eq 1 ]; then
	if [ -d "$response/MathReader" ]; then
	    echo "   There is already a directory \"MathReader\" in the directory"
	    echo "   \"$response\".  You cannot have both a file and a directory of"
	    echo "   the same name in the same directory.  Please either move the"
	    echo "   existing directory, or specify a different directory in which"
	    echo "   to place the MathReader executable script."
	    r=0
    
    # is there a file by this name?

	elif [ -f "$response/MathReader" ]; then
	    echo "   The directory \"$response\" already contains a file named"
	    echo "   \"MathReader\".  Please either move the existing file, or"
	    echo "   specify a different directory in which to place the MathReader"
	    echo "   executable script."
	    r=0

        elif [ -f "$response/mathreader" -a $r -eq 1 ]; then
	    echo "   The directory \"$TopDir\" contains a file named"
            echo "   \"mathreader\".  Please either move the existing file, or"
            echo "   specify a different directory in which to place the MathReader"
	    echo "   executable script."
	    r=0

    # will the MathReader installation directory conflict with $ScriptDir?

	elif [ "$TopDir" = "$ScriptDir" ]; then
	    echo "   You cannot place both the MathReader installation directory"
	    echo "   and the MathReader executable script in the same location."
	    echo "   Please specify a different directory in which to place the"
	    echo "   MathReader executable script."
	    r=0
	fi
    fi
done

# confirm user responses

echo "-----------------------------------------------------------------------------"
echo "   MathReader installation directory: $TopDir/MathReader/"
echo "   MathReader executable script directory: $ScriptDir/"
echo "-----------------------------------------------------------------------------"

r=0
while [ $r -ne 1 ]
do
   echo
   echo $n "   Enter \"[c]ontinue\" to continue; \"[e]xit\" to exit [c]: $c"
   read response
   response=`echo "${response}" | tr '[a-z]' '[A-Z]'`
   if [ "$response" = "C" -o "$response" = "CONTINUE" ]; then
	response="c"
    elif [ "$response" = "E" -o "$response" = "EXIT" ]; then
	response="e"
    else
	response="c"
    fi

   [ "$response" = "e" ] && exit
   [ "$response" = "c" ] && r=1

done
 
# begin the actual installation
echo
echo $n "   [installing...$c"

# make the MathReader directory

TopDir="$TopDir/MathReader"
mkdir "$TopDir/" > /dev/null 2>&1

if [ $? -eq 0 ]; then
   echo $n ".$c"
else
   echo " ERROR]"
   echo
   echo "The installer was unable to create the directory"
   echo "$TopDir"
   echo "Please make sure that you have appropriate file permissions, and that there is"
   echo "adequate space on the destination drive."
   echo
   exit
fi

# make the SystemFiles directory

mkdir "$TopDir/SystemFiles" > /dev/null 2>&1

if [ $? -eq 0 ]; then
   echo $n ".$c"
else
   echo " ERROR]"
   echo
   echo "The installer was unable to create the directory"
   echo "$TopDir/SystemFiles"
   echo "Please make sure that you have appropriate file permissions, and that there is"
   echo "adequate space on the destination drive."
   echo
   exit
fi

# copy the SystemFiles/CharacterEncodings directory

cp -r "$LayoutDir/Files/SystemFiles/CharacterEncodings" "$TopDir/SystemFiles" \
   > /dev/null 2>&1
if [ $? -eq 0 ]; then
   echo $n ".$c"
else
   echo " ERROR]"
   echo
   echo "The installer was unable to copy SystemFiles/CharacterEncodings"
   echo "to $TopDir"
   echo "Please make sure that you have appropriate file permissions, and that there is"
   echo "adequate space on the destination drive."
   echo
   exit
fi

# copy the SystemFiles/Fonts directory

cp -r "$LayoutDir/Files/SystemFiles/Fonts" "$TopDir/SystemFiles" > \
   /dev/null 2>&1
if [ $? -eq 0 ]; then
   echo $n ".$c"
else
   echo " ERROR]"
   echo
   echo "The installer was unable to copy SystemFiles/Fonts"
   echo "to $TopDir"
   echo "Please make sure that you have appropriate file permissions, and that there is"
   echo "adequate space on the destination drive."
   echo
   exit
fi

# copy the SystemFiles/FrontEnd directory

cp -r "$LayoutDir/Files/SystemFiles/FrontEnd" "$TopDir/SystemFiles" > \
   /dev/null 2>&1
if [ $? -eq 0 ]; then
   echo $n ".$c"
else
   echo " ERROR]"
   echo
   echo "The installer was unable to copy SystemFiles/FrontEnd"
   echo "to $TopDir"
   echo "Please make sure that you have appropriate file permissions, and that there is"
   echo "adequate space on the destination drive."
   echo
   exit
fi

# copy the SystemFiles/SpellingDictionaries directory

cp -r "$LayoutDir/Files/SystemFiles/SpellingDictionaries" \
   "$TopDir/SystemFiles" > /dev/null 2>&1
if [ $? -eq 0 ]; then
   echo $n ".$c"
else
   echo " ERROR]"
   echo
   echo "The installer was unable to copy SystemFiles/SpellingDictionaries"
   echo "to \"$TopDir\""
   echo "Please make sure that you have appropriate file permissions, and that there is"
   echo "adequate space on the destination drive."
   echo
   exit
fi

# copy the Documentation directory

cp -r "$LayoutDir/Files/Documentation" "$TopDir" > /dev/null 2>&1
if [ $? -eq 0 ]; then
   echo $n ".$c"
else
   echo " ERROR]"
   echo
   echo "The installer was unable to copy Documentation"
   echo "to $TopDir"
   echo "Please make sure that you have appropriate file permissions, and that there is"
   echo "adequate space on the destination drive."
   echo
   exit
fi

# copy the Configuration directory

cp -r "$LayoutDir/Files/Configuration" "$TopDir" > /dev/null 2>&1
if [ $? -eq 0 ]; then
   echo $n ".$c"
else
   echo " ERROR]"
   echo
   echo "The installer was unable to copy Configuration"
   echo "to $TopDir"
   echo "Please make sure that you have appropriate file permissions, and that there is"
   echo "adequate space on the destination drive."
   echo
   exit
fi


# make the Executables directory

mkdir "$TopDir/Executables/" > /dev/null 2>&1
if [ $? -eq 0 ]; then
  echo $n ".$c"
else
  echo " ERROR]"
  echo
  echo "The installer was unable to create the directory"
  echo "$TopDir/Executables/ "
  echo "Please make sure that you have appropriate file permissions, and that there is"
  echo "adequate space on the destination drive."
  echo
  exit
fi

# determine which fonts will be needed

if [ $Arch = "DEC-AXP" ]; then
   fonttype="PCF"
else
   fonttype="BDF"
fi

# generate the MathReader script

cat - > $TopDir/Executables/MathReader << .
#!/bin/sh
#
#  MathReader 4.2 command file for $Arch
#
#  Copyright 1988-2002 Wolfram Research, Inc.
#

topdir=$TopDir

XUSERFILESEARCHPATH=\$topdir/SystemFiles/FrontEnd/SystemResources/X/%N
export XUSERFILESEARCHPATH

LD_LIBRARY_PATH=\$topdir/SystemFiles/Libraries/\$sysid:\$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

userDisplay=\`echo "\$@" | sed 's/.*-display \\{1,\\}.*:[^ ]\\{1,\\}.*/setting user display/'\` 
if [ "\$userDisplay" = "setting user display" ]; then
  userDisplay=\`echo "\$@" | sed 's/.*\\(-display[ ]*[^ ]*\\).*/\\1/'\`
else
  userDisplay=""
fi

# append wri fonts to the font path if they are not already present

havefonts=\`xlsfonts -fn '-wri-Mathematica*-*-*-*-*-*-*-*-*-*-*-*-*' 2>&1 \$userDisplay | grep -c "wri"\`
font=\$topdir/SystemFiles/Fonts/Type1
[ \$havefonts -lt 2 ] && xset fp+ \$font \$userDisplay
font=\$topdir/SystemFiles/Fonts/$fonttype
[ \$havefonts -lt 2 ] && xset fp+ \$font \$userDisplay
xset fp rehash \$userDisplay

bindir=\$topdir/SystemFiles/FrontEnd/Binaries/$Arch
if [ ! -f \$bindir/MathReader ] ; then
   echo
   echo "-----------------------------------------------------------------------------"
   echo "MathReader executable"
   echo "\$bindir/MathReader"
   echo "not found.  Your MathReader installation may be incomplete or corrupted."
   echo "-----------------------------------------------------------------------------"
   echo
   exit
fi
exec \$bindir/MathReader -topDirectory \$topdir "\$@"

.

# done generating the script

echo $n ".$c"

# set the permissions on the script

chmod 755 "$TopDir/Executables/MathReader"

# create a copy called "mathreader"

cp "$TopDir/Executables/MathReader" "$TopDir/Executables/mathreader" > \
  /dev/null 2>&1
if [ $? -eq 0 ]; then
  echo $n ".$c"
else
  echo " ERROR]"
  echo
  echo "The installer was unable to create the file"
  echo "\"$TopDir/Executables/mathreader\""
  echo "Please make sure that you have appropriate file permissions, and that there is"
  echo "adequate space on the destination drive."
  echo
  exit
fi

# copy both scripts to $ScriptDir

cp -f "$TopDir/Executables/MathReader" "$TopDir/Executables/mathreader" \
  "$ScriptDir" > /dev/null 2>&1

if [ $? -ne 0 ]; then
   echo " ERROR]"
   echo
   echo "The installer was unable to copy the MathReader executable scripts to"
   echo "\"$ScriptDir\""
   echo "However, the rest of the installation was successful and the executable"
   echo "script should be present in \"$TopDir/Executables/\""
   echo "To recover from this error and use MathReader you can either:"
   echo "   (1) cd into \"$TopDir/Executables/\" and run MathReader,"
   echo "   (2) add \"$TopDir/Executables/\" to your \$PATH, or"
   echo "   (3) copy \"$TopDir/Executables/MathReader\" to a different"
   echo "       directory already in your \$PATH"
   echo
   exit
fi

# done

echo " done]"
echo 
echo "-----------------------------------------------------------------------------"
echo "MathReader 4.2 for $Arch is successfully installed on your computer."
echo "The necessary resource files are installed in:"
echo "   $TopDir"
echo "and the executable scripts are:"
echo "   $ScriptDir/MathReader and"
echo "   $ScriptDir/mathreader"
echo
echo "Thank you for installing MathReader 4.2 for $Arch.  To start MathReader,"
echo "type \"mathreader\" (You may need to type \"rehash\" first.)"
echo
echo "-----------------------------------------------------------------------------"
echo
