#!/bin/bash
# $Header: /cvs/WebShield/wsrc/files/mgmt/xmlconv.d/e01hfchanges,v 1.7 2002/09/09 15:15:29 bwhittak Exp $
#
# (C) Copyright Network Associates Inc. 2001
#
if [ -f $NETAWSS/.profile.vars ]; then
    . $NETAWSS/.profile.vars
else
    . /var/NAIENV/.profile.vars
fi
[[ "$PATH" == "$wsPATH":* || -z "$wsPATH" ]] || PATH=$wsPATH:$PATH
shopt -s extglob
shopt -s nullglob

# we only do files with VersionWebshield v1.0 (e500 v2.0 and e250 v2.1)
[[ -n $OLDWSXVER && $OLDWSXVER != *'v1.0'?( *) ]] && exit

#********************************************************
# ChangeWSxmlFile - This function takes a path to a 
# WebShield.xml file and applies changes as needed.
#*********************************************************
ChangeWSxmlFile()
{
    unset XMLsed XMLpatches
    declare -a XMLpatches
    tsed='
        /name="http-from-/,/<\/Service/ s/wsn="handoff-host"/wsn="handoff"/
    '
    sed -e "$tsed" $1 | diff -q $1 - >/dev/null ||
	XMLsed=$XMLsed$tsed XMLpatches=( "${XMLpatches[@]}" 'handoff hosts' )
    tsed='
    /\<name="http-gw"/,/<\/Service/{
		    /wsn="timeout"/s/>[0-9]\+</>60</
		    /wsn="accept-count"/s/>4</>20</
		    /wsn="max-scan-mem"/s/>4000</>50</
		    /cname="DefaultMaxScanMemory"/s/>[0-9]\+</>50</
		    /cname="DefaultAcceptCount"/s/>[0-9]\+</>20</
		    /cname="L3MaxScanMemory"/s/>[0-9]\+</>50</
		    /cname="L3AcceptCount"/s/>[0-9]\+</>20</
		}
    '
    sed -e "$tsed" $1 | diff -q $1 - >/dev/null ||
	XMLsed=$XMLsed$tsed XMLpatches=( "${XMLpatches[@]}" 'http defaults' )
    fgrep -qs 'cname="InitialGreeting"' $1 ||
    sed_smtparms='/\<name="smap"/,/<\/Service/{
    /\([[:space:]]*\)<\/Service/s%%\1	<Property cname="DirectSendSizeLimit" wsn="smtp-direct-size-limit">0</Property>\
    \1	<Property cname="DirectSendRecipientLimit" wsn="smtp-direct-recipient-limit">1</Property>\
    \1	<Property cname="InitialGreeting" wsn="smtp-connect-timeout">300</Property>\
    &%
    }
    /\<name="\(retry\|log-retry\)"/,/<\/Service/{
    /\([[:space:]]*\)<\/Service/s%%\1	<Property cname="InitialGreeting" wsn="smtp-connect-timeout">300</Property>\
    &%
    }
    '
    fgrep -qs '"smtp-direct-send-' $1 &&
    sed_smtparms=$sed_smtparms'
    s/wsn="smtp-direct-send-\(recipient\|size\)-limit"/wsn="smtp-direct-\1-limit"/g
    '
    [[ -n "$sed_smtparms" ]] && XMLsed="$XMLsed
    $sed_smtparms" XMLpatches=( "${XMLpatches[@]}" 'SMTP parameters' )
    fgrep -qs 'wsn="min-deferred-mb-space"' $1 ||
    XMLsed=$XMLsed'
    /\<name="smap"/,/<\/Service/{
    /\([[:space:]]*\)<\/Service/s%%\1	<Property cname="MinimumDeferredStorageAvailable" wsn="min-deferred-mb-space">200</Property>\
    &%
    }
    ' XMLpatches=( "${XMLpatches[@]}" 'SMTP minimum free space' )
    tsed='
    /\<name="\(smap\|http-gw\|pop3-pdk\|ftp-gw\)"/,/\(<\/Service\|wsn="allow-locked-cab"\)/{
    /wsn="allow-locked-cab"/s/>no</>yes</
    /\([[:space:]]*\)<\/Service/s%%\1	<Property cname="AllowLockedCab" wsn="allow-locked-cab">yes</Property>\
    &%
    }
    '
    sed -e "$tsed" $1 | diff -q $1 - >/dev/null ||
	XMLsed=$XMLsed$tsed XMLpatches=( "${XMLpatches[@]}" 'AllowLockedCab' )
    fgrep 'wsn="use-dns"' $1 | fgrep -qv 'check="makesame"' &&
    XMLsed=$XMLsed'
    /wsn="use-dns"/{
	/\<check="makesame"/!s/\(wsn="use-dns"[^>]*\)>/\1 check="makesame">/
    }
    ' XMLpatches=( "${XMLpatches[@]}" 'DNSEnable makesame' )
    fgrep -qs 'wsn="subject-virus-removed-message"' $1 ||
    XMLsed=$XMLsed'
    /\<name="smap-from-/,/<\/Service/{
    /\([[:space:]]*\)<\/Service/s%%\1	<Property cname="VirusMessageSubject" wsn="subject-virus-removed-message">off</Property>\
    &%
    }
    ' XMLpatches=( "${XMLpatches[@]}" 'VirusMessageSubject' )
    grep -qs '\<cname="TimeOut"[^<]*6000</Property>' $1 &&
    XMLsed=$XMLsed'
    /\<name="pop3-pdk"/,/<\/Service/{
    /\<cname="TimeOut"/s/>6000</>60</
    }
    ' XMLpatches=( "${XMLpatches[@]}" 'POP3 timeout' )
    # POP3 timeout
    tsed='
    /\<name="pop3-pdk"/,/<\/Service/{
    /wsn="timeout"/s/>6000</>60</
    }
    '
    sed -e "$tsed" $1 | diff -q $1 - >/dev/null ||
	XMLsed=$XMLsed$tsed XMLpatches=( "${XMLpatches[@]}" 'POP3 timeout' )
    # POP3 memory
    tsed='
    /\<name="pop3-pdk"/,/<\/Service/{
    /cname="MaxScanMemory"/s/>4000</>500</
    /cname="DefaultMaxScanMemory"/s/>[0-9]*</>500</
    /cname="L2MaxScanMemory"/s/>[0-9]*</>200</
    /cname="L4MaxScanMemory"/s/>[0-9]*</>500</
    /cname="L5MaxScanMemory"/s/>[0-9]*</>400</
    }
    '
    sed -e "$tsed" $1 | diff -q $1 - >/dev/null ||
	XMLsed=$XMLsed$tsed XMLpatches=( "${XMLpatches[@]}" 'POP3 memory' )
    # FTP memory
    tsed='
    /\<name="ftp-gw"/,/<\/Service/{
    /cname="MaxScanMemory"/s/>4000</>500</
    /cname="DefaultMaxScanMemory"/s/>[0-9]*</>500</
    /cname="L1MaxScanMemory"/s/>[0-9]*</>500</
    }
    '
    sed -e "$tsed" $1 | diff -q $1 - >/dev/null ||
	XMLsed=$XMLsed$tsed XMLpatches=( "${XMLpatches[@]}" 'FTP memory' )
    # proxy recycle
    tsed='
    /\<name="\(pop3-pdk\|ftp-gw\|http-gw\)"/,/\(<\/Service\|wsn="memory-limit"\)/{
    /\([[:space:]]*\)<\/Service/s%%\1	<Property cname="MaxMemory" wsn="memory-limit">75000</Property>\
    \1	<Property cname="ProxyRefreshRate" wsn="proxy-refresh-rate">0</Property>\
    &%
    }
    /\<name="smap"/,/\(<\/Service\|wsn="max-mbytes-per-proces"\)/{
    /\([[:space:]]*\)<\/Service/s%%\1	<Property cname="RecycleAfterBytesProcessed" wsn="max-mbytes-per-proces">6</Property>\
    &%
    }
    '
    sed -e "$tsed" $1 | diff -q $1 - >/dev/null ||
	XMLsed=$XMLsed$tsed XMLpatches=( "${XMLpatches[@]}" 'proxy recycle' )
    # AvengineAbortCount
    tsed='
    /\<name="smap"/,/\(<\/Service\|cname="AvengineAbortCount"\)/{
    /\([[:space:]]*\)<\/Service/s%%\1	<Property cname="AvengineAbortCount" wsn="avengine-abort-count">50</Property>\
    &%
    }
    '
    sed -e "$tsed" $1 | diff -q $1 - >/dev/null ||
	XMLsed=$XMLsed$tsed XMLpatches=( "${XMLpatches[@]}" 'AvengineAbortCount' )
    # Copyright date
    tsed='
    s/2001, Networks Associates/2002, Networks Associates/g
    '
    sed -e "$tsed" $1 | diff -q $1 - >/dev/null ||
	XMLsed=$XMLsed$tsed XMLpatches=( "${XMLpatches[@]}" 'Copyright date' )

    [[ ${#XMLpatches[@]} -gt 0 ]] || return 0	# nothing to do
    test -f $1 || return 0	# don't bother if it's not there
    printf "Applying the %s patch to $1\n" "${XMLpatches[@]}"
    if sed -e "$XMLsed" $1 > $1.new; then
	echo Backing up original file
	cp -p $1 $1.bak
	[[ -s $1.new ]] && cp -f $1.new $1 || retVal=1
	rm -f $1.new
    else
	echo "... failed!"
	retVal=1
    fi
    return $retVal
}

# edit the files
for f; do
    ChangeWSxmlFile "$f"
done
