#!/bin/sh
# Usage: clisp-link command [more args]
# where   command = link, add-module
# Bruno Haible 19.10.1994

usage () {
echo "Usage: $0 command ..." 2>&1
exit 1
}

with_dynamic_modules='@with_dynamic_modules@'

if [ $# = 0 ] ; then
  usage
fi

case "$1" in
  link)
    # Usage: clisp-link link dir
    if [ $# != 2 ] ; then
      echo "Usage: $0 link dir" 2>&1
      exit 1
    fi
    dir="$2"
    # Read the variables CC, CFLAGS, CLFLAGS, LIBS, X_LIBS, FILES
    . "$dir"/makevars
    if [ -z "$with_dynamic_modules" ] ; then
      # Generate new modules.o, compiled from modules.c, includes modules.h
      ${CC} ${CFLAGS} -I"$dir" -Ilinkkit -c linkkit/modules.c -o "$dir"/modules.o
      # Generate new lisp.run
      (cd "$dir" ; ${CC} ${CFLAGS} ${CLFLAGS} modules.o lisp.a ${LIBS} ${X_LIBS} -o lisp.run)
    else
      (cd "$dir" ; ${CC} ${CFLAGS} ${CLFLAGS} lisp.a ${LIBS} ${X_LIBS} -o lisp.run)
    fi
    ;;
  add-module)
    # Usage: clisp-link add-module modulename source-dir destination-dir
    if [ $# != 4 ] ; then
      echo "Usage: $0 add-module modulename source-dir destination-dir" 2>&1
      exit 1
    fi
    modulename="$2"
    sourcedir="$3"
    destinationdir="$4"
    if [ ! -d "$sourcedir" ] ; then
      echo "$0: $sourcedir is not a directory" 2>&1
      exit 1
    fi
    if [ ! -f "$destinationdir" ] ; then
      mkdir "$destinationdir"
    fi
    if [ ! -d "$destinationdir" ] ; then
      echo "$0: $destinationdir is not a directory" 2>&1
      exit 1
    fi
    absolute_sourcedir=`cd "$sourcedir" ; /bin/pwd`
    absolute_destinationdir=`cd "$destinationdir" ; /bin/pwd`
    if [ "$absolute_sourcedir" = "$absolute_destinationdir" ] ; then
      echo "$0: directories $sourcedir and $destinationdir may not be the same" 2>&1
      exit 1
    fi
    if [ ! -r "$sourcedir"/lisp.a -o ! -x "$sourcedir"/lisp.run -o ! -r "$sourcedir"/lispinit.mem -o ! -r "$sourcedir"/modules.h -o ! -r "$sourcedir"/makevars ] ; then
      echo "$0: directory $sourcedir does not contain a clisp link kit" 2>&1
      exit 1
    fi
    if [ ! -r linkkit/modules.d -o ! -r linkkit/modules.c -o ! -r linkkit/clisp.h ] ; then
      echo "$0: cannot find modules.d, modules.c, clisp.h" 2>&1
      exit 1
    fi
    if [ ! -r "$modulename/link1.sh" -o ! -r "$modulename/link2.sh" ] ; then
      echo "$0: illegal module name $modulename" 2>&1
      exit 1
    fi
    # Read the variables CC, CFLAGS, CLFLAGS, LIBS, X_LIBS, FILES
    . "$sourcedir"/makevars
    # Prepare the module directory and read the variable NEW_FILES
    . "$modulename"/link1.sh
    if [ -z "$with_dynamic_modules" ] ; then
      # Generate new modules.h
      (cat "$sourcedir"/modules.h ; echo 'MODULE('"$modulename"')') > "$destinationdir"/modules.h
    else
      if [ ! -f "$modulename"/"$modulename"_module.o ] ; then
        # Generate "$modulename"/"$modulename"_module.o
        ${CC} ${CFLAGS} -Ilinkkit -DMODULE="$modulename" -c linkkit/module.cc -o "$modulename"/"$modulename"_module.o
      fi
      NEW_FILES="$modulename"_module.o" $NEW_FILES"
    fi
    # Generate new lisp.a
    for f in ${FILES}; do
      ln "$sourcedir"/$f "$destinationdir"/$f || \
      ln -s "$absolute_sourcedir"/$f "$destinationdir"/$f || \
      cp "$sourcedir"/$f "$destinationdir"/$f
    done
    # Generate other libraries
    for f in ${NEW_FILES}; do
      ln "$modulename"/$f "$destinationdir"/$f || \
      ln -s `/bin/pwd`/"$modulename"/$f "$destinationdir"/$f || \
      cp "$modulename"/$f "$destinationdir"/$f
    done
    # Update the LIBS and FILES variables
    LIBS=${NEW_FILES}' '${LIBS}
    FILES=${FILES}' '${NEW_FILES}
    if [ -z "$with_dynamic_modules" ] ; then
      # Generate new modules.o, compiled from modules.c, includes new modules.h
      ${CC} ${CFLAGS} -I"$destinationdir" -Ilinkkit -c linkkit/modules.c -o "$destinationdir"/modules.o
    fi
    # Generate new lisp.run
    if [ -z "$with_dynamic_modules" ] ; then
      (cd "$destinationdir" ; ${CC} ${CFLAGS} ${CLFLAGS} modules.o lisp.a ${LIBS} ${X_LIBS} -o lisp.run)
    else
      (cd "$destinationdir" ; ${CC} ${CFLAGS} ${CLFLAGS} lisp.a ${LIBS} ${X_LIBS} -o lisp.run)
    fi
    # Generate new lispinit.mem
    . "$modulename"/link2.sh
    # Generate new makevars
    sed -e "s,^LIBS=.*\$,LIBS='${LIBS}'," -e "s,^FILES=.*\$,FILES='${FILES}'," < "$sourcedir"/makevars > "$destinationdir"/makevars
    ;;
  *) usage;;
esac

