#!/bin/bash -e
# Sets the autobuild status for a Common Lisp Controller Library Package
# Written by Kevin Rosenberg <kmr@debian.org>
# GPL-2 license

progname="$(basename $0)"
pversion='$Revision: 1.5 $'

if [ `id -u` != 0 ] ; then
 echo $progname: you need to be root to run this program
 exit 1
fi

usage()
{
        cat >&2 <<END
Usage: $progname library value [impl ...] [OPTIONS]

$progname sets the autobuilding for a Common Lisp
library. The autobuild value can be "yes", "no",
or "inherit". If it is "inherit", then the library's
status will be inherited from the default autobuild status
for each implementation

If no implementations are specified on the command line, then
autobuilding is set for all known implementations.

Options: 
  -h                  Print this short help message
END
}

if [ $# -lt 2 ]; then usage; exit 1; fi

# make sure that first argument is not an option switch
if [ $(expr "$1" : "\(\-\)") ]; then
  usage
  exit 1
fi

known_impl="cmucl-normal cmucl-safe cmucl-small sbcl sbcl-mt clisp acl alisp alisp8 mlisp mlisp8 lispworks openmcl scl"

library=$1
shift
autobuild=$(echo $1 | tr "[A-Z]" "[a-z]")
shift

if [ "$autobuild" != "yes" -a "$autobuild" != "no" -a "$autobuild" != "inherit" ]; then
    echo "$PROGNAME: the autobuild status must be \"yes\", \"no\", or \"inherit\"." >&2
    usage
    exit 1
fi

# Command line
impls=""
while [ $# != 0 ]; do
    case "$1" in
        -h) usage; exit 0         ;;
	-*) usage; exit 1         ;;
	*)  
	    arg=$1
	    if [ "$arg" = "cmucl" ]; then
		arg="cmucl-normal cmucl-safe cmucl-small"
	    elif [ "$arg" = "allegro" ]; then
		arg="acl alisp alisp8 mlisp mlisp8"
	    fi
	    impls="$arg $impls"
	    ;;
    esac
    shift
done

if [ ! "$impls" ]; then
  impls="$known_impl"
fi

for impl in $impls; do
    mkdir -p /etc/common-lisp/$impl/$library
    if [ "$autobuild" = "yes" ]; then
	echo "yes" > /etc/common-lisp/$impl/$library/autobuild
    elif [ "$autobuild" = "no" ]; then
	echo "no" > /etc/common-lisp/$impl/$library/autobuild
    else
	rm -f /etc/common-lisp/$impl/$library/autobuild
    fi
done
