#!/usr/bin/python3
import fcntl
import hashlib
import os
import subprocess
import sys
from pathlib import Path

lock = subprocess.check_output(['rpm', '--eval', '%{_rpmlock_path}'], universal_newlines=True).strip()
if not os.path.isabs(lock) or '%' in lock or '\n' in lock:
    raise SystemExit('Cannot determine the native RPM transaction lock')
def settled_frontend():
    with open(lock, 'r') as transaction:
        fcntl.lockf(transaction, fcntl.LOCK_SH)
        markers = []
        for root in ['/usr/share/zabbix', '/usr/share/zabbix/ui', '/usr/share/zabbix/php']:
            try:
                markers.append(hashlib.sha256((Path(root) / 'include/defines.inc.php').read_bytes()).digest())
            except FileNotFoundError:
                markers.append(None)
        return markers

before = settled_frontend()
while True:
    result = subprocess.run(['/usr/libexec/initmax-widget-deploy-sokobanmax'])
    if result.returncode:
        sys.exit(result.returncode if result.returncode >= 0 else 1)
    after = settled_frontend()
    if before == after:
        break
    before = after
