#! /bin/sh

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

#
# Start library to search for the lisp installations
#

TOPSEARCH=/usr

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

if [ $1 = "deinstall" ]; 
   then
       echo "Deinstalling debugger "
       mv $LISPINSTALL/lisp.core.old $LISPINSTALL/lisp.core
       exit 0
elif [ ! $1 = "install" ];
     then 
         echo "Usage : install-cmucl {install | deinstall }"
	 exit -1
fi

# Check if tcl-tk is installed on lisp 

echo "Checking if lisp has TK support ..."
rm TMP 2> /dev/null
lisp -eval "(progn (when (find-package \"TK\") (setf h (open \"TMP\" :direction :output)) (close h)) (quit))"

if [ ! -f TMP ];
   then
        echo "The TK subsystem is not installed on LISP , please install the "
	echo "TK subsystem before installing LISP DEBUGGER"
	echo "Quiting installation , debugger is not installed"
	exit -1
fi

# Start the generation of the core
 
echo "Building the lisp debugger code"
rm dlisp.core 2> /dev/null
lisp -eval "(load \"install-cmucl.lsp\")"

# Copy the neccesary files to the lisp directory and do the linking 

echo "Installing the lisp debugger code"
mv $LISPINSTALL/lisp.core $LISPINSTALL/lisp.core.old 2> /dev/null
mv $LISPINSTALL/dlisp.core $LISPINSTALL/dlisp.core.old 2> /dev/null
cp dlisp.core $LISPINSTALL/dlisp.core
ln $LISPINSTALL/dlisp.core $LISPINSTALL/lisp.core

echo "The lisp debugger is installed"






