#!/usr/bin/env python3

data = {
    "forgejo_user"            : self.ssh_user,
    "forgejo_group"           : self.ssh_group,
    "forgejo_port"            : str(self.forgejo_port),
    "forgejo_bin"             : bin_path,
    "forgejo_app_ini"         : conf_path,
    "forgejo_work_path"       : forgejo_work_dir_path,
    "forgejo_data_path"       : forgejo_data_dir_path,
    "forgejo_db_path"         : forgejo_db_path,
    "forgejo_repository_root" : git_dir_path,
    "forgejo_lfs_path"        : lfs_dir_path,
    "forgejo_log_path"        : forgejo_log_dir_path,
    "forgejo_hostname"        : _runcmd("hostname"),
    "forgejo_lfs_jwt_secret"  : _runcmd([bin_path,"generate", "secret", "LFS_JWT_SECRET"]),
    "forgejo_jwt_secret"      : _runcmd([bin_path,"generate", "secret", "JWT_SECRET"]),
    "forgejo_internal_token"  : _runcmd([bin_path,"generate", "secret", "INTERNAL_TOKEN"]),
    "forgejo_secret_key"      : _runcmd([bin_path,"generate", "secret", "SECRET_KEY"])
}

ini_template = "/usr/share/opengnsys-forgejo/forgejo-app.ini"


def _install_template(self, template, destination, keysvalues):
    data = ""
    with open(template, "r", encoding="utf-8") as template_file:
        data = template_file.read()

    for key in keysvalues.keys():
        if isinstance(keysvalues[key], int):
            data = data.replace("{" + key + "}", str(keysvalues[key]))
        else:
            data = data.replace("{" + key + "}", keysvalues[key])

    with open(destination, "w+", encoding="utf-8") as out_file:
        out_file.write(data)




_install_template(os.path.join(self.script_path, "forgejo-app.ini"), conf_path, data)

