#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright 2016 Continuum Analytics, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
"""Application entry point."""

# Standard library imports
import os
import shutil
import sys

# Local imports
from anaconda_navigator import __version__
from anaconda_navigator.app.cli import parse_arguments
from anaconda_navigator.exceptions import exception_handler


def main():  # pragma: no cover
    """Main application entry point."""
    # Parse CLI arguments
    options = parse_arguments()

    # Return information on version
    if options.version:
        print(__version__)
        sys.exit(0)

    # Reset Navigator conifg
    if options.reset:
        print('\nAnaconda Navigator configuration reset...\n\n')
        from anaconda_navigator.config import CONF_PATH
        if os.path.isdir(CONF_PATH):
            try:
                shutil.rmtree(CONF_PATH)
                print('Anaconda Navigator configuration reset successful!\n')
                sys.exit(0)
            except Exception as e:
                print('Anaconda Navigator configuration reset failed!!!\n')
                print(e)
                sys.exit(1)

    if options.removelock:
        print('\nRemoving Anaconda Navigator lock...\n\n')
        from anaconda_navigator.config import LOCKFILE
        try:
            os.remove(LOCKFILE)
            print('Anaconda Navigator lock removal successful!\n')
            sys.exit(0)
        except Exception as e:
            print('Anaconda Navigator lock removal failed!!!\n')
            print(e)
            sys.exit(1)

    # Clean old style logs
    from anaconda_navigator.utils.logs import clean_logs
    clean_logs()

    # Import app
    from anaconda_navigator.app.start import start_app
    return exception_handler(start_app, options)


if __name__ == '__main__':  # pragma: no cover
    main()
