#! /bin/sh

###########################################
# Install the ACL version of the debugger #
###########################################

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

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

# Compile the debugger and generate the parser

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

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

# Generating the image
# Tempory disabled until ACL fixes rebuilding    
# echo "(Generating a new image ....."
#    
# $LISP -e "(load \"instacl2.lisp\")"
#
# Installing the debugger
#
# echo "Installing the debugger ..."
# if [ $TYPE_INSTALL = "overwrite" ]; then
#    install_cp ./save.dxl $LISP_LIB/lisp.dxl
# else
#   install_cp ./save.dxl $LISP_LIB/debug.dxl
#   echo "Creating the acldebug command"
#   echo "#! /bin/sh" > ./acldebug
#   echo "$LISP_LIB/lisp -I $LISP_LIB/debug.dxl" >> ./acldebug
#   chmod a+rwx ./acldebug
#   install_cp ./acldebug $LISP_DEBUG_EXEC/acldebug
# fi

# Tempory fix to install acl with debugger support
echo "Copying debug code"
install_cp ./debugger.fasl $LISP_DEBUG_LIB/debugger.fasl
echo "Creating the acldebug command"
echo "#! /bin/sh" >./acldebug
echo "$LISP_LIB/lisp -L $LISP_DEBUG_LIB/debugger.fasl" >> ./acldebug
chmod a+rwx ./acldebug
install_cp ./acldebug $LISP_DEBUG_EXEC/acldebug


echo "Installing the parser"

mkdir $LISP_DEBUG_LIB 2> /dev/null
install_cp ./debugcode.fasl $LISP_DEBUG_LIB/debugcode.fasl
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_acl
cp ./deinstall_acl $LISP_DEBUG_EXEC/deinstall_acl
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"
