#! /usr/bin/env python3
# coding: utf-8

"""
This fast installer is destined to the developpers who want to create
their own quick installer, with 'geninstaller' (https://github.com/byoso/geninstaller).
Just read the comments and complete the informations asked below,
the magic will do the rest.

IMPORTANT:
Geninstaller only installs applications in the user's space, it does not
access to the system files. Therefore, each installation is relative
to one user of the computer. It is possible to install the application
on multiple sessions, but each application installed will remain totally
independent.

"""

# =========== THERE WE GO ===================================

# PLACE THIS FILE IN THE ROOT DIRECTORY OF YOUR PROJECT

# =====================================================================
# ======= Complete this informations ==================================

# choose a good name, do NOT use underscores here
NAME = "fake"
# short description (optionnal), a few words would be fine
DESCRIPTION = "A short description"
# The main executable file of the application
# The path is relative to this directory (BASE_DIR defined below)
EXECUTABLE = "fake/fake.py"
# The icon that your system will use (optionnal, but so much better)
# The path is relative to this directory (BASE_DIR defined below)
ICON = "fake/icon.png"
# Does your application needs to appear within a terminal ?
# (usually, a graphical application doesn't need a terminal)
TERMINAL = False

# Just uncomment the categories in which you want your app to appear in (optionnal).
# If you need to know more (expert), read this:
# freedesktop.org doc for categories
# https://specifications.freedesktop.org/menu-spec/latest/apa.html#main-category-registry
# additionnal categories
# https://specifications.freedesktop.org/menu-spec/latest/apas02.html
CATEGORIES = [
    # "AudioVideo",
    # "Audio",
    # "Video",
    # "Development",
    # "Education",
    # "Game",
    # "Graphics",
    # "Network",
    # "Office",
    # "Science",
    # "Settings",
    # "System",
    # "Utility",
]

# if your app needs python dependencies, list here the requirements files
# your program will be installed in a virtual environment with these dependencies
python_dependencies = []

# ADDITIONAL OPTIONS
exec_options = ""  # if the exec needs additional options

# if more parameters are needed in the .desktop file, fill this list
# with the lines you want. e.g: ["foo=bar", "truc=machin"]
options = [
    "Keywords<eq>geninstaller;",  # use <eq> instead of = to avoid issues with bash
]

# optionnal pre installation/uninstallation script files
pre_install = ""
post_install = ""
pre_uninstall = ""
post_uninstall = ""

# you're done ! :)

# = do not touch what is following unless you know what you're doing ==
# =====================================================================


import os
import subprocess


# geninstaller version check
############################

geninstaller_min_required_version = "2.0.6"

def version_from_string(v: str) -> tuple:
    return tuple(map(int, (v.split("."))))

def required_version(v1: str, min: str, max: str) -> bool:
    """Compares two version strings.
    Returns True if min <= v1 < max, else False."""
    current_version = version_from_string(v1)
    min_version = version_from_string(min)
    max_version = version_from_string(max)
    return current_version >= min_version and current_version < max_version

installed_pipx = subprocess.run(
    ["pipx", "list", "--short"],
    capture_output=True,
    text=True,
    check=False
)
installed_list = installed_pipx.stdout.strip().splitlines()
installed_tools = {i: j for i, j in (item.split() for item in installed_list)}
install_geninstaller_message = f"""
!! ABORTED !!
Geninstaller >= {geninstaller_min_required_version} must be installed with pipx before running this installer:

# INSTALLING pipx:
Ubuntu / Debian		        sudo apt install pipx or pip install --user pipx
Fedora / RHEL / CentOS		sudo dnf install pipx
Arch / Manjaro		        sudo pacman -S pipx
openSUSE		            sudo zypper install pipx

# INSTALLING geninstaller with pipx:
$ pipx install geninstaller
"""
if not "geninstaller" in installed_tools or not required_version(installed_tools["geninstaller"], geninstaller_min_required_version, "3.0.0"):
    print(install_geninstaller_message)
    exit(0)


# Installation with geninstaller
################################

this_file = os.path.basename(__file__)


BASE_DIR = os.path.abspath(os.path.dirname(__file__))

def install() -> None:
    """installs the application with geninstaller"""
    # building the query parameters
    query_params = f'?name="{NAME}"+exec="{EXECUTABLE}"+description="{DESCRIPTION}"+' \
        f'terminal="{TERMINAL}"+icon="{ICON}"+categories="{";".join(CATEGORIES)}"+' \
        f'base_dir="{BASE_DIR}"+exec_options="{exec_options}"+options="{";".join(options)}"+' \
        f'pre_install_file="{pre_install}"+post_install_file="{post_install}"+' \
        f'pre_uninstall_file="{pre_uninstall}"+post_uninstall_file="{post_uninstall}"+' \
        f'python_dependencies="{";".join(python_dependencies)}"'
    try:
        subprocess.run(
            ["geninstaller", "_install", query_params],  # prod
            # ["python", "-m", "geninstaller.cmd", "_install", query_params],  # dev
            check=True
        )
    except Exception as e:
        print(f"Installation failed: {e}")
        exit(1)

def main() -> None:
    install()


if __name__ == "__main__":
    main()
