#!/usr/bin/env python3

def check_geant4_datasets():
    #Like geant4-config --check-datasets but working with the conda packages,
    #which often strips off a 'G4' in the directory name.
    import subprocess
    import pathlib
    import shlex
    cl = subprocess.run(['geant4-config','--datasets'],capture_output=True,check=True)
    n_ok = 0
    n_fail = 0
    for line in cl.stdout.splitlines():
        name,envvar,path = line.decode().split()
        path = pathlib.Path(path)
        if ( True and not path.is_dir()
             and path.name.startswith('G4')
             and ( path.parent / path.name[2:] ).is_dir() ):
             path = path.parent / path.name[2:]
        if path.is_dir():
             status='INSTALLED'
             n_ok += 1
        else:
             status='NOTFOUND'
             n_fail += 1
        print(f"{name} {status} {shlex.quote(str(path))}")
    if n_ok+n_fail < 11:
        print("ERROR: Too few datasets reported by geant4-config!")
        n_fail=True
    if n_fail:
        raise SystemExit(1)

if __name__=='__main__':
    check_geant4_datasets()
