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

#########################################################################
# diagnose
# 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 pyeole.diagnose import test_maj, test_maj_reconfigure, test_need_reboot
from subprocess import getstatusoutput

if __name__ == "__main__":
    cmd = [". /usr/lib/eole/diagnose.sh"]
    cmd.append('EchoGras "*** Mise à jour"')
    cmd.append('printf ".  %$((len_pf+2))s => " "Dernière mise à jour"')
    ret = test_maj()
    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.append('printf ".  %$((len_pf+1))s => " "Reconfigure effectué"')
    ret = test_maj_reconfigure()
    if ret['status'] == 'On':
        cmd.append('EchoVert "OK"')
    elif ret['status'] == 'Off':
        cmd.append('EchoRouge "Erreur ({0})"'.format(ret['msg']))
    cmd.append('printf ".  %$((len_pf+1))s => " "Reboot nécessaire"')
    ret = test_need_reboot()
    if not ret:
        cmd.append('EchoVert "Non"')
    else:
        cmd.append('EchoRouge "Oui"')
    cmd.append('echo')
    cmd_string = "\n".join(cmd)
    err, ret = getstatusoutput(cmd_string)
    print(ret)
