#!/bin/bash
# apachkconfig -- tool for for  maintaining  the apache addon configuration
#                 (e.g  /etc/httpd*/addon.d) directories by relieving system
#                 administrators of the task  of  directly  manipulating
#                 the numerous symbolic links in those directories.
#
# License: GPL
#
# Yury Konovalov <yurix at altlinux dot org>
# (c) 2004
#

VERSION="0.1"
D_ROOT="/etc/httpd-addon"
D_ADDON_CONF="/etc/httpd-addon/init.d"
D_FAVOURS="/etc/httpd-addon/favours"
Favours=""
EffFav="auto"
addon_favours=""
addon_priority="99"
addon_restrictions=""

TEMP=`getopt -o a:d: --long add:,del:,level:,favour:,list:: \
     -n 'ap-chkconfig' -- "$@"`
     
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

eval set -- "$TEMP"

ListifyFavours()
{
echo $1|sed -e "s/\(.\)/\1 /g" -e "s/ $//"
}

LoadFavours()
{
if ! ls $D_FAVOURS/*.favour 2>&1 >/dev/null; then
    echo "No Apache favours found. Sorry."
    echo "maybe 'apt-get install smth' ?"
    exit 1
fi

for f in $D_FAVOURS/*.favour; do
    source $f
    if [ "$(echo "$FAVOUR"|sed -e "s/^[[:alnum:]]\\{1\\}//")" != ""  -o "$FAVOUR" = "" ] ; then
	echo "Error parsing favour config file '$f': Invalid favour '$FAVOUR'"
	echo "Ignoring settings in config file '$f'"
	continue
    fi

    if [ "$(echo "$ADDONCONFD"|sed -e "s/[^[:blank:]]*//")" != "" -o "$ADDONCONFD" = "" -o ! -d "$ADDONCONFD" ]; then
	echo "Error parsing favour config file '$f': Invalid addon directory '$ADDONCONFD'"
	echo "Ignoring settings in config file '$f'"
	continue
    fi
    if [ "$(echo "$ACTIVEPREFFIX"|sed -e "s/[^[:blank:]]*//")" != "" -o "$ACTIVEPREFFIX" = "" ]; then
	echo "Error parsing favour config file '$f': Invalid active preffix '$ACTIVEPREFFIX'"
	echo "Ignoring settings in config file '$f'"
	continue
    fi
    Favours="$Favours$FAVOUR"
    Favours_ConfD="$Favours_ConfD$ADDONCONFD "
    Favours_Preffix="$Favours_Preffix$ACTIVEPREFFIX "
done

Favours=$(ListifyFavours $Favours)

source $D_ROOT/defaults
if [ "$DEFAULT_FAVOUR" == "auto" ] || [ "$DEFAULT_FAVOUR" == "" ]; then
    EffFav="$Favours"
else
    EffFav="$DEFAULT_FAVOUR"
fi
}

SubsetOfFavours()
{ 
 t_subset=""
 t_favours="$2"
 req="$1"
 prevreq="$req"
 for f in $t_favours; do
    req=$(echo "$req"|sed -e "s/$f//g")
    if [ "$prevreq" != "$req" ]; then t_subset="$t_subset$f "; fi
    prevreq="$req"
 done
 echo "$t_subset"
}

SetFavour()
{
Requested=$(SubsetOfFavours "$1" "$Favours")
EffFav="$Requested"
}


GetAddonDefaults()
{
config_line=`cat $D_ADDON_CONF/$1.conf|sed -n -e \
 "s/^#.*apachkconfig[[:blank:]]*\([[:alnum:]-]*\)[[:blank:]]*\([[:digit:]]\{2\}\)[[:blank:]]*\(.*\)/\1 \2 \3/p"`

t_favours=$(echo "$config_line"| awk '{print$1}')
t_priority=$(echo "$config_line"| awk '{print$2}')
t_restrictions=$(echo "$config_line"| awk '{print$3}' | sed -e "s/\([[:alnum:]-]*\)/\1/")

 case "$t_favours" in
     "") addon_favours="$Favours" ;;
    "-") addon_favours="-" ;;
      *) addon_favours=$(ListifyFavours $t_favours) ;;
 esac
 
 if [ "$t_priority" != "" ]; then addon_priority="$t_priority"; fi
 if [ "$t_restrictions" != "" ]; then addon_restrictions="$t_restrictions"; fi
 
}

FavourIDs()
{
t_favar=( $Favours )
FavIDs=""
for f in $(seq 0 $((${#t_favar[@]} - 1))); do
    for a in $1; do
	if [ "$a" = "${t_favar[$f]}" ]; then
	    FavIDs="$FavIDs$f "
        fi
    done
done
echo "$FavIDs"
}

GetStatusOf()
{
t_name="$1"
t_favour="$2"

t_favours_dir_array=( $Favours_ConfD )
t_favours_pref_array=( $Favours_Preffix )

ID=$(FavourIDs "$2")
GetAddonDefaults "$t_name"

if [ "$addon_restrictions" != "" ]; then
    if [ "`echo "$addon_restrictions"|sed -e "s/$t_favour//g"`" != "$addon_restrictions" ]; then
	t_open=1
    else
	t_open=0
    fi
else
    t_open=1
fi

if [ -h ${t_favours_dir_array[$ID]}/${t_favours_pref_array[$ID]}$addon_priority.$t_name.conf ]; then
    if [ $t_open -eq 1 ]; then
	echo "on"
    else
	echo "-ON-"
    fi
else
    if [ $t_open -eq 1 ]; then
	echo "off"
    else
	echo "----"
    fi
fi
}

AddonSymlinks()
{
t_favours="$3"
t_operation="$2"
t_addon="$1"
t_favours_array=( $Favours )
t_favours_dir_array=( $Favours_ConfD )
t_favours_pref_array=( $Favours_Preffix )

FavIDs=$(FavourIDs "$3")
for f in $FavIDs; do
    case "$t_operation" in
    link)
	ln -f -s $D_ADDON_CONF/$t_addon.conf \
	    ${t_favours_dir_array[$f]}/${t_favours_pref_array[$f]}$addon_priority.$t_addon.conf
	;;
    unlink)
	find ${t_favours_dir_array[$f]} -name "${t_favours_pref_array[$f]}*.$t_addon.conf" \
	    -exec rm -f {} \;
	;;
    *) echo "Invalid operation: '$2'" >&2 ; exit 1 ;;
    esac
done
}

Manage()
{
t_active_favours="$EffFav"
GetAddonDefaults "$1"
t_restrictions=$(SubsetOfFavours "$addon_restrictions" "$t_active_favours")

if [ "$addon_restrictions" != "" ]; then
    if [ "$t_restrictions" != "" ]; then
#	echo "$1 addon is restricted to $t_restrictions"
	if [ "$t_active_favours" != "$t_restrictions" ]; then
	    t_active_favours="$t_restrictions"
	fi	
    else
	echo "$1 is restricted to '$addon_restrictions' favours."
	echo "Requested operation not performed..."
	exit 1
    fi
fi

    case "$2" in
	on)
#	    echo "Activate $1 on $t_active_favours favour"
	    AddonSymlinks "$1" link "$t_active_favours"
	    ;;
	off)
#	    echo "Disabling $1 on $t_active_favours favour"
	    AddonSymlinks "$1" unlink "$t_active_favours"
	    ;;
	reset)
#	    echo "Resetting $1 on $addon_favours favour"
	    AddonSymlinks "$1" unlink "$Favours"
	    if [ "$addon_favours" != "-" ]; then
		AddonSymlinks "$1" link "$addon_favours"
	    fi
	    ;;
	add)
#	    echo "Adding $1 on $addon_favours favour"
            if [ "$addon_favours" != "-" ]; then
		AddonSymlinks "$1" link "$addon_favours"
	    fi
	    ;;
	remove)
#	    echo "Removing $1 on $Favours favour"
	    AddonSymlinks "$1" unlink "$Favours"
	    ;;
	*) echo "Invalid operation: $2" >&2 ; exit 1 ;;
    esac
}

Add()
{
Manage "$1" add
}

Del()
{
Manage "$1" remove
}

List()
{
list=$(ls -1 $D_ADDON_CONF/*.conf |sed -e "s:^.*\/\(.*\)\.conf$:\1:")

if [ "$1" != "" ]; then
    list=$(echo "$list"|egrep "^$1\$")
    if [ "$list" = "" ]; then
	echo "Error: No such addon '$1'"
	exit 1
    fi
fi

for v in $list; do
    echo -n "$v "
    for i in $(seq 0 $((30-${#v}))); do echo -n " "; done
	
    for f in $Favours; do
	echo -n "$f:$(GetStatusOf "$v" "$f")	"
    done
    echo
done
}

LoadFavours

while true ; do 
    case "$1" in
	--add) Add "$2" ; exit 0; ;;
        --del) Del "$2" ; exit 0; ;;
	--favour|--level) SetFavour "$2"; shift 2 ;;
	--list) action=list ; shift 2 ;;
	--) shift ; break ;;
	*) echo "Internal error!" ; exit 1 ;;
    esac
done

case "$action" in
    list) List $1; ;;
    *)	if [ ${#@} -eq 2 ]; then
	    Manage $1 $2
	else
	    echo "apachkconfig version $VERSION - Copyright (C) 2004 ALT Linux Team.
This may be freely redistributed under the terms of the GNU Public License.
usage:   apachkconfig --list [name]
         apachkconfig --add <name>
         apachkconfig --del <name>
         apachkconfig [--favour|--level <favours>] <name> <on|off|reset>)"
	fi ;;
esac
