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


def install():

    # Uninstalling older versions of the software that can remain
    for to_uninstall in installed_softwares(name="dnGrep"):
      if (Version(to_uninstall["version"]) < Version(control.get_software_version()) or 
          control.architecture.upper() not in to_uninstall['name']):
        print(f"Removing: {to_uninstall['name']} ({to_uninstall['version']})")
        killalltasks(ensure_list(control.impacted_process))
        run(uninstall_cmd(to_uninstall["key"]))
        wait_uninstallkey_absent(to_uninstall["key"])

    bin_name = glob.glob(f"dnGREP.*.{control.architecture}.msi")[0]
    install_msi_if_needed(
       bin_name, 
       min_version=control.get_software_version(),
       name=f"dnGrep {Version(control.get_software_version(), 3)} \({control.architecture.upper()}\)"
    )
