# -*- coding: utf-8 -*-
from setuphelpers import *


uninstallkey = []

# Defining variables
bin_name_sub = "Zotero-%s_setup.exe"
silent_args = "-ms"
app_uninstallkey_sub = "Zotero %s (x86 en-US)"


def install():
    # Initializing variables
    package_version = control.get_software_version()
    bin_name = bin_name_sub % package_version
    app_uninstallkey = app_uninstallkey_sub % package_version

    # Installing the package
    install_exe_if_needed(
        bin_name,
        silentflags=silent_args,
        key=app_uninstallkey,
        min_version=package_version,
    )

    # Disable Zotero auto update
    profiles_path = makepath(user_appdata, "Zotero", "Zotero", "Profiles", "*")

    for profile in glob.glob(profiles_path):
        if not os.path.isdir(profile):
            continue

        user_js = os.path.join(profile, "user.js")

        if os.path.exists(user_js):
            print(f"SKIP (existe déjà): {user_js}")
            continue

        content = 'user_pref("app.update.auto", false);\n'

        try:
            with open(user_js, "w", encoding="utf-8") as f:
                f.write(content)
            print(f"CREATED: {user_js}")
        except Exception as e:
            print(f"ERROR: {user_js} -> {e}")