
# -*- coding: utf-8 -*-
from setuphelpers import *
from setupdevhelpers import *
import glob
import json


def update_package():
    # Declaring local variables
    package_updated = False
    
    proxies = get_proxies_from_wapt_console()
    if not proxies:
        proxies = get_proxies()
  
    # url_base = "https://alfaview.com/en/download/"
    url_msi = "https://assets.alfaview.com/stable/win/msi_version.info"

    json_file = json.loads(wgets(url_msi, proxies = proxies))

    for data in json_file["versions"]:
        latest_bin = data["download_url"]
        download_url = "https://assets.alfaview.com/stable/win/" + latest_bin
        version = data["version"]
        break

    # Downloading latest binaries
    print("Download URL is: %s" % download_url)
    if not isfile(latest_bin):
        print("Downloading: %s" % latest_bin)
        wget(download_url, latest_bin, proxies=proxies)
        package_updated = True
    else:
        print("Binary is present: %s" % latest_bin)

    version = get_version_from_binary(latest_bin)
    # Mettre à jour le package
    control.set_software_version(version)
    control.save_control_to_wapt()    
        

