#!/bin/bash
#
# This code was written by BigSoft Limited.
# Please include this message when you use the code in any form.
# Code comes "as is" and without warrenty.
#
# David Newcomb (c) BigSoft Limited 2008
#
 
# Get MySQL's admin password
PSA_PW=`cat /etc/psa/.psa.shadow`
PSA_USER=admin
 
# Location of webstats folders
VHOST=/var/www/vhosts
STATS=statistics/webstat
 
# Find enabled domain names that use awstats statistics.
# When a client is deactivated the status of a domain
# becomes non-zero, so there is no need to join on the
# client's table.
	 
SQL="     SELECT d.name"
SQL="$SQL FROM domains d, hosting h"
SQL="$SQL WHERE d.id = h.dom_id"
SQL="$SQL AND d.status = 0"
SQL="$SQL AND h.webstat = 'awstats'"
 
# Run SQL
DOMAINS=`mysql --skip-column-names -u$PSA_USER -p$PSA_PW -e"$SQL" psa`
 
for DOMAIN in $DOMAINS
do
 
	AWSTATS_DIR=$VHOST/$DOMAIN/$STATS
 
	echo "Checking $AWSTATS_DIR"
	if [ ! -d "$AWSTATS_DIR" ]
	then
		echo "Sorry awstats folder does not exist: $AWSTATS_DIR"
		continue
	fi
 
	cd $AWSTATS_DIR
 
	grep '^<option value="' nav.html | sort -r > nav_options.html
	grep -v '<option value="' nav.html > nav_nooptions.html
 
	ed nav_nooptions.html << EOF
/^<\/select>
-1r nav_options.html
wq
EOF
 
	rm -f nav_options.html
	rm -f nav.html
	mv nav_nooptions.html nav.html
	 
done


