#!/bin/bash
# Script zum Erstellen von Webalizer-Statistiken aller Web-Seiten des Servers
# Version: 0.2
# (C) 2010 Stefan Schaefer -- invis-server.org
# Author: Stefan Schaefer <stefan@invis-server.org>
# Questions: http://forum.invis-server.org
# License: GPLv3

# Globale Einstellungen
rootpackdir="/etc/rootpack"
rootpackconfig="$rootpackdir/rootpack.cfg"
logdir="/var/log/apache2"

# Konfigurationsdaten ermitteln
getconfvalue() {
    cat $rootpackconfig | grep "$1:" | cut -d ":" -f 2
}

# Vhost-Verzeichniss ermitteln
vhostdir=$(getconfvalue VhostDir)

for customerdir in $vhostdir/*; do
    customer=$(basename $customerdir)
    #echo $customer
    domains=(`ls $customerdir |grep -P '(?=^.{1,254}$)(^(?:(?!\d|-)[a-zA-Z0-9\-]{1,63}(?<!-)\.)+(?:[a-zA-Z]{2,})$)'`)
    if (( ${#domains[*]} > 0 )); then
	for domain in ${domains[*]}; do
	    if [[ -f /var/log/apache2/$domain-access_log ]]; then
		echo "Datum $(date +%d.%m.%Y)" > /var/log/webalizer.log
		webalizer -n $domain -o $vhostdir/$customer/$domain/webalizer/ $logdir/$domain-access_log 2>>/var/log/webalizer.log
	    fi
	done
    fi
done

