#! /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 stat


# 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.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 = "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 !"""
    from geninstaller.helpers import autoinstall
    from geninstaller import core
    autoinstall()
    # and then installs your application
    core.install(datas)


def check_pip():
    """Checks if pip is installed"""
    res = os.popen("python3 -m pip -V").read().strip()
    if res.startswith('pip'):
        return True
    else:
        return False


def set_executable(file) -> None:
    """set a file executable"""
    st = os.stat(file)
    os.chmod(file, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)


def main():

    this_file = os.path.basename(__file__)
    if not check_pip():
        print(
            "pip is not installed for your main version of python3.\n"
            "Please install pip first, and then, restart your session "
            "before executing './{this_file}' again.\n\n"
            "- DEBIAN (Ubuntu, Linux Mint, etc...):\n"
            "apt install python3-pip\n"
            "- ARCH LINUX:\n"
            "sudo pacman -S python-pip\n"
            "- OPENSUSE:\n"
            "zypper install python3-pip\n"
            "\n"
            )
        return

    import pkg_resources

    dependencies = [
        'geninstaller>=1.0.7',
    ]

    try:
        pkg_resources.require(dependencies)
    except pkg_resources.DistributionNotFound:
        os.system('python3 -m pip install --upgrade geninstaller')

    from flamewok.cli import cli

    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"),
    ]

    cli.route(*routes)


if __name__ == "__main__":
    main()
