#! /bin/sh

#####################################################
# Script to install the CMU version of the debugger #
#####################################################

#
# Stuff you can modify
#

#
# Start directory to search for LISP
#

TOP=/usr

#
# Installation directory of debugger files
#

INSTALLDEBUG=/usr/local/lib/lispdebug

#
# Installation directory of interface program
#

INSTALLINTERFACE=/usr/local/bin

#
# Do not modify the next stuff
#

#
# Searching for the installation lib of CMUCL , by looking at the lisp.core file
#

echo "Searching for lisp files..."
INSTALL=`find $TOP -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 code in $LISPINSTALL"
else
    echo "Found more then one CMUCL installation , go to manual installation"
    echo "or modify TOP in install-cmucl so that only one installation"
    echo "is found !!!"
    echo "Quiting installation , debugger is not installed"
    exit -1
fi

#
# Check the arguments and do a deinstall if needed
#

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


#
# Do a compilation of the debugger
#

echo "Building and compiling the debugger"
lisp -eval "(load \"instcmu1.lisp\")"
rm dlisp.core 2> /dev/null
lisp -eval "(load \"instcmu2.lisp\")"


#
# Install the files
#

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 "Installing the parser"

if [ ! -d $INSTALLDEBUG ]; then
    mkdir $INSTALLDEBUG
    if [ $? != 0 ]; then
	echo "Could not create the directory : $INSTALLDEBUG"
	echo "The debugger is not installed"
	exit -1
    fi
fi
mv $INSTALLDEBUG/debugcode.x86f $INSTALLDEBUG/debugcode.x86f.old 2> /dev/null
mv $INSTALLDEBUG/lispsyntax $INSTALLDEBUG/lispsyntax.old 2> /dev/null
cp debugcode.x86f $INSTALLDEBUG/debugcode.x86f
cp lispsyntax $INSTALLDEBUG/lispsyntax
	
echo "Installing of the interface"

cd cfiles
./configure
if [ $? != 0 ]; then
    echo "Could not configure the interface"
    echo "The debugger is not installed"
    cd ..
    exit -1
fi
make
if [ $? != 0 ]; then
    echo "Could not make the interface"
    echo "The debugger is not installed"
    cd ..
    exit -1
fi

cd ..

mv $INSTALLINTERFACE/interface $INSTALLINTERFACE/interface.old 2> /dev/null
cp cfiles/interface $INSTALLINTERFACE/interface

echo "Congratulations , unless my installation script has an error,"
echo "you finished installing the debugger"
echo "THE DEBUGGER IS INSTALLED"
