#!/bin/bash

#
# htaccess - program for using .htaccess files without AllowOverride directive
# Copyright (C) 2005 Michal Krenek
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# You can dowload a copy of the GNU General Public License here:
# http://www.gnu.org/licenses/gpl.txt
#
# Author: Michal Krenek (Mikos)
# Email:  mikos@sg1.cz
# Jabber: mikos@njs.netlab.cz
#

# Zakladni promenne
basedir="/var/www"
docsdir="htdocs"
confdir="/etc/apache2/htaccess.d"
apachebin="/usr/sbin/apache2"
apachereload="/etc/init.d/apache2 reload"
htallowed=( "RewriteEngine" "RewriteBase" "RewriteCond" "RewriteRule" )

# Vytvoreni adresare pro konfiguracni soubory Apache (pokud neexistuje)
if [ ! -d "$confdir" ]; then
	mkdir -p $confdir
fi

# Smazani vsech starych konfiguracnich souboru
rm $confdir/*.conf

# Vytvoreni promenne pro grepovani .htaccess souboru
htgrep=""
for ht in ${htallowed[@]}; do
	htgrep="$htgrep -e ^$ht"
done

# Ziskani seznamu cest k .htaccess souborum a seznamu nazvu konfiguracnich souboru pro Apache
if [ "$docsdir" != "" ]; then
	htfindpath="$basedir/*/$docsdir"
else
	htfindpath="$basedir"
fi
htfiles="$(find $htfindpath -name .htaccess | sed "s|/[^/]*$||g")"
conffiles="$(echo "$htfiles" | sed -e "s|$basedir/||g" -e "s|/|_|g")"

# Vytvoreni pole cest k .htaccess souborum a pole nazvu konfiguracnich souboru (a spocitani poctu prvku v nich)
htarray=( $htfiles )
confarray=( $conffiles )
countht=${#htarray[@]}
countconf=${#confarray[@]}

# Ukonceni skriptu, pokud nebyl nalezen zadny .htaccess soubor
if [ "$htfiles" == "" ]; then
	logger -s -t htaccess "No .htaccess file found"
	exit 0
fi

# Overeni zda si odpovida pocet .htaccess souboru a pocet konfiguracnich souboru
if [ $countht -ne $countconf ]; then
	logger -s -t htaccess "Number of .htaccess files is not equal to number of configuration files"
	exit 1
fi

# Vytvoreni samotnych konfiguracnich souboru pro Apache s povolenymi direktivami z .htaccess souboru
for i in $(seq 0 $(($countht-1))); do
	htfile=${htarray[$i]}
	conffile=${confarray[$i]}

	echo "<Directory \"$htfile\">" > $confdir/$conffile.conf
	cat $htfile/.htaccess | grep $htgrep >> $confdir/$conffile.conf
	echo "</Directory>" >> $confdir/$conffile.conf
done

# Kontrola spravnosti konfigurace (a smazani spatnych konfiguraku)
cleanconfig=0
while [ $cleanconfig -eq 0 ]; do
	wrongconfig="$($apachebin -t 2>&1 | grep -o "$confdir/[^/]*.conf")"
	if [ "$wrongconfig" != "" ]; then
		logger -s -t htaccess "Syntax error in file '$wrongconfig'. Removing..."
		rm $wrongconfig
	else
		cleanconfig=1
	fi
done

# Znovunacteni konfiguracnich souboru Apache
$apachereload
