## Bu dosya ansible tarafindan yonetilmektedir!
## Burada yapilan degisikliklerin uzerine yazilir!!
{{ ansible_managed }}

#!/bin/bash

MAILSEND='/usr/bin/mutt'
LOGGER='/usr/bin/logger'
WGET='/usr/bin/wget'
MD5SUM='/usr/bin/md5sum'
TAR='/bin/tar'
DIFF='/usr/bin/diff'
AWK='/usr/bin/awk'
SED='/bin/sed'
CAT='/bin/cat'
WC='/usr/bin/wc'
PATCH='/usr/bin/patch'
CUT='/usr/bin/cut'
DANSGUARDIAN='/usr/sbin/dansguardian'
LOGFILE='/var/log/updshalla.log'

HEAD='# SHALLA LIST UPDATE SCRIPT FOR DANSGUARDIAN - Conti Remigio V.20100727.0817 #'
ERRMSG1='updshalla.conf file does not exist: must exist in the same folder of updshalla script'
ERRMSG2='The destination folder'
ERRMSG3='does not exist or is not writable'
ERRMSG4='Error during list download operation'
ERRMSG5='Error during list MD5 checksum download operation'
ERRMSG6='Error during MD5 checksum'
ERRMSG7='Error during archive open operation'
ERRMSG8='Error during Categories name matching (downloaded/current): '
ERRMSG9="$MAILSEND not found"
ERRMSG10='Error during file patching'
ERRMSG11='Error during dansguardian reload'
ERRSUB='WEBPROXY01 (dansguardian): updshalla script error'
OKSUB='WEBPROXY01 (dansguardian): updshalla script report'
MAILBODY='See logfile for details'
MSG1='Remove old downloaded list files from'
MSG2='Download list'
MSG3='Download list MD5 Checksum'
MSG4='Check MD5 Checksum'
MSG5='Explode tar.gz archive'
MSG6='Verify Name Categories matching'
MSG7='Insert current domains categories name in downloaded categories domain files'
MSG8='Insert current urls categories name in downloaded categories urls files'
MSG9='Patching original files...'
MSG10='Reload Dansguardian'
MSG11='Remove old .diff files from'
MSG12='DB Modification date/time is'
MSG13='Send report to mail addresses:'

function logmsg {
        TAG='updshalla'
        MSG="$1"
        $LOGGER -t "$TAG" "$MSG"
        echo "`date +%Y.%m.%d-%H:%M:%S` - $MSG"
        echo "`date +%Y.%m.%d-%H:%M:%S` - $MSG">>$LOGFILE
}

function mailmsg {
        SUBJECT="$1"
	BODY="$2"
        $MAILSEND -s "$SUBJECT" -a "$LOGFILE" -- $DESTMAIL <<< "$BODY" 

}

function InsertCategoryName {
	CATTYPE=$1
	cd $LISTBASE
	for CATFILE in `find ./ -name $CATTYPE`;
        	do
                	CATNAME=`head -1 "$CATFILE"`

                	# VERIFY IF NAME CONTAINS ALREADY A TIME/DATE STAMP AND IF YES REPLACE WITH NEW
               		if [ -n "`echo "$CATNAME"|grep DB`" ]; then
                        	BCATNAME=`echo "$CATNAME"|$AWK -F ' DB' '{print $1}'`
                   	  else
                        	BCATNAME=`echo "$CATNAME"|$CUT -d '"' -f 1,2`
                	fi
                	CATNAME="$BCATNAME DB:$DBDATE\""

                	# VERIFY IF DESTINATION FILE HAS NO LINE
                	[ `$CAT "$NLISTBASE/$CATFILE"|$WC -l` == 0 ] && echo>"$NLISTBASE/$CATFILE"

                	$SED -i "1 i $CATNAME" "$NLISTBASE/$CATFILE"
        done


}

BASETIME=`date +%Y%m%d-%H%M%S`

echo >$LOGFILE
echo "$HEAD">>$LOGFILE
echo >>$LOGFILE
echo
echo "$HEAD"
echo

cd `dirname $0`
GLOBALPATH=/etc/dansguardian
. "$GLOBALPATH/updshalla.conf" 1>/dev/null 2>&1

# VERIFY IF updshalla.conf EXIST
[ $? = 1 ] && logmsg "$ERRMSG1" && echo && exit 1

# VERIFY IF MAILSEND BINARY IS PRESENT
[ ! -x $MAILSEND ] && logmsg "$ERRMSG9" && exit 1

# STRIP OUT ARCHIVE AND MD5 FILE NAME
ARCHNAME=`basename "$LISTURL"`
MD5NAME=`basename "$LISTMD5"`

# NEW LIST BASE
NLISTBASE=$GLOBALPATH/lists/blacklists/shalla

# VERIFY IF SHALLALIST FOLDER EXIST AND IS WRITABLE

! [ -w "$LISTBASE" ] && logmsg "$ERRMSG2 $LISTBASE $ERRMSG3" && echo && mailmsg "$ERRSUB" "$ERRMSG2 $LISTBASE $ERRMSG3" && exit 1

# CREATE TMP FOLDER IF NOT EXIST
! [ -d "$GLOBALPATH/tmp" ] && mkdir "$GLOBALPATH/tmp"
cd "$GLOBALPATH/tmp"

# REMOVE OLD CONTENT
logmsg "$MSG1 $GLOBALPATH/tmp"
rm -rf *

# DOWNLOAD LIST
logmsg "$MSG2 $LISTURL"
$WGET -nv -a $LOGFILE -t 5 -w 10m -O "$ARCHNAME" $LISTURL
[ $? = 1 ] && logmsg "$ERRMSG4" && echo && mailmsg "$ERRSUB" "$ERRMSG4" && exit 1

# DOWNLOAD MD5 CHECKSUM
logmsg "$MSG3 $LISTMD5"
$WGET -nv -a $LOGFILE -t 5 -w 10m -O "$MD5NAME" $LISTMD5
[ $? = 1 ] && logmsg "$ERRMSG5" && echo && mailmsg "$ERRSUB" "$ERRMSG5" && exit 1

# MD5 CHECKSUM
logmsg "$MSG4"
$MD5SUM -c --status "$MD5NAME"
[ $? = 1 ] && logmsg "$ERRMSG6" && echo && mailmsg "$ERRSUB" "$ERRMSG6" && exit 1

# GRAB DB MODIFICATION TIME
DBDATE=`date -r $ARCHNAME +%Y%m%d-%H%M%S`
logmsg "$MSG12 $DBDATE"

# EXPLODE ARCHIVE LIST
logmsg "$MSG5"
$TAR -zxf $ARCHNAME
[ $? = 1 ] && logmsg "$ERRMSG7" && echo && mailmsg "$ERRSUB" "$ERRMSG7" && exit 1

# VERIFY NAME CATEGORIES MATCHING
logmsg "$MSG6"
MATCHRES=`$DIFF -qr BL $LISTBASE|$AWK '/Only/ && !/COPYRIGHT/ && !/global_usage/ && !/processed/ && !/diff/ && !/bannedurllist_shalla/ && !/bannedsitelist_shalla/ && !/bannedregexpurllist_shalla/ && !/exceptionurllist_shalla/ && !/exceptionsitelist_shalla/'`
[ -n "$MATCHRES" ] && logmsg "$ERRMSG8 $MATCHRES" && echo && mailmsg "$ERRSUB" "$ERRMSG8 $MATCHRES" && exit 1

# INSERT DOMAINS CATEGORIES NAME IN DOWNLOADED CATEGORIES DOMAINS WITH TIME/DATE STAMP
logmsg "$MSG7"
InsertCategoryName domains

# INSERT URLS CATEGORIES NAME IN DOWNLOADED CATEGORIES URLS
logmsg "$MSG8"
InsertCategoryName urls

# REMOVE OLD .DIFF FILES
logmsg "$MSG11 $LISTBASE"
rm -rf *.diff

# PATCH ORIGINAL FILES
logmsg "$MSG9"
FILEDIFF=$BASETIME.diff
$DIFF -u -b -r . $NLISTBASE >$FILEDIFF
echo >>$LOGFILE
$PATCH -f -p0 < $FILEDIFF 1>>$LOGFILE 2>>$LOGFILE
[ $? = 1 ] && logmsg "$ERRMSG10" && echo && mailmsg "$ERRSUB" "$ERRMSG10" && exit 1
echo >>$LOGFILE
chown -R dansguardian:dansguardian *

# RELOAD DANSGUARDIAN
logmsg "$MSG10"
$DANSGUARDIAN -g
[ $? = 1 ] && logmsg "$ERRMSG11" && echo && mailmsg "$ERRSUB" "$ERRMSG11" && exit 1

# ALL OK SO SEND REPORT
logmsg "$MSG13 $DESTMAIL"
mailmsg "$OKSUB" "$MAILBODY"
