#!/bin/sh
#--------------------
# PSMT 2.3
# 2013.03.01 update
# 2013.08.22 egrepȽʸ󤬴ְäƤupdaterupdate
# 2013.10.26 åץǡURLˡѹ
# 2016.12.06 freshclamIgnoreΥ顼ˤб褦˽
#            mirrors.dat褦˽
# 2016.12.07 ʬåץǡȤ˼Ԥ˥顼Ƚꤷʤ褦˽
# 2018.03.05 OUTDATEWarnnig̵
# 2018.08.03 URLѥ᡼ΥХ
# 2020.11.13 LIBPATHAIXѡ
# 2021.06.16 0.102.2б
# 2022.01.31 0.103.4б
# 2023.11.16 egrep -v -> grep -v
#-------------------
# update
#-------------------

# ѥ᡼
LOG=/tmp/.proscan_update.log
TEMP=/tmp/.proscan_update.$$
CONFIG=/opt/proscan/agent/conf/psmt_agent.conf
MANAGER=$1
if [ -f $CONFIG ]
then
    . $CONFIG
fi

URL=$2
if [ "$URL" = "" ]
then
    URL=$MANAGER
fi

if [ ! -f $LOG ]
then
    LOG=/tmp/.proscan_update.log
fi

if [ "$RM_MIRRORS_DAT" = "yes" -a -f "/usr/lib/clamav/mirrors.dat" ]
then
    rm /usr/lib/clamav/mirrors.dat > /dev/null 2>&1
fi

/opt/proscan/bin/proscanup -V -U http://$URL > $LOG
RC=$?
res=`egrep "ClamAV update successfully|Installing new ProScan modules successfully" $LOG`
if [ "$res" != "" ]
then
    res=`grep "Nothing to Database update" $LOG`
    if [ "$res" = "" ]
    then
    	update=`date '+%Y/%m/%d %H:%M:%S'`
    fi
fi
egrep "^ERROR:|error|fail" $LOG | grep -v "Incremental update failed|\.cdiff|Ignoring mirror|Recommended version|OUTDATED|disabled||Falling back to HTTP mode" > $TEMP
res=`egrep -i "(error|fail|warning)" $TEMP`
if [ "$res" != "" -o $RC -ne 0 ]
then
    logger -i -t update -p local6.notice "error: proscan update failed (see detail $LOG)"
    exit 1
fi

/opt/proscan/agent/script/postversion $MANAGER "$update"

rm $TEMP
exit 0
