#!/usr/bin/env python3

"""
** Allows to uninstall 'raisin'. **

This does not uninstall the module as such but
removes all traces generated by ``raisin.application.install``.
"""

import os

from context_verbose import printer as ctp

from raisin.application.location import DIR_PATH, CONFIG_PATH


def _del_raisin_dir():
    """
    ** Delete the main directory if it is empty. **
    """
    if not os.listdir(DIR_PATH):
        os.removedirs(DIR_PATH)
        ctp.print(f'the {DIR_PATH} directory has just been deleted')

def uninstall_config_file():
    """
    ** Deletes the configuration file. **

    Symmetric function of ``raisin.application.install.install_config_file``.
    """
    if os.path.exists(CONFIG_PATH):
        os.remove(CONFIG_PATH)
        ctp.print(f'the {CONFIG_PATH} file has just been deleted')
    _del_raisin_dir()

def uninstall_app(*, config_only):
    """
    ** Entry point to uninstall everything. **

    Parameters
    ----------
    config_only : boolean, default=False
        If True, call only the ``uninstall_config_file`` function.
    """
    with ctp('Application uninstallation...'):
        if config_only:
            ctp.print('uninstalls only the configuration file')
            uninstall_config_file()
        else:
            ctp.print('uninstalls all')
            uninstall_config_file()
