#!/usr/bin/env python3

import sys
from pathlib import Path
from subprocess import getstatusoutput
from pyeole.lock import is_locked, get_system_lock_name

LEN_PF = 26 # value set in /usr/lib/eole/diagnose.sh

cmd = [". /usr/lib/eole/diagnose.sh"]
cmd.append('EchoGras "*** États des verrous EOLE"')
cmd.append('printf "Test des verrous système : "')
if is_locked(level='system'):
    cmd.append('EchoRouge "présent"')
    for lock in get_system_lock_name():
        lock = Path(lock).name
        len_pf = LEN_PF + (len(lock.encode("utf-8")) - len(lock))
        cmd.append(f'printf ". %{len_pf}s => " "verrou {lock}"')
        cmd.append('EchoRouge "présent"')
else:
    cmd.append('EchoVert "absent"')

cmd.append('echo')
cmd_string = "\n".join(cmd)
err, ret = getstatusoutput(cmd_string)
print(ret)
sys.exit(0)
