#!/usr/bin/env python

import json
import os
import subprocess

output = {
    "techs": {},
    "system": {
        "distro": "debian",
        "version": "11.6",
    }
}

image = ""
with open("Dockerfile") as f:
    lines = f.readlines()
    for line in lines:
        if line.startswith("FROM debian:"):
            image = line.split(" ")[1].strip()
            continue
        if "build_" in line and "ADD" not in line:
            line = line.replace("RUN ", "").strip()
            line = line.replace("& \\", "")
            line = line.lstrip(" ")
            
            parts = line.split(" ")
            name = parts[0].replace("build_", "").replace(".sh", "")
            if name.startswith("php"):
                name = "php"
            version = parts[1].strip()
            if name not in output["techs"]:
                output["techs"][name] = {
                    "versions": [],
                }
            output["techs"][name]["versions"].append(version)

# os.system(f"docker pull {image}")
system_version = subprocess.check_output(f"docker run --rm {image} cat /etc/debian_version", shell=True)
output["system"]["version"] = system_version.strip().decode()

print(json.dumps(output, indent=4))
