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

def update_package():
    # Declaring local variables
    package_updated = False
    proxies = get_proxies()
    if not proxies:
        proxies = get_proxies_from_wapt_console()

    # Récupération des informations produits
    url_api = "https://download.scenari.software/SCENARIclient/?productInfos="

    # Récuperer les dernieres versions à partir des sources d'origines
    print("API used is: %s" % url_api)
    json_load = json.loads(wgets(url_api, proxies=proxies))

    for download in json_load["ch"]:
        # Conditions pour rechercher le binaire, correspond à windows/fr/.exe
        if (download.get("prc") == "dlProduct" and download.get("metas", {}).get("os") == "windows" and download.get("metas", {}).get("lang") == "fr-FR" and download.get("n", "").endswith(".exe")):
            url_dl = "https://download.scenari.software" + download["metas"]["path"]
            filename = download["n"]
            version = download["n"].split("client")[1].split("_")[0]
            break

    print("Version : %s" % version)
    print("Filename : %s" % filename)
    print("URL : %s" % url_dl)

    if not isfile(filename):
        package_updated = True
        wget(url_dl,filename,proxies=proxies)

    # Nettoyer les fichiers temporaires
    for f in glob.glob('*.exe'):
        if f != filename:
            remove_file(f)

    control.set_software_version(version)
    control.save_control_to_wapt()