#! /bin/sh

###########################################
# Install the ACL 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=lisp

#
# Do not modify the next stuff
#


#
# Search for the installation lib of ACL by looking at libacl*.so
#

echo "Searching for lisp files ...."
INSTALLPORT=`find $TOP -name "libacl*.so" -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 library libacl*.so , is Allegro CL really installed?"
    echo "Quiting installation , debugger is not installed"
    exit -1
elif [ $I = 1 ]; then
    INSTALLPORT=`dirname $INSTALLPORT`
    LISP="$INSTALLPORT/$LISP"
    echo "Found libacl*.so in $INSTALLPORT"
else 
    echo "Found more then one Allegro installation , go to manual installation"
    echo "or modify TOP in install-acl 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/lisp.dxl.old $INSTALLPORT/lisp.dxl
    echo "Debugger deinstalled"
    exit 0
elif [ ! $1 = "install" ]; then
    echo "Usage : install-acl {install | deinstall} "
    exit -1
fi

# Compile the debugger and genearint the parser

echo "Compiling the debugger and genearating the parser ..."

$LISP -e "(load \"instacl1.lisp\")"

# Generating the image
    
echo "(Generating a new image ....."
    
$LISP -e "(load \"instacl2.lisp\")"

# Installing the debugger

echo "Installing the debugger ..."

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

cp ./save.dxl $INSTALLPORT/lisp.dxl


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