#! /bin/sh

###########################################
# Install the GCL version of the debugger #
###########################################

#
# $1 = overwrite / add
# $2 = Prefix default install directory of debugger
# $3 = "NOTFOUND" if lisp version doesn't exist
# $4 = The directory where lisp.core is found
# $5 = LISP executable
# $6 = tkl.o
#

if [ $3 = NOTFOUND ]; then
    echo "I can't add a debugger if the LISP doesn't exist"
    exit -1
fi

###############################################
# Definition of certain environment variables #
###############################################

TYPE_INSTALL=$1
LISP=$5
LISP_LIB=$4
LISP_DEBUG_LIB=$2/lib/lispdebug
LISP_DEBUG_EXEC=$2/bin
LISP_TKLO=$6


###############################################
# Definition of a version of cp which can be  #
# undone by running a uninstall program       #
###############################################


function install_cp ()
{
    FROM=$1
    TO=$2
     # Generate a name for BACKUPTO which is unique
    i=0
    BACKUPTO=$TO
    while test -e $BACKUPTO
       do
	    i=`expr $i + 1`
	    BACKUPTO=$TO$i
       done
    # Decide if the uninstall command is a rm or a cp back
    if [ $TO = $BACKUPTO ]; then
       # the uninstall command should be a remove ,and 
       # I must not make a backup
       echo "Executing::cp $FROM $TO"
       cp $FROM $TO
       if [ $? -ne 0 ]; then
          echo "Serious error during cp, exiting"
	  exit -1
       fi
       echo "rm $BACKUPTO" >> ./deinstall_gcl
     else
       # the uninstall command should do a copy back
       # I must make a backup
       echo "Executing::cp $TO $BACKUPTO"
       cp $TO $BACKUPTO
       if [ $? -ne 0 ]; then
	  echo "Serious error during cp, exiting"
	  exit -1
       fi
       echo "cp $BACKUPTO $TO" >> ./deinstall_gcl
       echo "if [ $? -ne 0 ]; then" >> ./deinstall_gcl
       echo "   echo \"Serious error during cp, exiting\"" >> ./deinstall_gcl
       echo "   exit -1" >> ./deinstall_gcl
       echo "fi" >> ./deinstall_gcl
       # In case of uninstall clean up the backupcopy
       echo "rm $BACKUPTO" >> ./deinstall_gcl
       echo "Executing::cp $FROM $TO"
       cp $FROM $TO
       if [ $? -ne 0 ]; then
          echo "Serious error during cp, exiting"
	  exit -1
       fi
     fi
}

########################################################
# Start intallation process
########################################################

# Tell user what will happen

echo "*****************************************************"
echo "* I'm gooing to do the following:                   *"
if test $TYPE_INSTALL = add; then
   echo "* - Add the command gcldebug to $LISP_DEBUG_EXEC    *"
   echo "*   which must be used to start gcl in debug mode   *"
else
   echo "* - Overwrite gcl  which causes GCL    *"
   echo "*   to start always with debugger support           *"
fi
echo "* - Add the command deinstall_gcl to                *"
echo "*   $LISP_DEBUG_EXEC, to deinstall the debugger     *"
echo "*****************************************************"
echo

# Ask user to continue

echo -n "Do you want to continue (Y/N)"
read answord

case $answord in 
y);;
Y);;
*) echo "Exiting program"
   exit 0 ;;
esac

# Start with the installation

# Create the first line in the installation script

echo "#! /bin/sh" > ./deinstall_gcl

# Remove .o files

rm *.o

# Compile the debugger

echo "(load \"$LISP_TKLO\")" > ./tmp.lisp
cat gcl.lisp >> ./tmp.lisp

echo "compiling the debugger"
echo "(push 'gclload *features*) (when (not (find-package \"DEBUGGER\")) (make-package \"DEBUGGER\")) (compile-file \"debugger.lisp\") (compile-file \"tmp.lisp\")" | $LISP
cp ./tmp.o ./gcl.o

# 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
echo "(push 'gclload *features*) (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 ...."
#echo '(when (find-package "PCL") (use-package "PCL"))'\
#     '(when (not (find-package "DEBUGGER")) (make-package "DEBUGGER"))'\
#     '(load "debugger.o")'\
#     '(si::save-system "saved-debug")' | $LISP
echo "(load \"debugger.o\") (si::save-system \"saved-debug\")" | $LISP

# Installing the debugger

echo "Installing the debugger ..."

install_cp ./saved-debug $LISP_LIB/saved-debug
echo "Modifying the gcl command"
echo "exec $LISP_LIB/saved-debug\\" > ./gcldebug
echo " -dir "`dirname $LISP_LIB`"/ \\" >> ./gcldebug
echo " -eval '(setq si::*allow-gzipped-file* t)' \\" >> ./gcldebug
echo " "'$@' >> ./gcldebug
chmod a+rwx ./gcldebug

if [ $TYPE_INSTALL = "overwrite" ]; then
   install_cp ./gcldebug $LISP_DEBUG_EXEC/gcl
else
   install_cp ./gcldebug $LISP_DEBUG_EXEC/gcldebug
fi


echo "Installing the parser"

mkdir $LISP_DEBUG_LIB 2> /dev/null
install_cp ./debugcode.o $LISP_DEBUG_LIB/debugcode.o
install_cp lispsyntax $LISP_DEBUG_LIB/lispsyntax

echo "Installing of the interface"

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

cd ..


install_cp cfiles/interface $LISP_DEBUG_EXEC/interface

# Install the deinstall program

chmod a+rwx ./deinstall_gcl
cp ./deinstall_gcl $LISP_DEBUG_EXEC/deinstall_gcl
if [ $? -ne 0 ]; then 
   echo "Error copying deinstall program"
   exit -1
fi


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