#!/bin/bash
conf_file="/etc/MailScanner/MailScanner.conf"
conf_clam_file="/etc/clamd.conf"
conf_freshclam="/etc/freshclam.conf"
conf_old="/etc/MailScanner/MailScanner.conf.old"
conf_new="/etc/MailScanner/MailScanner.conf.new"
conf_rpmnew="/etc/MailScanner/MailScanner.conf.rpmnew"
    echo "This may take a while to update...";
    if [ -f $conf_rpmnew ]; then
        upgrade_MailScanner_conf $conf_file $conf_rpmnew > $conf_new 2>/dev/null
        mv -f $conf_file $conf_old 
        mv -f $conf_new $conf_file
    fi

    if [ -f $conf_file ]; then
        seds1="s/^[[:space:]]*Scan Messages[[:space:]]*=.*/Scan Messages = \/etc\/MailScanner\/rules\/virus-scan.rules/g; "
        seds2="s/^[[:space:]]*Virus Scanners[[:space:]]*=.*/Virus Scanners = clamd/g; " 
        seds3="s/^[[:space:]]*.*Incoming Work User[[:space:]]*=.*/Incoming Work User = clamav/g; " 
        seds4="s/^[[:space:]]*.*Clamd Socket[[:space:]]*=.*/Clamd Socket = \/var\/run\/clamav\/clamd.socket/g; " 
        seds5="s/^[[:space:]]*.*Clamd Lock File[[:space:]]*=.*/Clamd Lock File = \/var\/lock\/subsys\/clamd/g; " 
        seds6="s/^[[:space:]]*.*Spam Checks[[:space:]]*=.*/Spam Checks = no/g; "
        sed -e "$seds1 $seds2 $seds3 $seds4 $seds5 $seds6" $conf_file > $conf_file.$$
        mv -f $conf_file.$$ $conf_file
    fi
    
    if [ -f $conf_freshclam ]; then
        seds1="s/^[[:space:]]*.*UpdateLogFile[[:space:]]*.*/UpdateLogFile \/var\/log\/clamav\/freshclam.log/g; "
        sed -e "$seds1 " $conf_freshclam > $conf_freshclam.$$
        mv -f $conf_freshclam.$$ $conf_freshclam
        if [ ! -f /var/log/clamav ]; then
            mkdir -p /var/log/clamav
        fi
        touch /var/log/clamav/freshclam.log 2> /dev/null
    fi


    if [ -f $conf_clam_file ] ; then
        seds1="s/^[[:space:]]*.*TCPAddr[[:space:]]*.*/#TCPAddr/g; "
        seds2="s/^[[:space:]]*.*TCPSocket[[:space:]]*.*/#TCPSocket/g; "
        seds3="s/^[[:space:]]*.*LocalSocket[[:space:]]*.*/LocalSocket \/var\/run\/clamav\/clamd.socket/g; "
        sed -e "$seds1 $seds2 $seds3 " $conf_clam_file > $conf_clam_file.$$
        mv -f $conf_clam_file.$$ $conf_clam_file
        /usr/sbin/useradd -c "Clam AntiVirus" -s /sbin/nologin -r -d /var/lib/clamav clamav 2> /dev/null
        chown clamav.clamav /var/log/clamav/freshclam.log
        if chkconfig --list clamd 2>&1>/dev/null; then
            /etc/init.d/clamd restart;
        else
            /etc/init.d/clamav restart;
        fi;
    fi
    /etc/init.d/MailScanner restart;    
    echo "Complete...";
