#!/usr/bin/python

import pyclamd
from sys import exit
from optparse import OptionParser
from os.path import isfile

parser = OptionParser()

parser.add_option("-p", "--ping", action="store_true", dest="only_ping",
        default=False, help="Only ping clamd server")

(options, args) = parser.parse_args()

only_ping=options.only_ping
PIDFILE='/var/run/clamav/clamd.pid'
SOCKETFILE='/run/clamav/clamd'


def on_error(msg):
    print msg
    exit(1)

try:
    pyclamd.init_unix_socket(filename=SOCKETFILE)
except:
    if not only_ping and not isfile(PIDFILE):
        #Si pas de fichier PIDFILE, l'application n'est pas cense
        #est demarre, par contre, si on ping c'est pour savoir si
        #clam tourne ou non
        exit()
    on_error('Could not reach clamd')

try:
    pyclamd.ping()
except:
    on_error('Could not ping clamd')

if only_ping:
    exit()

try:
    pyclamd.reload()
except:
    on_error('Could not reload clamd')
