#!/bin/sh -e

SYSCONFDIR='<%= node[:apache][:dir] %>'

if [ -z $1 ]; then
        echo "Which module would you like to enable?"
        echo -n "Your choices are: "
        ls $SYSCONFDIR/mods-available/*.load | \
        sed -e "s,$SYSCONFDIR/mods-available/,,g" | sed -e 's/\.load$//g;' | xargs echo
        echo -n "Module name? "
        read MODNAME
else
        MODNAME=$1
fi

#figure out if we're on a prefork or threaded mpm
if [ -x /usr/sbin/apache2 ]; then
        PREFORK=`/usr/sbin/apache2 -l | grep prefork || true`
fi

if [ -e $SYSCONFDIR/mods-enabled/$MODNAME.load && -e $SYSCONFDIR/mods-enabled/$MODNAME.conf ]; then
        echo "This module is already enabled!"
        exit 0 
fi

if ! [ -e $SYSCONFDIR/mods-available/$MODNAME.load ]; then
        echo "This module does not exist!"
        exit 1
fi

for i in conf load; do 
        if [ -e $SYSCONFDIR/mods-available/$MODNAME.$i -a ! -e $SYSCONFDIR/mods-enabled/$MODNAME.$i ]; then
        ln -sf $SYSCONFDIR/mods-available/$MODNAME.$i $SYSCONFDIR/mods-enabled/$MODNAME.$i;
        fi
done

echo "Module $MODNAME installed; reload apache to enable."