#!/bin/sh

#############################################################################
#
# This AwStat installation script has been developed by Jesper Knudsen
# at http://sme.swerts-knudsen.dk
#
# Revision History:
#
# July 12, 2004: 	Initial revision history.
#############################################################################

cls()
{   
for (( i=1 ; i<35 ; i++ ))
do
echo ""
done
}

cls
echo "========================================================"
echo "= AwStats Installation Script                          ="
echo "=                                                      ="
echo "= This script will install AwStats in an ibay          ="
echo "= on your system.                                      ="
echo "=                                                      ="
echo "= No express or implied warranties are provided and its="
echo "= usage is at your own risk.                           ="
echo "=                                                      ="
echo "= If you feel confortable with the above then press    ="
echo "= enter if not pres Ctrl+C to abort the installation   ="
echo "= script.                                              ="
echo "=                                                      ="
echo "========================================================"
if [ $1 ]
then
echo "= Using install ibay as specified on command line = \"$1\""
GAL=$1
else  
echo "= No Install ibay specified using default \"awstats\""
GAL="awstats"
fi
echo "========================================================"
read x
cls


if [ -d /home/e-smith/files/ibays/$GAL ]
then

PRIMARY=http://sme.swerts-knudsen.com
SECONDARY=http://sme.swerts-knudsen.dk

# determine download server....
mkdir downloadtest
cd downloadtest
wget -q --timeout=4 $PRIMARY/index.html
if [ -f index.html ]
then
# download from primary site OK
SITE=$PRIMARY
else
wget -q --timeout=4 $SECONDARY/index.html
if [ -f index.html ]  
then  
# download from mirror site
SITE=$SECONDARY
fi
fi
echo "Downloading RPMs and files from $SITE"
cd ..
rm -rf downloadtest

#Now gallery itself
cd /home/e-smith/files/ibays/$GAL/ 
wget $SITE/downloads/AwStats/awstats-6.4.tgz
tar xzf awstats-6.4.tgz
rm -f awstats-6.4.tgz
cd awstats-6.4
mv wwwroot/cgi-bin/* ../cgi-bin
rm -rf wwwroot/cgi-bin
mv wwwroot/* ../html
mv tools/* ../cgi-bin
cd ..
rm -rf awstats-6.4

cd html
rm -f index.html
wget -N $SITE/downloads/AwStats/index.html.top
mv -f index.html.top index.html

cd ../cgi-bin 

# create a cron job to get the stats updated
echo "#!/bin/bash" > /etc/cron.hourly/awstats                                                                                                                   
echo "cd /home/e-smith/files/ibays/$GAL/cgi-bin" >> /etc/cron.hourly/awstats 
# allow it to be executed
chmod 755 /etc/cron.hourly/awstats

# now get the config files patched and updated the
# cron job with all the local domains.

wget -N $SITE/downloads/AwStats/awstats.domain.conf 

# patch with right install ibay name
sed -e "s/"ibay"/"$GAL"/" awstats.domain.conf > awstats.domain.conf.new
rm -f awstats.domain.conf 
mv awstats.domain.conf.new awstats.domain.conf

wget -N $SITE/downloads/AwStats/awstats_update.pl
chmod 755 awstats_update.pl
./awstats_update.pl $GAL

cd ../html
wget -N $SITE/downloads/AwStats/index.html.footer
cat index.html.footer >> index.html
rm -f index.html.footer
rm -f index.htm

echo "Now Update the stats the first time - please wait...."
echo "This can easily take several minutes so be patient"
cd /etc/cron.hourly
./awstats

# Now notify Swerts-Knudsen that you have installed - only so that I can enhance 
# the scripts that are most in use                 

ERR=`wget -q -t 1 -T 2 --delete-after http://sme.swerts-knudsen.dk/cgi-bin/egometer?AwStats_Script`

else
echo "Ibay called \"$GAL\" does not exist - Installation terminated!"
echo "Please use the Server-Manager and create one."
fi



