#! /bin/sh

###########################################
# Install the GCL 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=gcl

#
# Do not modify the next stuff
#


#
# Search for the installation lib of GCL , by looking at unixport
#

echo "Searching for lisp files ...."
INSTALLPORT=`find $TOP -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 unixport , 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 $TOP -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 TOP 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 2> /dev/null
    mv $INSTALLINTERFACE/interface.old $INSTALLINTERFACE/interface 2> /dev/null
    echo "Debugger deinstalled"
    exit 0
elif [ ! $1 = "install" ]; then
    echo "Usage : install-gcl {install | deinstall} "
    exit -1
fi

# Compile the debugger

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

# Generating of the parser

echo "Generating the parser "
echo '(when (not (find-package "DEBUGGER")) (make-package "DEBUGGER"))'\
     '(load "debugger.o")'\
     '(DEBUGGER::process-definition-file "lispsyntax")' | $LISP
# Generating the image
    
echo "(Generating a new image ....."
    
# Generating the image

echo "Generating a new image ...."
echo '(use-package "PCL")'\
     '(when (not (find-package "DEBUGGER")) (make-package "DEBUGGER"))'\
     '(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 "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.o $INSTALLDEBUG/debugcode.o.old 2> /dev/null
mv $INSTALLDEBUG/lispsyntax $INSTALLDEBUG/lispsyntax.old 2> /dev/null
cp debugcode.o $INSTALLDEBUG/debugcode.o
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"
