#! /bin/sh

#############################################
# Install the CLISP version of the debugger #
#############################################

#
# Stuff you can modify
#

#
# Start directory to search for the lisp installation
#

TOP=/usr

# Installation directory of debugger files
#

INSTALLDEBUG=/usr/local/lib/lispdebug

#
# Installation directory of interface program
#

INSTALLINTERFACE=/usr/local/bin


#
# How is the lisp executable called
#

LISP=clisp

#
# Do not modify the next stuff
#


#
# Search for the installation lib of CLISP , by looking at wlisp.run
#

echo "Searching for lisp files ...."
INSTALLPORT=`find $TOP -name "wlisp.run" -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 wlisp.run , is clisp really installed?"
    echo "Quiting installation , debugger is not installed"
    exit -1
elif [ $I = 1 ]; then
    INSTALLPORT=`dirname $INSTALLPORT`
    echo "Found unixport in $INSTALLPORT"
else 
    echo "Found more then one CLISP installation , go to manual installation"
    echo "or modify TOP in install-clisp 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 $INSTALLINTERFACE/interface.old $INSTALLINTERFACE/interface 2> /dev/null
    mv $INSTALLPORT/lispinit.mem.old $INSTALLPORT/lispinit.mem
    echo "Debugger deinstalled"
    exit 0
elif [ ! $1 = "install" ]; then
    echo "Usage : install-clisp {install | deinstall} "
    exit -1
fi

# Compile the debugger

echo "Compiling the debugger ..."
clisp -c debugger.lisp
clisp -c clisp.lisp

# Generating of the parser

clisp -i instclisp1.lisp

# Generating the image
    
echo "(Generating a new image ....."
    
clisp -i instclisp2.lisp

# Installing the debugger

echo "Installing the debugger ..."

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

cp ./lispinit.mem $INSTALLPORT/lispinit.mem

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.fas $INSTALLDEBUG/debugcode.fas.old 2> /dev/null
mv $INSTALLDEBUG/lispsyntax $INSTALLDEBUG/lispsyntax.old 2> /dev/null
cp debugcode.fas $INSTALLDEBUG/debugcode.fas
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"
