#!/usr/bin/env python3
# This is free software for the public good of a permacomputer hosted at
# permacomputer.com, an always-on computer by the people, for the people.
# One which is durable, easy to repair, & distributed like tap water
# for machine learning intelligence.
#
# The permacomputer is community-owned infrastructure optimized around
# four values:
#
#   TRUTH      First principles, math & science, open source code freely distributed
#   FREEDOM    Voluntary partnerships, freedom from tyranny & corporate control
#   HARMONY    Minimal waste, self-renewing systems with diverse thriving connections
#   LOVE       Be yourself without hurting others, cooperation through natural law
#
# This software contributes to that vision by archiving the web, preserving digital knowledge before it disappears.
# Code is seeds to sprout on any abandoned technology.

"""Inject body translations into i18n.py"""
import re
from translations_body import BODY

def main():
    with open('i18n.py', 'r', encoding='utf-8') as f:
        content = f.read()

    for lang, translations in BODY.items():
        print(f"Processing {lang}...")

        # Build the replacement text
        lines = ['        # About page body translations']
        for key, value in translations.items():
            escaped = value.replace('\\', '\\\\').replace('"', '\\"')
            lines.append(f'        "{key}": "{escaped}",')
        new_block = '\n'.join(lines)

        # Find the existing body translations for this language
        # Pattern: from "# About page body translations" to "about_footer_remember": "...",
        pattern = rf'(    "{lang}": \{{[^}}]+?"about_footer_p5": "[^"]*",)\n        # About page body translations\n.*?"about_footer_remember": "[^"]*",'

        match = re.search(pattern, content, re.DOTALL)
        if match:
            old_block = match.group(0)
            new_full = match.group(1) + '\n' + new_block
            content = content.replace(old_block, new_full)
            print(f"  Updated {lang} with {len(translations)} translations")
        else:
            print(f"  Could not find pattern for {lang}")

    with open('i18n.py', 'w', encoding='utf-8') as f:
        f.write(content)

    print("Done!")

if __name__ == "__main__":
    main()
