#!/usr/bin/env python3
from __future__ import annotations
import json, shutil, subprocess, sys

def ver(cmd):
    try:
        p=subprocess.run(cmd,capture_output=True,text=True,errors='replace')
    except OSError as exc:
        return 127,[str(exc)]
    return p.returncode,(p.stdout or p.stderr).splitlines()[:1]
def main():
    tools={}
    for n,c in {'python':[sys.executable,'--version'],'ffmpeg':['ffmpeg','-version'],'ffprobe':['ffprobe','-version'],
                'node':['node','--version'],'npm':['npm','--version'],'7z':['7z']}.items():
        executable=shutil.which(c[0])
        if not executable: tools[n]={'ok':False,'error':'not found'}
        else:
            rc,line=ver([executable,*c[1:]])
            tools[n]={'ok':rc==0 or n=='7z','version':' '.join(line)}
    try: import faster_whisper; tools['faster_whisper']={'ok':True,'version':getattr(faster_whisper,'__version__','unknown')}
    except Exception as e: tools['faster_whisper']={'ok':False,'error':str(e)}
    print(json.dumps(tools,ensure_ascii=False,indent=2))
    if not all(tools[x]['ok'] for x in ('python','ffmpeg','ffprobe','node','npm')): raise SystemExit(2)
if __name__=='__main__': main()
