#!/bin/sh

# This is the installer for packages from the Mathematica Applications Library.
# This installer is for floppy-disk based installation on Unix systems.
# The basic procedure begins by copying the selected archive chunks off
# of the floppy disks. The chunks are concatenated into tar archives. The
# archives are unpacked, then files within the archives are uncompressed.
# (This sequence is intended to reduce disk and memory consumption at any
# one time. Note that different directories can be specified for the working
# directory and the final directory; this may enable you to spread disk usage
# around a bit.) If you are an experienced shell programmer, you may wish
# to modify this installer for your own environment.

# Set basics of installation
clear
# Get current directory, and set various variables.
STARTDIR=`pwd`
LAYOUTFILE=$STARTDIR"/mathapps.layout"

# get behavior of echo -n
DASHN="-n"
if [ ".`echo -n`" = ".-n" ] ; then
   DASHN="" 
fi

# Verify the existence of the layout file
if [ ! -f $LAYOUTFILE ] ; then
   while [ ! -f $LAYOUTFILE ] ;
     do
       echo "Unable to find the layout file mathapps.layout!"
       echo "It should have been untarred with the installation script."
       echo "Please specify a location for the file or terminate this"
       echo $DASHN "installation. [Q/filename] "
       read answer
       if [ \( ".$answer" = ".Q" \) -o \( ".$answer" = ".q" \) -o \
            \( ".$answer" = "." \) ] ; then
          exit 1
       else
          LAYOUTFILE=$answer
       fi
  done
fi


# Set device - check possible disk names in sequence: if one is valid, then
# use it.
if [ -c /dev/rfd0 ] ; then
   INDEVICE=/dev/rfd0
   EJECT=/usr/bin/eject
elif [ -c /dev/rdiskette ] ; then
   INDEVICE=/dev/rdiskette
   EJECT=/usr/bin/eject
elif [ -c /dev/rfh0a ] ; then
   INDEVICE=/dev/rfh0a
   EJECT=/usr/sony/bin/eject
else
   INDEVICE=
   EJECT=""
fi

if [ \( ".$EJECT" = "." \) -a \( -f /usr/bin/eject \) ]; then
   EJECT=/usr/bin/eject
fi

if [ ! -f "$EJECT" ] ; then
   EJECT=""
fi

# Set installation directory. This uses the location declared in the
# mathapps.layout file, which specifies what the pieces of the installation
# are, and where they can be found.
INSTALLDIR=`head -1 $LAYOUTFILE`

# Set working directory.  Set to current directory.

WORKINGDIR=$STARTDIR

# OK, now we let the user validate these choices. We also double-check
# existence and writability of paths.

# First the welcome screen. Grab the name of the application being
# installed from the second line of the mathapps.layout file. I'm sure
# there must be a better way to grab a particular line, but since I
# can't think of it...
APPNAME=`awk 'NR == 2 {print}' $LAYOUTFILE`

echo "Welcome to the Mathematica Applications Library Installation."
echo "This routine will install" $APPNAME"."
echo "If you have problems with this installation, please contact"
echo "Wolfram Research Technical Support at:"
echo "Phone: 1-217-398-6500"
echo "Fax: 1-217-398-0747"
echo "E-Mail: support@wri.com"
echo ""
echo "The installer must know where certain directories and devices are."

while :
  do
    breakok="1"
    echo "These are the current settings:"
    echo "Floppy Drive Device :" $INDEVICE
    echo "Directory for Application :" $INSTALLDIR
    echo "Working Directory for Temporary Files :" $WORKINGDIR
    echo $DASHN "Are these correct? [Y/n/q] "
    read answer
    if [ \( ".$answer" = ".q" \) -o \( ".$answer" = ".Q" \) ] ; then
       echo "Installation terminated by user!"
       exit 1
    fi
    if [ \( ".$answer" != ".Y" \) -a \
         \( ".$answer" != ".y" \) -a \
         \( ".$answer" != "." \) ] ; then
       echo "Please input new values."
       echo $DASHN "Floppy Drive? ["$INDEVICE"] "
       read answer
       if [ ".$answer" != "." ] ; then
           INDEVICE=$answer
       fi
       answer=
       echo "Mathematica Applications Directory?"
       echo $DASHN "  ["$INSTALLDIR"] "
       read answer
       if [ ".$answer" != "." ] ; then
           INSTALLDIR=$answer
       fi
       answer=
       echo $DASHN "Temporary Directory? ["$WORKINGDIR"] "
       read answer
       if [ ".$answer" != "." ] ; then
           WORKINGDIR=$answer
       fi
       echo ""
    fi
    answer=
   # check installation path - does it exist?
    if [ ! -d $INSTALLDIR ] ; then
       echo "The MathApps directory" $INSTALLDIR "does not exist."
       echo $DASHN "Do you want to create it? [Y/n] "
       read answer
       if [ \( ".$answer" = ".Y" \) -o \
            \( "$answer" = "y" \) -o \
            \( "$answer" = "" \) ] ; then
          mkdir $INSTALLDIR
          if [ ! -d $INSTALLDIR ] ; then
              echo "Directory creation failed!"
              echo "Please check your pathnames and permissions..."
              breakok=0
          fi
       else
           breakok=0
       fi
    fi
   # is installation directory writable?
    if [ \( -d $INSTALLDIR \) -a \( ! -w $INSTALLDIR \) ] ; then
        echo "Installation directory not writable!"
        echo "Please check your access permissions..."
        breakok=0
    fi
   # check working directory - does it exist?
    if [ ! -d $WORKINGDIR ] ; then
       echo "The working directory" $WORKINGDIR "does not exist."
       echo $DASHN "Do you want to create it? [Y/n] "
       read answer
       if [ \( ".$answer" = ".Y" \) -o \
            \( ".$answer" = ".y" \) -o \
            \( ".$answer" = "." \) ] ; then
          mkdir $WORKINGDIR
          if [ ! -d $WORKINGDIR ] ; then
              echo "Directory creation failed!"
              echo "Please check your pathnames and permissions..."
              breakok=0
          fi
       else
           breakok=0
       fi
    fi
   # is working directory writable?
    if [ \( -d $WORKINGDIR \) -a \( ! -w $WORKINGDIR \) ] ; then
        echo "Working directory not writable!"
        echo "Please check your access permissions..."
        breakok=0
    fi
    if [ "$breakok" != "0" ] ; then
        break 2
    fi
    echo ""
done

# Suppose the user has changed the working directory to other than the
# current directory. We have to move the files belonging to disk01 to the
# working directory. Note that mathapps.install and mathapps.layout stay in
# the starting directory.
if [ ".$STARTDIR" != ".$WORKINGDIR" ] ; then
   for filename in `awk '/Section:.* 1 / {print $2}' $LAYOUTFILE`
     do
       cp $filename.[0-9]* $WORKINGDIR/
       rm $filename.[0-9]*
    done
fi

# Change to the working directory
cd $WORKINGDIR

# Determine the parts of the package
# and ask me again why I though sh would be the language to use for
# this thing... I really wish TCL were a standard language, or that
# all sysadmins knew how to use Mathematica... --JMN 9.93
echo ""
echo "You can customize the installation of this package."
echo "This package comes in several parts:"
awk '/Description: / {print substr($0, 14)}' $LAYOUTFILE
echo $DASHN "Do you wish to install all the parts? [Y/n] "
read answer
if [ \( ".$answer" != ".Y" \) -a \
     \( ".$answer" != ".y" \) -a \
     \( ".$answer" != "." \) ] ; then
   while :
     do
       echo ""
       INSTALLPARTS=""
       counter=1
       while :
         do
           set `awk 'NR == '$counter' * 2 + 1 {print}' $LAYOUTFILE`
           if [ ".$1" = ".END" ] ; then
              break
           fi
           compname=$2
           descr=`awk 'NR == '$counter' * 2 + 2 {print substr($0, 14)}'\
                      $LAYOUTFILE`
           echo "Install $descr ?"
           echo $DASHN "[Y/n] "
           read answer
           if [ \( ".$answer" = ".Y" \) -o \
                \( ".$answer" = ".y" \) -o \
                \( ".$answer" = "." \) ] ; then
              INSTALLPARTS="$INSTALLPARTS $compname"
           fi
           counter=`expr $counter + 1`
       done
       echo ""
       echo "Are these the parts you want to install?"
       for i in $INSTALLPARTS
         do
           echo `awk '$2 == "'$i'" {this = NR}\
                     (NR == this + 1) && (this > 0) {print substr($0, 14)}'\
                     $LAYOUTFILE`
       done
       echo $DASHN "[Y/n] "
       read answer
       if [ \( ".$answer" = ".Y" \) -o \
            \( ".$answer" = ".y" \) -o \
            \( ".$answer" = "." \) ] ; then
          break
       fi
   done
else
   INSTALLPARTS=`awk '/Section:/ {print $2}' $LAYOUTFILE`
fi

# OK, we have the part names. Let's get the components of each part off
# of the disks, cat them together, then untar them in the installation
# directory.

echo "Loading files...."

for basename in $INSTALLPARTS
  do
    counter=1
    FILELIST=""
    for disk in `awk '/ '$basename' / {for(i=4;i<=NF;++i) print $i}'\
                      $LAYOUTFILE`
      do
        while [ ! -r "$basename.$counter" ] ;
          do
            if [ ".$EJECT" != "." ] ; then
               eval $EJECT
            fi
            echo "Please insert disk "$disk" and hit return to continue."
            read answer
            tar xf $INDEVICE
            if [ "$?" != "0" ] ; then
               echo "Error reading disk!"
            fi
            if [ ! -r "$basename.$counter" ] ; then
               echo "File $basename.$counter not found!"
               echo "Did you insert the right disk? Please try again!"
            fi
        done
        FILELIST="$FILELIST $basename.$counter"
		counter=`expr $counter + 1`
    done
    cat $FILELIST > $basename.tar
    echo "Unpacking file $basename.tar"
    cd $INSTALLDIR
    tar xf $WORKINGDIR/$basename.tar
    cd $WORKINGDIR
    rm $basename.[0-9]*
    rm $basename.tar
done

# Right, now we uncompress things.
echo "Uncompressing files..."
for basename in $INSTALLPARTS
  do
    pathhead=`awk '/ '$basename' / {print $3}' $LAYOUTFILE`
    for filename in `find $INSTALLDIR/$pathhead -name \*.Z -print`
      do
        echo "Uncompressing $filename"
        uncompress $filename
    done
done

if [ ".$EJECT" != "." ] ; then
   eval $EJECT
fi

echo "Installation complete!"
echo
echo "Don't forget to add the directory "$INSTALLDIR
echo "to Mathematica's \$Path."
echo "Actual disk consumption may increase as notebooks are opened"
echo "and the machine-specific binary portions are created."


# Sorry about the inefficiencies in this monster. I forgot how weird
# shell programming was. I would have written this in Mathematica with
# calls to tar and uncompress, but I thought that could cause problems
# with system administrators. TCL would have been a nice choice, too,
# except that relatively few sites have it.
# Oh, well.
# The external calls pretty much consist of:
# tar, compress, awk, expr, test, echo, mkdir, rm, cp