#!/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 package-name:

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

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

FILE="/usr/share/common-lisp/systems/$1.system"
SOURCENAME="/usr/share/common-lisp/source/$1"
if [ ! -h "$SOURCENAME" ] ; then
    cat <<EOF
$0: symlink $SOURCENAME does not exist.

This is where I expected package $1 to be.

Maybe you already uninstalled it?
EOF
    exit 0
fi

if [ ! -e "$FILE" -a ! -h "$FILE" ] ; then                                                                                                                   
    cat <<EOF
$0: $FILE does not exist.  This is needed to  
unregister the $1 package.

Maybe you already uninstalled it?
EOF
    exit 0
fi
# now recompile the stuff
chown -R nobody /usr/lib/common-lisp/

su --shell=/bin/sh --command="
umask 022
for compiler in /usr/lib/common-lisp/bin/*.sh ; do
    if [ -f \$compiler -a -r \$compiler ] ; then
     i=\${compiler##.*/}
     i=\${i%.sh}
     echo Removing for \${i##*/}
     if [ ! -x \"\$compiler\" ] ; then
	cat <<EOF
$0: to recompile $1 with \$(basename \$compiler)
I should use \$compiler, but that script does not
exist or is not executable for me
EOF
	exit 4
     fi
     echo Removing package $0 for \$i
     sh  \"\$compiler\" remove \"$1\"
     echo
     echo Done removing
    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: Package $1 uninstalled
EOF
