#!/bin/bash

PROGNAME=$(basename $0)

# first check if there is at least a package-name:

if [ -z "$1" ] ; then
 cat <<EOF
usage: $PROGNAME package-name

registers a Common Lisp source tree to the 
Common-Lisp-Controller system.
EOF
    exit 1
fi

PKG_NAME="$1"
DEFSYSTEM3_FILE="/usr/share/common-lisp/systems/$PKG_NAME.system"
ASDF_FILE="/usr/share/common-lisp/systems/$PKG_NAME.asd"
SOURCENAME="/usr/share/common-lisp/source/$PKG_NAME"

if [ ! \( -d "$SOURCENAME" -o  -h "$SOURCENAME" \) ] ; then    
    cat <<EOF
$PROGNAME: directory or symlink $SOURCENAME does not exist.

This is where I expected package $PKG_NAME to be.
EOF
    exit 2
fi

if [ ! \( -f "$DEFSYSTEM3_FILE" -o -f "$ASDF_FILE" \) ] ; then
    cat <<EOF
$PROGNAME: Neither $DEFSYSTEM3_FILE nor $ASDF_FILE exists. 
One of these files is needed to register the $PKG_NAME package.
EOF
    exit 3
fi

if [ ! \( -r "$DEFSYSTEM3_FILE" -o -r "$ASDF_FILE" \) ] ; then
    cat <<EOF
$PROGNAME: Neither $DEFSYSTEM3_FILE nor $ADSF_FILE is a regular file. 
One of these files is needed to register the $PKG_NAME package.
EOF
    exit 3
fi

# now recompile the stuff

for compiler in /usr/lib/common-lisp/bin/*.sh ; do
    if [ -f $compiler -a -r $compiler ] ; then
	i=${compiler##*/}
	i=${i%.sh}
        clc-autobuild-check $i $PKG_NAME
        if [ $? = 0 ]; then
	    echo Recompiling package $PKG_NAME for implementation $i
	    /usr/bin/clc-send-command --quiet recompile $PKG_NAME $i
	fi
    fi
done

echo "$PROGNAME: Package $PKG_NAME installed"

