#! /bin/sh

#############################################
# Install the CLISP 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 lispinit.mem is found
# $5 = LISP executable
#

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

###############################################
# 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_clisp
     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_clisp
       echo "if [ $? -ne 0 ]; then" >> ./deinstall_clisp
       echo "   echo \"Serious error during cp, exiting\"" >> ./deinstall_clisp
       echo "   exit -1" >> ./deinstall_clisp
       echo "fi" >> ./deinstall_clisp
       # In case of uninstall clean up the backupcopy
       echo "rm $BACKUPTO" >> ./deinstall_clisp
       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 clispdebug to $LISP_DEBUG_EXEC    *"
   echo "*   which must be used to start clisp in debug mode   *"
else
   echo "* - Overwrite lispinit.mem which causes CLISP    *"
   echo "*   to start always with debugger support           *"
fi
echo "* - Add the command deinstall_clisp 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_clisp

# Compile the debugger

echo "Compiling the debugger ..."

$LISP -i instclisp3.lisp -c debugger.lisp
$LISP -c clisp.lisp

# Generating of the parser

$LISP -i instclisp1.lisp

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

# Installing the debugger

echo "Installing the debugger ..."

if [ $TYPE_INSTALL = "overwrite" ]; then
    install_cp ./lispinit.mem $LISP_LIB/lispinit.mem
else
    install_cp ./lispinit.mem $LISP_LIB/debuginit.mem
    echo "Creating the clispdebug command"
    echo "#! /bin/sh" > ./clispdebug
    echo "$LISP -M $LISP_LIB/debuginit.mem" >> ./clispdebug
    chmod a+rwx ./clispdebug
    install_cp ./clispdebug $LISP_DEBUG_EXEC/clispdebug
fi

echo "Installing the parser"

mkdir $LISP_DEBUG_LIB 2> /dev/null
install_cp ./debugcode.fas $LISP_DEBUG_LIB/debugcode.fas
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 deinstall program

chmod a+rwx ./deinstall_clisp
cp ./deinstall_clisp $LISP_DEBUG_EXEC/deinstall_clisp
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"
