#! /bin/sh

##############################################
# Install the CMUCL version of the debugger. #
##############################################

#
# Start library to search for the lisp installations
#

TOPSEARCH=/usr
LISP=gcl

#
# Search for the installation lib of CMUCL , by looking at 
# the lisp.core file.
#
echo "Searching for lisp files ...."
INSTALLPORT=`find $TOPSEARCH -name "unixport" -printf " %p" -true`
# Count the entries found 
I=0
for FILE in $INSTALLPORT
  do
    I=`expr $I + 1`
  done
if [ $I = "0" ]; then
	echo "Didn't found lisp.core , is gcl really installed?"
	echo "Quiting installation , debugger is not installed"
	exit -1
   elif [ $I = "1" ]; then
	echo "Found unixport in $INSTALLPORT"
   else
	echo "Found more then one GCL installation , go to manual installation"
	echo "or modify TOPSEARCH in install-gcl so that only one installation"
 	echo "is found !!!"
	echo "Quiting installation , debugger is not installed"
	exit -1
fi

INSTALLDIR=`find $TOPSEARCH -name $LISP -printf "%p " -true`
# Count the entries found 
I=0
for FILE in $INSTALLDIR
  do
    I=`expr $I + 1`
  done
if [ $I = "0" ]; then
	echo "Didn't found gcl , is gcl really installed?"
	echo "Quiting installation , debugger is not installed"
	exit -1
   elif [ $I = "1" ]; then
	INSTALLDIR=`dirname $INSTALLDIR`
	echo "Found gcl in $INSTALLDIR"
   else
	echo "Found more then one GCL installation , go to manual installation"
	echo "or modify TOPSEARCH in install-gcl so that only one installation"
 	echo "is found !!!"
	echo "Quiting installation , debugger is not installed"
	exit -1
fi

# Do a deinstallation if asked instead of an installation

if [ $1 = "deinstall" ];
   then
       echo "Deinstalling debugger "
       mv $INSTALLDIR/$LISP.old $INSTALLDIR/$LISP
       if [ -e $INSTALLPORT/saved-debug,old ];
          then 
             mv $INSTALLPORT/saved-debug.old $INSTALLPORT/saved-debug
       fi
       echo "Debugger deinstalled"
       exit 0
elif [ ! $1 = "install" ];
     then
          echo "Usage : install-gcl {install | deinstall} "
	  exit -1
fi

# Check if tcl-tk is installed on lisp 

echo "Checking lisp on TK support ...."

rm TMP
echo "(progn (when (find-package \"TK\") (setf h (open \"TMP\" :direction :output)) (close h)) (bye))" | $LISP

if [ ! -e TMP ];
   then
      echo "LISP contains no support for TK , please add TK support before installing"
      echo "Quiting installation , lisp debugger is not installed"
      exit -1
fi

# Compile the debugger

echo "Compiling the debugger ..."
echo '(load "debugger.lsp")'\
     '(compile-file "debugger.lsp")' | $LISP

# Generating the image

echo "Generating a new image ...."
echo '(use-package "PCL")'\
     '(load "debugger.o")'\
     '(si::save-system "saved-debug")' | $LISP

# Installing the debugger

echo "Installing the debugger ..."

if [ -e $INSTALLPORT/saved-debug ];
   then
       mv $INSTALLPORT/saved-debug $INSTALLPORT/saved-debug.old
       if [ $? -ne 0 ];
          then 
               echo "Saving old version $INSTALLPORT/saved-debug failed ,"
	       echo "Quiting installation , debugger is not installed"
	       exit -1
       fi
fi
cp $INSTALLDIR/$LISP $INSTALLDIR/$LISP.old
if [ $? -ne 0 ]
   then 
       echo "Saving old version $INSTALLDIR/$LISP failed , "
       echo "Quiting installation , debugger is not installed"
       exit -1
fi


cp ./saved-debug $INSTALLPORT/saved-debug
cat $INSTALLDIR/$LISP|./filter $INSTALLPORT > $INSTALLDIR/$LISP
chmod a+rwx $INSTALLDIR/$LISP

echo "Lisp Debugger installed"






