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

def update_package():
    # Declaring local variables
    package_updated = False

    proxies = get_proxies_from_wapt_console()
    if not proxies:
        proxies = get_proxies()

    url = "https://sc1.checkpoint.com/documents/Infinity_Portal/WebAdminGuides/EN/SASE-Admin-Guide/SASE_Security/Topics/introduction/supported_devices_and_operating_systems.html"

    # Getting latest version from official sources
    print("URL used is: %s" % url)
    for bs_search in bs_find_all(url, "a", "class", "xref", proxies=proxies):
        download_url = bs_search["href"]
        if download_url.endswith(".msi") and "_arm64" not in download_url:
            latest_bin = download_url.rsplit("/", 1)[-1]
            version = download_url.split("_")[-1].replace(".msi", "")
            break

    print("Latest %s version is: %s" % (control.name, version))
    print("Download url is: %s" % download_url)

    # Downloading latest binaries
    if not isfile(latest_bin):
        print("Downloading: %s" % latest_bin)
        wget(download_url, latest_bin, proxies=proxies)

    # Changing version of the package
    control.set_software_version(version)
    control.save_control_to_wapt()
    print("Changing package version to: %s in WAPT\\control" % control.version)

    # Deleting outdated binaries
    remove_outdated_binaries(version)