# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright 2016 Anaconda, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
"""Bootstrapping script."""

# Standard library imports
import optparse
import os
import os.path as osp
import sys

# Local imports
from navigator_updater.app import main

# =============================================================================
#  Parse command line
# =============================================================================
parser = optparse.OptionParser(
    usage="python bootstrap.py [options] [-- navigator_updater_options]",
    epilog="""\
Arguments for main script are specified after the --
symbol (example: `python bootstrap.py -- --foo-bar`).
"""
)
parser.add_option(
    '--gui',
    default=None,
    help="GUI toolkit: pyqt5 (for PyQt5) or pyqt (for PyQt4)"
)

options, args = parser.parse_args()

assert options.gui in (None, 'pyqt5', 'pyqt'), \
       "Invalid GUI toolkit option '{0}'".format(options.gui)

# Prepare arguments for main script
sys.argv = [sys.argv[0]] + args

# =============================================================================
#  Start execution
# =============================================================================
# Get current dir
print("Executing from source checkout")
DEVPATH = osp.dirname(osp.abspath(__file__))

# Warn if we are is located on non-ASCII path
try:
    osp.join(DEVPATH, 'test')
except UnicodeDecodeError:
    print(
        "STOP: Navigator Updater is located in the path with non-ASCII "
        "characters,"
    )
    print("      which is known to cause problems.")
    try:
        input = raw_input
    except NameError:
        pass
    input("Press Enter to continue or Ctrl-C to abort...")

# Patch sys.path
sys.path.insert(0, DEVPATH)
print("01. Added {0} to sys.path".format(DEVPATH))

# =============================================================================
#  Selecting the GUI toolkit
# =============================================================================
if options.gui is None:
    try:
        import PyQt5  # analysis:ignore
        print("02. PyQt5 is detected, selecting")
        os.environ['QT_API'] = 'pyqt5'
    except ImportError:
        try:
            import PyQt4  # analysis:ignore
            print("02. PyQt4 is detected, selecting")
            os.environ['QT_API'] = 'pyqt'
        except ImportError:
            print("02. No PyQt5 or PyQt4 detected, please install one of them")
else:
    print("02. Skipping GUI toolkit detection")
    os.environ['QT_API'] = options.gui

# =============================================================================
#  Executing Anaconda Navigator Updater
# =============================================================================
print("04. Executing the application")

main.main()
