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

app_name = "Process Monitor"
app_dir = makepath(programfiles32, app_name)

def get_app_path():
    if isARM64():
        app_file = "Procmon64a.exe"
    elif iswin64():
        app_file = "Procmon64.exe"
    else:
        app_file = "Procmon.exe"
    return makepath(app_dir, app_file)

def install():
    # Declaring local variables
    package_version = control.get_software_version()
    bin_name = glob.glob("ProcessMonitor.zip")[0]

    app_path = get_app_path()

    # Getting installed software version
    if isfile(app_path):
        installed_version = get_version_from_binary(app_path)
    else:
        installed_version = None

    print("Installing: %s" % app_name)
    if installed_version is None or Version(installed_version) < Version(package_version) or force:
        killalltasks(ensure_list(control.impacted_process))
        if isdir(app_dir):
            remove_tree(app_dir)
        mkdirs(app_dir)
        print("Extracting: %s to: %s" % (bin_name, app_dir))
        unzip(bin_name, app_dir)

        # Adding software to "list-registry"
        register_windows_uninstall(control, win64app=False)
    else:
        print("%s already installed. Skipping" % app_name)

    # Creating custom shortcuts
    create_desktop_shortcut(app_name, app_path)
    create_programs_menu_shortcut(app_name, app_path)


def uninstall():
    # Uninstalling software
    killalltasks(ensure_list(control.impacted_process))
    if isdir(app_dir):
        remove_tree(app_dir)
    unregister_uninstall(app_name)

    # Removing shortcuts
    remove_desktop_shortcut(app_name)
    remove_programs_menu_shortcut(app_name)


def audit():
    # Declaring local variables
    package_version = control.get_software_version()
    app_path = get_app_path()

    # Getting installed software version
    if isfile(app_path):
        installed_version = get_version_from_binary(app_path)
    else:
        installed_version = None

    # Auditing software
    if installed_version is None:
        print("%s is not installed" % app_name)
        return "ERROR"
    elif Version(installed_version) != Version(package_version):
        print("%s is installed in version (%s) instead of (%s)" % (app_name, installed_version, package_version))
        return "WARNING"
    else:
        print("%s (%s) is installed" % (app_name, installed_version))
        return "OK"