#! /bin/sh

#####################################################
# Script to install the CMU 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
#

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

#
# Do a compilation of the debugger
#

echo "Building and compiling the debugger"
$LISP -eval "(load \"instcmu1.lisp\")"
rm dlisp.core 2> /dev/null
$LISP -eval "(load \"instcmu2.lisp\")"

#
# Install the files
#

echo "Installing the lisp debugger.code"

if [ $TYPE_INSTALL = "overwrite" ]; then
   install_cp dlisp.core $LISP_LIB/lisp.core
else
   install_cp dlisp.core $LISP_LIB/dlisp.core
   echo "Creating the cmucldebug command"
   echo "#! /bin/sh" > ./cmucldebug
   echo "$LISP -core $LISP_LIB/dlisp.core" >> ./cmucldebug
   chmod a+rwx ./cmucldebug
   install_cp ./cmucldebug $LISP_DEBUG_EXEC/cmucldebug
fi

echo "Installing the parser"

mkdir $LISP_DEBUG_LIB 2> /dev/null
install_cp ./debugcode.x86f $LISP_DEBUG_LIB/debugcode.x86f
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_cmucl
cp ./deinstall_cmucl $LISP_DEBUG_EXEC/deinstall_cmucl
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"
