#! /bin/sh

usage() {
    >&2 echo "usage: `basename $0` [ --version ] [ --install | --remove ] [ --notify ] --package package modulepath ..."
    exit 1
}

install=0
remove=0

notify=0

while [ $# -ne 0 ]
do
    case "$1" in
	--version)
	    echo `basename $0` version 1.0, by Yves Arrouye \
		"<arrouye@debian.org>"
	    exit 0
	    ;;
	--install)
	    [ $remove -ne 1 ] || usage
	    install=1
	    ;;
	--remove)
	    [ $install -ne 1 ] || usage
	    remove=1
	    ;;
	--package)
	    [ $# -gt 1 ] || usage
	    package="$2"
	    shift
	    ;;
	--notify)
	    notify=1
	    ;;
	-*)
	    usage
	    ;;
	*)
	    modules=" $modules $1 "
	    ;;
    esac
    shift
done

if [ -z "$modules" ]
then
    >&2 echo `basename $0`: no modules to install or remove
    exit 3
fi

if [ -z "$package" ]
then
    >&2 echo `basename $0`: missing installing package name
    exit 2
fi

if [ $remove -eq 1 ]
then
    install=0
fi

apacheconfig=/usr/lib/apache/config/scripts

if [ -r ${apacheconfig}/apache.sh ]
then
    . ${apacheconfig}/apache.sh
     _apache_sh_testloaded
else
    >&2 echo $me: cannot load informations from ${apacheconfig}
    exit 2
fi

thconf=$TMPDIR/.httpd.conf.$$

for m in $modules
do
    if [ ! -e "$m" ]
    then
	>&2 echo `basename $0`: cannot find module \`$m\'
	exit 3
    fi
    if [ $install -eq 1 ]
    then
	foundp=0
	for d in `echo $APACHE_MODULES_PATH | tr ':' ' '`
	do
	    if [ ! -z "`echo $m | grep \"^$d/\"`" ]
	    then
		foundp=1
		break
	    fi
	done
	if [ $foundp = 0 ]
	then
	    APACHE_MODULES_PATH="${APACHE_MODULES_PATH}:`dirname $m`"
	fi
    else
	modname=`apachemodname $m`
	egrep -v "^[ 	#]*LoadModule[ 	][ 	]*$modname[ 	][ 	]*('$m'|\"$m\"|$m)[ 	#]*" ${httpd} >$thconf && mv $thconf ${httpd}
    fi
done

if [ $install -eq 1 ]
then
    apacheconfig --update

    toload=0

    for m in $module
    do
	if egrep -q "^[ 	]*LoadModule[ 	][ 	]*$modname[ 	][ 	]*('$m'|\"$m\"|$m)[ 	#]*" ${httpd}
	then
	    toload=1
	    break
	fi
    done
    if [ $toload -eq 0 ]
    then
	notify=0
    fi
fi

if [ $notify -eq 1 ]
then
    /etc/init.d/apache reload-modules
fi

