#!/bin/sh
#
#
# Copyright (C) Comunicacion Integral, Las Palmas de Gran Canaria, 1997
#
#
#
# INSTALL_FTP - Install 5D Monsters for Jaleo from FTP Distribution Script.
#
# Version: SGI Irix 6.2(-n32)
#

#
# Set the distribution file name
#

tarfileroot=5dmonstr
tarfile=5dmonstr.tar

#
# Announce what we are.
#

case `xconfirm -c \
        -header "Introduction" \
	-t "I am about to install:" \
	-t "5D Monsters for Jaleo's product V2.7/2.8/2.9/3.0" \
	-t "You should have logged in to the Jaleo Base Account" \
	-t "and then become root (superuser) by this point." \
	-t "You should also have downloaded ${tarfileroot}.tgz" \
	-t "into the same directory as this script." \
	-t "Shall I continue?" \
        -icon info \
        -b "No" -B "Yes"`
        in
        No) echo "Goodbye.";
            exit 0;;
        Yes) echo "Beginning 5D Monsters for Jaleo installation.";;
esac

#
# Check we are root.
#

if [ "`whoami`" != "root" ]; then
        xconfirm -c \
        -header "Error" \
        -icon error \
        -t "You need to be root to run this script. You can become root" \
        -t "by using the command su. Please do this, then restart the" \
        -t "script." \
        -B "OK"
        exit 1
fi

#
# Try to check that we originally logged in as the Jaelo Base Account
# and that Jaleo has been installed on this machine.
#

if [ ! -d ${HOME}/JALEO-ENV ]; then
        xconfirm -c \
        -header "Error" \
        -icon error \
        -t "I cannot find the directory: $HOME/JALEO-ENV !" \
        -t "Are you sure Jaleo 2.7/2.8 has been installed AND" \
        -t "that you logged in as the Jaleo Base Account BEFORE" \
        -t "becoming superuser?" \
        -B "Abort"
        exit 1
fi

#
# Check the distribution file is available.
#

if [ ! -f ./${tarfileroot}.tgz ]; then
        xconfirm -c \
        -header "Error" \
        -icon error \
        -t "I cannot find the file: ./${tarfileroot}.tgz !" \
        -t "Please make sure you have downloaded it into" \
        -t "the same directory this script is in." \
        -B "Abort"
        exit 1
fi

#
# Try to work out what sort of processor we have.
#

processor="";
if [ "`hinv -c processor|grep R2|uniq`" != "" ]; then
	processor="mips1";
fi
if [ "`hinv -c processor|grep R3|uniq`" != "" ]; then
	processor="mips2";
fi
if [ "`hinv -c processor|grep R4|uniq`" != "" ]; then
	processor="mips3";
fi
if [ "`hinv -c processor|grep R5|uniq`" != "" ]; then
	processor="mips4";
fi
if [ "`hinv -c processor|grep R8|uniq`" != "" ]; then
	processor="mips4";
fi
if [ "`hinv -c processor|grep R10|uniq`" != "" ]; then
	processor="mips4";
fi

if [ "$processor" = "" ]; then
	case `xconfirm -c \
        	-header "CPU Type" \
	        -icon question \
		-t "I failed to determine your machines CPU type." \
		-t "Please tell me whether you have a Mips IV compatible CPU" \
		-t "(e.g. R5000, R10000 or R8000) or something else" \
		-t "(e.g. R4400, R4000, R3000, R2000)." \
	        -b "Other" -b "MipsIV"`
	        in
	        Other) processor="Other";;
	        MipsIV) processor="mips4";;
	esac
fi

case "$processor" in
mips4) echo "... CPU ISA is Mips IV compatible ...";
     echo;
     ;;
*) echo "... CPU ISA is NOT Mips IV compatible ...";
        xconfirm -c \
        -header "Error" \
        -icon error \
        -t "Your CPU is not Mips IV compatible." \
        -t "This release of 5D Monsters for Jaleo can only be" \
        -t "used on Mips IV compatible machines. These include" \
        -t "Octanes and O2s but not (most) Indigo 2s. Sorry." \
        -B "Abort";
        exit 1;
     ;;
esac;

#
# Set install_home to the current directory
#

install_home=`pwd`;
if [ "$install_home" = "" ]; then
	install_home="/var/tmp";
fi

#
# Check there is enough space where we are to do the installation
#

ihfree=`df -k ${install_home} | grep fs | awk '{print $5}'`
ihfree=`expr $ihfree \/ 1000`

if [ "`expr $ihfree \< 70`" = "1" ]; then
	xconfirm -c \
        -header "No Free Space (TMPDIR)" \
	-icon error \
	-t "I need 70MB of free space in the temporary directory." \
	-t "You have only ${ihfree}MB free in ${install_home}. " \
	-t "Please find more free space, or set the environment" \
	-t "variable TMPDIR to a directory on a file system" \
	-t "with 70MB free space or more." \
        -B "OK"
        exit 1
fi

#
# Check there is enough disk space where Jaleo lives
#

udfree=`df -k $HOME | grep fs | awk '{print $5}'`
udfree=`expr $udfree \/ 1000`

if [ "`expr $udfree \< 30`" = "1" ]; then
	xconfirm -c \
        -header "No Free Space (HOME)" \
	-icon error \
	-t "I need 30MB of free space on the disk holding Jaleo." \
	-t "You have only ${udfree}MB free on this disk. " \
	-t "Please find more free space, then try again." \
        -B "OK"
        exit 1
fi

#
# Uncompress and extract dist file contents and execute its install script.
#

echo "... Uncompressing installation file ..."
gzip -cd ./${tarfileroot}.tgz > ./${tarfile}
echo "... Extracting contents of installation file ..."
tar xvof ./${tarfile}
rm ./${tarfile}

keptfile="";
case `xconfirm -c \
        -header "Delete Installation File" \
        -icon question \
        -t "Would you like to delete the installation file?" \
        -t "This saves space, but you may want to keep it," \
        -t "perhaps, to install on other machines." \
        -B "Yes" -b "No"`
        in
        No) keptfile="yes";
            echo "... Continuing ...";;
        Yes) keptfile="no";
             rm -f ./${tarfileroot}.tgz;;
esac

#
# Do the installation.
#

cd __5DJL__
./install.sh ${install_home}

#
# Having (hopefully!) completed the installation, delete the evidence.
#

cd ${install_home}
rm -rf __5DJL__
rm install_ftp

if [ "$keptfile" = "yes" ]; then
        xconfirm -c \
        -header "Installation Completed" \
        -icon info \
        -t "All done!" \
        -t "The installation file: ${tarfileroot}.tgz has been kept." \
        -B "OK"
else
        xconfirm -c \
        -header "Installation Done" \
        -icon info \
        -t "All done!" \
        -B "OK"
fi

echo "All Done"
