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

"""
This fast installer is destined to the developpers who want to create
their own quick installer, in the 'geninstaller' way.
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.

"""

import os
import pkg_resources

# If your application have pip dependencies, you can add them here
# they will be installed automatically, but do not remove
# the ones already included
dependencies = [
    'geninstaller>=1.0.0',
]
clean_up = False
try:
    pkg_resources.require(dependencies)
except pkg_resources.DistributionNotFound:
    os.system('pip install --upgrade geninstaller')
    clean_up = True

from flamewok.cli import cli
from flamewok.helpers import clear
from geninstaller import core
from geninstaller.helpers import autoinstall



# PLACE THIS FILE IN THE ROOT DIRECTORY OF YOUR PROJECT

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

# choose a good name, do NOT use underscores here
NAME = "A good name"
# 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

# uncomment the categories in which you want your app to appear in (optionnal).
# if needed, 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",
]

# you're done ! :)

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

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

datas = {
    "name": NAME,
    "exec": EXECUTABLE,
    "comment": DESCRIPTION,
    "terminal": TERMINAL,
    "icon": ICON,
    "categories": CATEGORIES,
    "base_dir": BASE_DIR,
}


def install():
    """installs geninstaller's database on the system,
     if not already installed. Absolutely required !"""
    autoinstall()
    # and then installs your application
    core.install(datas)


infos = (
    "ABOUT GENINSTALLER\n"
    "| This installer works with geninstaller. \n"
    "| To know more about geninstaller and its options, type in console: \n"
    "|      $ geninstaller -h"
)

if __name__ == "__main__":
    if clean_up:
        clear()
    this_file = os.path.basename(__file__)
    routes = [
        f"installer program for: {NAME}",
        "INSTALLATION",
        ("", install, (
            f"Install '{NAME}' by simply executing '$ ./{this_file}'"
            )),

        "HELP",
        ("-h", cli.help, "\n|    display this help"),
        ("--help", cli.help, "\n|    display this help"),
        infos,
    ]

    cli.route(*routes)
