#!/bin/bash

# Version 2, 07 Sep 2008

# (C) 2008 Michael Weinberger

############################
# Data directory
############################

mkdir -p /home/e-smith/awstats
chown root.www /home/e-smith/awstats
chmod 750 /home/e-smith/awstats

############################
# Qmail logfile preprocessor
############################

cat <<HERE0 > /home/e-smith/awstats/qmail-preprocessor.sh
#!/bin/bash
/usr/bin/test ! -f /var/log/qmail/*.s && /bin/touch /var/log/qmail/awstats_dummy.s
RR=\`/bin/ls -t /var/log/qmail/*.s|/usr/bin/head -1\`
RR=\`/bin/basename \$RR\`
/usr/bin/test ! -f /var/log/qmail/.recent-rotated && SRC="/var/log/qmail/*.s" && echo \$RR > /var/log/qmail/.recent-rotated
LR=\`/bin/cat /var/log/qmail/.recent-rotated\`
/usr/bin/test "\$LR" != "\$RR" && SRC="/var/log/qmail/\$RR" && echo \$RR > /var/log/qmail/.recent-rotated 
/bin/cat \$SRC /var/log/qmail/current | /usr/local/bin/tai64nlocal | /usr/bin/perl /usr/local/awstats/tools/maillogconvert.pl standard | /bin/grep -v ' alias-localdelivery-'
/bin/rm -f /var/log/qmail/awstats_dummy.s
HERE0

chmod 550 /home/e-smith/awstats/qmail-preprocessor.sh

############################
# Config file for Qmail
############################

DomainName=`config get DomainName`
DNREG=${DomainName/\./\\.}
mkdir -p /etc/awstats

cat <<HERE1 > /etc/awstats/awstats.mail.conf
LogFile="/home/e-smith/awstats/qmail-preprocessor.sh|"
LogType=M
LogFormat="%time2 %email %email_r %host %host_r %method %url %code %bytesd"
LogSeparator=" "
SiteDomain="$DomainName"
HostAliases="localhost 127.0.0.1 REGEX[$DNREG$]"
DNSLookup=2
DirData="/home/e-smith/awstats"
DirCgi="/cgi-bin"
DirIcons="/icon"
AllowToUpdateStatsFromBrowser=0
AllowFullYearView=2
EnableLockForUpdate=0
DNSStaticCacheFile="dnscache.txt"
DNSLastUpdateCacheFile="dnscachelastupdate.txt"
SkipDNSLookupFor=""
AllowAccessFromWebToAuthenticatedUsersOnly=0
AllowAccessFromWebToFollowingAuthenticatedUsers=""
AllowAccessFromWebToFollowingIPAddresses=""
CreateDirDataIfNotExists=0
BuildHistoryFormat=text
BuildReportFormat=html
SaveDatabaseFilesWithPermissionsForEveryone=0
PurgeLogFile=0
ArchiveLogRecords=0
KeepBackupOfHistoricFiles=0
DefaultFile="index.html"
SkipHosts=""
SkipUserAgents=""
SkipFiles=""
SkipReferrersBlackList=""
OnlyHosts=""
OnlyUserAgents=""
OnlyFiles=""
NotPageList="css js class gif jpg jpeg png bmp ico swf"
ValidHTTPCodes="200 304"
ValidSMTPCodes="1 250"
AuthenticatedUsersNotCaseSensitive=0
URLNotCaseSensitive=0
URLWithAnchor=0
URLQuerySeparators="?;"
URLWithQuery=0
URLWithQueryWithOnlyFollowingParameters=""
URLWithQueryWithoutFollowingParameters=""
URLReferrerWithQuery=0
WarningMessages=1
ErrorMessages=""
DebugMessages=0
NbOfLinesForCorruptedLog=50
WrapperScript=""
DecodeUA=0
MiscTrackerUrl="/js/awstats_misc_tracker.js"
LevelForBrowsersDetection=2         
LevelForOSDetection=2               
LevelForRefererAnalyze=2            
LevelForRobotsDetection=2           
LevelForSearchEnginesDetection=2    
LevelForKeywordsDetection=2         
LevelForFileTypesDetection=2        
LevelForWormsDetection=0            
UseFramesWhenCGI=1
DetailedReportsOnNewWindows=1
Expires=0
MaxRowsInHTMLOutput=1000
Lang="auto"
DirLang="./lang"
ShowMenu=1					
ShowSummary=HBM
ShowMonthStats=HBM
ShowDaysOfMonthStats=HBM
ShowDaysOfWeekStats=HBM
ShowHoursStats=HBM
ShowDomainsStats=0
ShowHostsStats=HBL
ShowAuthenticatedUsers=0
ShowRobotsStats=0
ShowWormsStats=0
ShowEMailSenders=HBML
ShowEMailReceivers=HBML
ShowSessionsStats=0
ShowPagesStats=0
ShowFileTypesStats=0
ShowFileSizesStats=0		
ShowOSStats=0
ShowBrowsersStats=0
ShowScreenSizeStats=0
ShowOriginStats=0
ShowKeyphrasesStats=0
ShowKeywordsStats=0
ShowMiscStats=0
ShowHTTPErrorsStats=0
ShowSMTPErrorsStats=1
ShowClusterStats=0
AddDataArrayMonthStats=1
AddDataArrayShowDaysOfMonthStats=1
AddDataArrayShowDaysOfWeekStats=1
AddDataArrayShowHoursStats=1
IncludeInternalLinksInOriginSection=0
MaxNbOfDomain = 10
MinHitDomain  = 1
MaxNbOfHostsShown = 10
MinHitHost    = 1
MaxNbOfLoginShown = 10
MinHitLogin   = 1
MaxNbOfRobotShown = 10
MinHitRobot   = 1
MaxNbOfPageShown = 10
MinHitFile    = 1
MaxNbOfOsShown = 10
MinHitOs      = 1
MaxNbOfBrowsersShown = 10
MinHitBrowser = 1
MaxNbOfScreenSizesShown = 5
MinHitScreenSize = 1
MaxNbOfWindowSizesShown = 5
MinHitWindowSize = 1
MaxNbOfRefererShown = 10
MinHitRefer   = 1
MaxNbOfKeyphrasesShown = 10
MinHitKeyphrase = 1
MaxNbOfKeywordsShown = 10
MinHitKeyword = 1
MaxNbOfEMailsShown = 20
MinHitEMail   = 1
FirstDayOfWeek=1
ShowFlagLinks=""
ShowLinksOnUrl=1
UseHTTPSLinkForUrl=""
MaxLengthOfShownURL=64
HTMLHeadSection=""
HTMLEndSection=""
Logo="awstats_logo6.png"
LogoLink="awstats.pl?config=mail"
BarWidth   = 260
BarHeight  = 90
StyleSheet=""
color_Background="FFFFFF"		
color_TableBGTitle="CCCCDD"		
color_TableTitle="000000"		
color_TableBG="CCCCDD"			
color_TableRowTitle="FFFFFF"	
color_TableBGRowTitle="ECECEC"	
color_TableBorder="ECECEC"		
color_text="000000"				
color_textpercent="606060"		
color_titletext="000000"		
color_weekend="EAEAEA"			
color_link="0011BB"				
color_hover="605040"			
color_u="FFAA66"				
color_v="F4F090"				
color_p="4477DD"				
color_h="66DDEE"				
color_k="2EA495"				
color_s="8888DD"				
color_e="CEC2E8"				
color_x="C1B2E2"				
ExtraTrackedRowsLimit=500
HERE1


############################
# Webserver configuration
############################

mkdir -p /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf

cat <<HERE2 > /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf/86awstats
Alias /awstatsclasses "/usr/local/awstats/wwwroot/classes/"
Alias /awstatscss "/usr/local/awstats/wwwroot/css/"
Alias /awstatsicons "/usr/local/awstats/wwwroot/icon/"
Alias /icon "/usr/local/awstats/wwwroot/icon/"
ScriptAlias /awstats/ "/usr/local/awstats/wwwroot/cgi-bin/"

<Directory "/usr/local/awstats/wwwroot">
SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
allow from all
AuthName "Qmail Statistics"
AuthType Basic
AuthExternal pwauth
require user admin
</Directory>

HERE2
expand-template /etc/httpd/conf/httpd.conf
service httpd-e-smith restart


############################
# Cronjob
############################

mkdir -p /etc/e-smith/templates-custom/etc/crontab
cat <<HERE3 > /etc/e-smith/templates-custom/etc/crontab/awstats
45 */1 * * * root /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=mail >/dev/null

HERE3

expand-template /etc/crontab
