#!/bin/sh

if [ `id -u` != 0 ] ; then
 echo You need to be root to run $0
 exit 1
fi

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

if [ -z "$1"  ] ; then
 cat <<EOF
$0 compiler-name 

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

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

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

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

#install the defsystem into the lisp

sh "$FILE" install-defsystem || (echo Installation defsystem failed ; exit 3) 

# now recompile the stuff
chown -R nobody /usr/lib/common-lisp/

su --shell=/bin/sh --command="
umask 022
for i  in /usr/share/common-lisp/systems/*.system  ; do
    if [ -f \$i -a -r \$i ] ; then
     i=\${i%.system}
     package=\${i##*/}
     echo Recompiling package \$package for $1
     sh  \"$FILE\" rebuild \"\$package\"
     echo
     echo Done rebuilding
   fi
done" nobody || echo Installation failed 
chown -R root.root /usr/lib/common-lisp/
chmod a+r,a+X,og-w,-s -R /usr/lib/common-lisp/
cat <<EOF
$0: Compiler $1 installed
EOF
