#!/usr/bin/env python3
"""Génère le script de fond (« Scripts - Background », portée globale) qui installe dans une
instance ServiceNow l'intégration catalogue de PKIFactor. Idempotent, aucun secret.

    python3 installer_servicenow.py --base-url https://clm.example.com/api/integrations/servicenow \\
        --mid-server MON_MID --role x_<id>_pkifac_0.api_user > installer.js

La clé d'appels entrants (pkf_sn_...) se pose ENSUITE dans l'identifiant « PKIFactor - inbound API key
» (Connections & Credentials > Credentials), jamais dans un script.
"""
import argparse
import json
import os

ICI = os.path.dirname(os.path.abspath(__file__))

LIBELLES = {
    "en": {
        "category": "Certificates (PKIFactor)",
        "category_desc": "Request, renew or revoke a certificate managed by PKIFactor",
        "request": "Request a certificate",
        "request_desc": "New certificate issued by PKIFactor, from the profiles allowed for the person",
        "renew": "Renew a certificate", "renew_desc": "Renewal of a PKIFactor certificate (new CSR)",
        "revoke": "Revoke a certificate", "revoke_desc": "Revocation of a PKIFactor certificate",
        "for": "Requested for", "profile": "Certificate profile", "cn": "Common name (CN)",
        "san": "Subject alternative names (SAN), comma-separated", "csr": "Certificate signing request (CSR, PEM)",
        "id": "Certificate number in PKIFactor", "new_csr": "New certificate signing request (CSR, PEM)",
        "reason": "Revocation reason",
    },
    "fr": {
        "category": "Certificats (PKIFactor)",
        "category_desc": "Demander, renouveler ou révoquer un certificat géré par PKIFactor",
        "request": "Demander un certificat",
        "request_desc": "Nouveau certificat émis par PKIFactor, selon les profils autorisés pour la personne",
        "renew": "Renouveler un certificat", "renew_desc": "Renouvellement d'un certificat PKIFactor (nouvelle CSR)",
        "revoke": "Révoquer un certificat", "revoke_desc": "Révocation d'un certificat PKIFactor",
        "for": "Pour qui", "profile": "Profil de certificat", "cn": "Nom commun (CN)",
        "san": "Noms alternatifs (SAN), séparés par des virgules", "csr": "Demande de signature (CSR, PEM)",
        "id": "Numéro du certificat dans PKIFactor", "new_csr": "Nouvelle demande de signature (CSR, PEM)",
        "reason": "Motif de la révocation",
    },
}


def lire(nom):
    with open(os.path.join(ICI, nom), encoding="utf-8") as fh:
        return fh.read()


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--base-url", required=True)
    ap.add_argument("--mid-server", default="")
    ap.add_argument("--role", default="x_2273904_pkifac_0.api_user")
    ap.add_argument("--language", choices=("en", "fr"), default="en",
                    help="langue des articles, variables et notes de ticket")
    a = ap.parse_args()
    api = lire("api_retour_ticket.js").replace("x_2273904_pkifac_0.api_user", a.role)
    print("var CFG = " + json.dumps({
        "base_url": a.base_url, "mid": a.mid_server,
        "si": lire("PKIFactorClient.js"), "br": lire("regle_metier_catalogue.js"),
        "cs": lire("client_profils.js"), "api": api,
        "job": lire("tache_reprise.js"), "lang": a.language, "L": LIBELLES[a.language]}, ensure_ascii=False) + ";")
    print(lire("installer_servicenow.js"))


if __name__ == "__main__":
    main()
