import os

# Base content from strongswan_profile_check_inc_en.php
base_path = '/var/www/html/mylib4ikev2svr/strongswan_profile_check_inc_language/strongswan_profile_check_inc_en.php'
with open(base_path, 'r') as f:
    base_content = f.read()

# Helper to provide translations
translations = {
    "Notice": {
        "ko": "알림",
        "ja": "通知",
        "ru": "Уведомление",
        "zh-cn": "通知",
        "zh-tw": "通知",
        "tr": "Bildirim",
        "vi": "Thông báo",
        "es": "Aviso",
        "pt": "Aviso",
        "it": "Avviso",
        "de": "Hinweis",
        "fr": "Avis",
        "ar": "إشعار",
        "pl": "Powiadomienie",
        "uk": "Повідомлення",
        "fa": "اعلان",
        "hi": "सूचना",
        "bn": "বিজ্ঞপ্তি",
        "ms": "Notis"
    },
    "Your ID has been banned.<br>Date: ": {
        "ko": "귀하의 아이디는 차단되었습니다.<br>일시: ",
        "ja": "あなたのIDは禁止されました。<br>日付: ",
        "ru": "Ваш ID был заблокирован.<br>Дата: ",
        "zh-cn": "您的ID已被禁止。<br>日期: ",
        "zh-tw": "您的ID已被禁止。<br>日期: ",
        "tr": "Kimliğiniz yasaklandı.<br>Tarih: ",
        "vi": "ID của bạn đã bị cấm.<br>Ngày: ",
        "es": "Su ID ha sido prohibida.<br>Fecha: ",
        "pt": "Seu ID foi banido.<br>Data: ",
        "it": "Il tuo ID è stato bannato.<br>Data: ",
        "de": "Ihre ID wurde gesperrt.<br>Datum: ",
        "fr": "Votre ID a été banni.<br>Date: ",
        "ar": "تم حظر معرفك.<br>التاريخ: ",
        "pl": "Twój identyfikator został zablokowany.<br>Data: ",
        "uk": "Ваш ID заблоковано.<br>Дата: ",
        "fa": "شناسه شما مسدود شده است.<br>تاریخ: ",
        "hi": "Ihre ID wurde gesperrt.<br>Datum: ",
        "bn": "আপনার আইডি নিষিদ্ধ করা হয়েছে।<br>তারিখ: ",
        "ms": "ID anda telah diharamkan.<br>Tarikh: "
    },
    "Your ID has been withdrawn and cannot be accessed.<br>Date: ": {
        "ko": "귀하의 아이디는 탈퇴 처리되어 접속할 수 없습니다.<br>일시: ",
        "ja": "あなたのIDは退会処理され、アクセスできません。<br>日付: ",
        "ru": "Ваш ID был удален и доступ к нему невозможен.<br>Дата: ",
        "zh-cn": "您的ID已被撤销，无法访问。<br>日期: ",
        "zh-tw": "您的ID已被撤銷，無法訪問。<br>日期: ",
        "tr": "Kimliğiniz geri çekildi ve erişilemez.<br>Tarih: ",
        "vi": "ID của bạn đã bị rút lại và không thể truy cập.<br>Ngày: ",
        "es": "Su ID ha sido retirada y no se puede acceder.<br>Fecha: ",
        "pt": "Seu ID foi retirado e não pode ser acessado.<br>Data: ",
        "it": "Il tuo ID è stato ritirato e non è possibile accedervi.<br>Data: ",
        "de": "Ihre ID wurde zurückgezogen und ist nicht mehr zugänglich.<br>Datum: ",
        "fr": "Votre ID a été retiré et n'est plus accessible.<br>Date: ",
        "ar": "تم سحب معرفك ولا يمكن الوصول إليه.<br>التاريخ: ",
        "pl": "Twój identyfikator został wycofany i nie można uzyskać do niego dostępu.<br>Data: ",
        "uk": "Ваш ID відкликано, доступ до нього неможливий.<br>Дата: ",
        "fa": "شناسه شما پس گرفته شده است و قابل دسترسی نیست.<br>تاریخ: ",
        "hi": "आपकी आईडी वापस ले ली गई है और उस तक पहुँचा नहीं जा सकता है।<br>दिनांक: ",
        "bn": "আপনার আইডি প্রত্যাহার করা হয়েছে এবং অ্যাক্সেস করা যাবে না।<br>তারিখ: ",
        "ms": "ID anda telah ditarik balik dan tidak boleh diakses.<br>Tarikh: "
    },
    " Available until": {
        "ko": " 사용 가능 기한",
        "ja": " 有効期限",
        "ru": " Доступно до",
        "zh-cn": " 有效期至",
        "zh-tw": " 有效期至",
        "tr": " Kadar geçerli",
        "vi": " Có sẵn cho đến",
        "es": " Disponible hasta",
        "pt": " Disponível até",
        "it": " Disponibile fino a",
        "de": " Verfügbar bis",
        "fr": " Disponible jusqu'au",
        "ar": " متاح حتى",
        "pl": " Dostępne do",
        "uk": " Доступно до",
        "fa": " در دسترس تا",
        "hi": " तक उपलब्ध",
        "bn": " পর্যন্ত উপলব্ধ",
        "ms": " Tersedia sehingga"
    },
    " Local Time": {
        "ko": " 현지 시간",
        "ja": " 現地時間",
        "ru": " Местное время",
        "zh-cn": " 当地时间",
        "zh-tw": " 當地時間",
        "tr": " Yerel Saat",
        "vi": " Giờ địa phương",
        "es": " Hora local",
        "pt": " Hora local",
        "it": " Ora locale",
        "de": " Ortszeit",
        "fr": " Heure locale",
        "ar": " بالتوقيت المحلي",
        "pl": " Czas lokalny",
        "uk": " Місцевий час",
        "fa": " زمان محلی",
        "hi": " स्थानीय समय",
        "bn": " স্থানীয় সময়",
        "ms": " Masa Tempatan"
    }
}

# Mapping of android res folders to language codes in the dictionary
res_mapping = {
    "ru": "ru",
    "ja": "ja",
    "ko": "ko",
    "zh-cn": "zh-cn",
    "zh-tw": "zh-tw",
    "tr": "tr",
    "vi": "vi",
    "es": "es",
    "pt": "pt",
    "it": "it",
    "de": "de",
    "fr": "fr",
    "ar": "ar",
    "pl": "pl",
    "uk": "uk",
    "fa": "fa",
    "hi": "hi",
    "bn": "bn",
    "ms": "ms"
}

file_mapping = {
    "ru": ["strongswan_profile_check_inc_ru.php"],
    "ja": ["strongswan_profile_check_inc_ja.php"],
    "ko": ["strongswan_profile_check_inc_ko.php"],
    "zh-cn": ["strongswan_profile_check_inc_zh-cn.php"],
    "zh-tw": ["strongswan_profile_check_inc_zh-tw.php"],
    "tr": ["strongswan_profile_check_inc_tr.php"],
    "vi": ["strongswan_profile_check_inc_vi.php"],
    "es": ["strongswan_profile_check_inc_es-es.php"],
    "pt": ["strongswan_profile_check_inc_pt-br.php", "strongswan_profile_check_inc_pt-pt.php"],
    "it": ["strongswan_profile_check_inc_it.php"],
    "de": ["strongswan_profile_check_inc_de.php"],
    "fr": ["strongswan_profile_check_inc_fr.php"],
    "ar": ["strongswan_profile_check_inc_ar.php"],
    "pl": ["strongswan_profile_check_inc_pl.php"],
    "uk": ["strongswan_profile_check_inc_uk.php"],
    "fa": ["strongswan_profile_check_inc_fa.php"],
    "hi": ["strongswan_profile_check_inc_hi.php"],
    "bn": ["strongswan_profile_check_inc_bn.php"],
    "ms": ["strongswan_profile_check_inc_ms.php"]
}

# Header language name mapping
header_lang_map = {
    "ru": "Russian ru",
    "ja": "Japanese ja",
    "ko": "Korean ko",
    "zh-cn": "Chinese Simplified zh-cn",
    "zh-tw": "Chinese Traditional zh-tw",
    "tr": "Turkish tr",
    "vi": "Vietnamese vi",
    "es": "Spanish es",
    "pt": "Portuguese pt",
    "it": "Italian it",
    "de": "German de",
    "fr": "French fr",
    "ar": "Arabic ar",
    "pl": "Polish pl",
    "uk": "Ukrainian uk",
    "fa": "Persian fa",
    "hi": "Hindi hi",
    "bn": "Bengali bn",
    "ms": "Malay ms"
}

base_dir = '/var/www/html/mylib4ikev2svr/strongswan_profile_check_inc_language/'

for lang_code, file_names in file_mapping.items():
    if lang_code not in res_mapping:
         continue

    trans_code = res_mapping[lang_code]

    new_content = base_content

    # Replace strings
    for en_string, lang_dict in translations.items():
        if trans_code in lang_dict:
            new_content = new_content.replace(en_string, lang_dict[trans_code])

    # Header replacement
    if lang_code in header_lang_map:
        new_content = new_content.replace("// English en", f"// {header_lang_map[lang_code]}")

    for fname in file_names:
        full_path = os.path.join(base_dir, fname)

        # update filename in header (minor detail)
        new_content_final = new_content.replace("strongswan_profile_check_inc_en.php", fname)

        # Extract specific language code from filename for $current_lang variable
        # Filename format: strongswan_profile_check_inc_CODE.php
        prefix = "strongswan_profile_check_inc_"
        suffix = ".php"
        if fname.startswith(prefix) and fname.endswith(suffix):
            specific_lang_code = fname[len(prefix):-len(suffix)]
            # Replace the hardcoded english language variable with the specific one
            new_content_final = new_content_final.replace("$current_lang = 'en';", f"$current_lang = '{specific_lang_code}';")

        with open(full_path, 'w') as f:
            f.write(new_content_final)
        print(f"Updated {fname}")
