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

app_name = "Tautulli"
app_dir = makepath(programfiles, app_name)
app_exe = makepath(app_dir, f'{app_name}.exe')

def install():
    bin_name = glob.glob('Tautulli-windows*x64.exe')[0]
    app_path = makepath(app_dir, bin_name)

    killalltasks(ensure_list(control.impacted_process))

    if isdir(app_dir) and force:
        remove_tree(app_dir)

    mkdirs(app_dir)
    filecopyto(bin_name, app_path)
    unzip_with_7zip(app_path, app_dir)

    # Delete useless folder
    remove_tree(makepath(app_dir, '$PLUGINSDIR'))
    remove_file(app_path)

    # Creating shortcuts
    create_desktop_shortcut(app_name, target=app_exe)
    create_programs_menu_shortcut(app_name, target=app_exe)


def uninstall():
    # Uninstalling software
    killalltasks(ensure_list(control.impacted_process))

    for uninstall in installed_softwares(app_name):
        uninstall_key = uninstall['key']
        run(uninstall_cmd(uninstall_key))
        wait_uninstallkey_absent(uninstall_key)

    if isdir(app_dir):
        remove_tree(app_dir)

    # Removing shortcuts
    remove_desktop_shortcut(app_exe)
    remove_programs_menu_shortcut(app_exe)
