import os

installed = [
    {
        "name":"fmt"
    },
    {
        "name":"zlmediakit"
    },
    {
        "name":"Boost",
        "child":[
            {
                "name":"json"
            }
        ]
    },
]
cmake_lines = []

cmake_finds = []
cmake_links = []

cmake_lines.append(f'cmake_minimum_required(VERSION 4.0)\n')
cmake_lines.append(f'project(jtjai_media)\n')
cmake_lines.append(f'set(CMAKE_CXX_STANDARD 11)\n')


for lib in installed:
    childs = lib.get("child", [])
    if  len(childs) > 0:
        child_names = [child["name"] for child in childs]
        cmake_finds.append(
            f'find_package({lib["name"]} COMPONENTS {" ".join(child_names)} REQUIRED)\n'
        )
        link_components = " ".join(f'{lib["name"]}::{name}' for name in child_names)
        cmake_links.append(f'target_link_libraries(jtjai_media PRIVATE {link_components})\n')
    else:
        cmake_finds.append(f'find_package({lib["name"]} CONFIG REQUIRED)\n')
        cmake_links.append(f'target_link_libraries(jtjai_media PRIVATE {lib["name"]}::{lib["name"]})\n')

for line in cmake_finds:
    cmake_lines.append(line)

cmake_lines.append('add_executable(jtjai_media main.cpp)\n')

for line in cmake_links:
    cmake_lines.append(line)

with open('CMakeLists.txt','a') as f:
    f.__del__()
with open('CMakeLists.txt','w') as f:
    f.writelines(cmake_lines)
