#!/bin/sh
TARGET_LIB="/usr/lib/clisp/meta-cvs"
LISPRUN="$TARGET_LIB/lisp.run -M $TARGET_LIB/lispinit.mem"

if [ $# != 1 ] ; then
  echo
  echo "Syntax:"
  echo
  echo "  mcvs-upgrade <path-to-source>"
  echo
  echo "The path specifies the directory which holds unpacked Meta-CVS source,"
  echo "the place where the file mcvs-main.lisp is located."
  echo
  echo "This upgrade mechanism is intended to make it possible to use a"
  echo "newer version of Meta-CVS without having to obtain a complete binary"
  echo "distribution that includes the Lisp executable, and without having"
  echo "to install the Lisp development environment to build the sources."
  echo
  echo "Note that it's not possible to upgrade if the newer Meta-CVS sources"
  echo "rely on new C functions being linked into the Lisp system; get a"
  echo "new binary distribution, or set up CLISP and compile Meta-CVS from"
  echo "the sources using its install.sh script"
  echo
  echo "Ignore the copious compiler warnings about objects and functions"
  echo "being redefined; this is what we want."
  echo
  exit 1
fi

SOURCE_PATH="$1"

if ! cd "$SOURCE_PATH" ; then
  echo "unable to change to $SOURCE_PATH"
  exit 1
fi

if ! $LISPRUN -c mcvs-main ; then
  echo "unable to compile"
  exit 1
fi

if ! $LISPRUN -q -i mcvs-main -x '(ext:saveinitmem "mcvs.mem" :quiet t)' ; then
  echo "unable to load and generate memory image"
  exit 1
fi

if ! cp mcvs.mem $TARGET_LIB/lispinit.mem ; then
  echo "unable to copy new Meta-CVS image to $TARGET_LIB directory."
  exit 1
fi

echo
echo "It appears that Meta-CVS has been re-generated from the given sources."
echo 

