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

'''{
   "key":"Zotero 7.0.1 (x64 en-US)",
   "name":"Zotero",
   "version":"7.0.1",
   "install_date":"",
   "install_location":"C:\\Program Files\\Zotero",
   "uninstall_string":"C:\\Program Files\\Zotero\\uninstall\\helper.exe",
   "publisher":"Corporation for Digital Scholarship",
   "system_component":0,
   "win64":true
  }'''

def install():
    # Initializing variables
    bin_name = glob.glob('Zotero-*_x64_setup.exe')[0]
    version = bin_name.split('_')[0].rsplit('-')[-1]

    # Constructing the registry key
    reg_key = f"Zotero {version} (x64 en-US)"

    # Installing the package

    install_exe_if_needed(
        bin_name,
        silentflags='/s /allusers',
        key= reg_key,
        min_version=control.get_software_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}")