#!/bin/bash

PROGNAME=$(basename $0)

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

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

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

IMPL=$1
FILE="/usr/lib/common-lisp/bin/$IMPL.sh"

if [ ! -f "$FILE" ] ; then
  cat <<EOF
$PROGNAME: I cannot find the script $FILE for the implementation $IMPL
EOF
  exit 2
fi

if [ ! -r "$FILE" ] ; then
  cat <<EOF
$PROGNAME: I cannot read the script $FILE for the implementation $IMPL
EOF
  exit 2
fi

#install CLC into the lisp

sh "$FILE" install-clc || (echo "Installation of CLC failed" >&2 ; exit 3) 

# now recompile the stuff
for i  in /usr/share/common-lisp/systems/*.asd  ; do
    if [ -f $i -a -r $i ] ; then
     i=${i%.asd}
     package=${i##*/}
     clc-autobuild-check $IMPL $package
     if [ $? = 0 ]; then
	 echo recompiling package $package for implementation $IMPL
	 /usr/bin/clc-send-command --quiet recompile $package $IMPL
     fi
    fi
done

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


echo "$PROGNAME: Compiler $IMPL installed"

