#!/usr/bin/env python
# -*- coding: utf-8 -*-

#########################################################################
# pyeole.service - manage EOLE services
# Copyright © 2014 Pôle de Compétence EOLE <eole@ac-dijon.fr>
#
# License CeCILL:
#  * in french: http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html
#  * in english http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
#########################################################################
from os.path import isfile
from commands import getstatusoutput
from pyeole.diagnose import test_clamd, test_freshclam
from pyeole.diagnose.diagnose import CLAMD_CONTAINER
from creole.client import CreoleClient

if __name__ == "__main__":
    #clamd
    client = CreoleClient()
    if client.get_creole('activer_clam') == 'oui':
        cmd = [". /usr/lib/eole/diagnose.sh"]
        cmd.append('EchoGras "*** Anti-virus"')
        cmd.append('printf ".  %${len_pf}s => " "Anti-virus"')
        if isfile(CLAMD_CONTAINER):
            ret = test_clamd()
            if ret == '':
                cmd.append('EchoOrange "Non configuré"')
            elif ret == 0:
                cmd.append('EchoVert "Ok"')
            else:
                cmd.append('EchoRouge "Erreur"')
        else:
            cmd.append('Inactif "Anti-virus"')
        #freshclam

        cmd.append('TestPid "Service de maj" freshclam')
        cmd.append('printf ".  %$((len_pf+1))s => " "Dernière maj"')
        ret = test_freshclam()
        if ret['status'] == 'On':
            cmd.append('EchoVert "OK ({0})"'.format(ret['msg']))
        elif ret['status'] == 'Off':
            cmd.append('EchoRouge "Erreur ({0})"'.format(ret['msg']))
        else:
            cmd.append('EchoOrange "{0}"'.format(ret['msg']))
        cmd_string = "\n".join(cmd)
        err, ret = getstatusoutput(cmd_string)
        print ret
