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

dict_loc_file = {"en": "", "de": "d", "all": ""}


def update_package():
    proxies = {}
    if isfile(makepath(application_data(), "waptconsole", "waptconsole.ini")):
        proxywapt = inifile_readstring(makepath(user_local_appdata(), "waptconsole", "waptconsole.ini"), "global", "http_proxy")
        if proxywapt:
            proxies = {"http": proxywapt, "https": proxywapt}
    winrarversion = wgets("https://www.win-rar.com/postdownload.html", proxies=proxies).split("winrar-x64-")[1].split(".exe")[0]
    reallang = control.locale
    if reallang in dict_loc_file:
        reallang = dict_loc_file[control.locale]
    if control.architecture == "x64":
        filename = "winrar-x64-%s%s.exe" % (winrarversion, reallang)
        urldl = "https://www.rarlab.com/rar/%s" % filename
    else:
        filename = "wrar%s%s.exe" % (winrarversion, reallang)
        urldl = "https://www.win-rar.com/fileadmin/winrar-versions/winrar/%s" % filename
    if not isfile(filename):
        wget(urldl, filename, proxies=proxies)
    for exe in glob.glob("*.exe"):
        if exe != filename:
            remove_file(exe)
    control.version = "%s-0" % (get_file_properties(filename)["ProductVersion"])
    control.save_control_to_wapt(".")