# Copyright (c) 2010 ActiveState Software Inc. All rights reserved.

from __future__ import print_function

# X.Y.Z (X.Y is compat version). each compat version separates the local 
# repo index area and other files ... eg: ~/Library/Caches/PyPM/1.1/...
__version_info__ = ('1', '3', '4')
__version__ = '.'.join(__version_info__) # + '.dev'

# Set the default logging level for global module. It is unfortunate that
# logging module has ERROR/WARN only has the default levels. All loggers
# (pypm.**) will inherit this level unless explicitly overrided elsewhere.
import logging
logging.getLogger('pypm').setLevel(logging.DEBUG) # DEBUG and above levels

from os import path
import sys


# We depend on distribute; if setuptools is loaded somewhere in sys.path,
# we are screwed (for we rely on several features/bug-fixes in distribute)
import setuptools
if not getattr(setuptools, '_distribute', False):
    import textwrap
    from glob import glob

    pyver = '%d.%d' % sys.version_info[:2]

    # setuptools_egg: this is the egg file or simply a package
    # directory that contains the setuptools files; the user will be
    # asked to remove this directory/egg.
    pkgdir = path.dirname(setuptools.__file__)
    if '.egg' in pkgdir:
        setuptools_egg = pkgdir.split('.egg', 1)[0] + '.egg'
    else:
        setuptools_egg = pkgdir # just remove the package dir

    rename_cmd = 'ren' if sys.platform == 'win32' else 'mv'
    
    paragraphs = [
        """error: incompatible "setuptools" package found at "%s".""" % \
            setuptools.__file__,

        "",
        "* * *",

        """To install packages properly, PyPM requires the Distribute
package. Distribute is a fork of the common "setuptools" package
developed by the Python community to fix various bugs. It is fully
backward compatible with "setuptools". ActiveState recommends that all
users switch from setuptools to Distribute.""",

        "",

        """This ActivePython installation already includes
Distribute. However there is a conflicting "setuptools" installation
in the way. Most likely this is from having installed ActivePython
over a pre-existing Python site-packages area. To use PyPM you must
remove (or rename) the setuptools ".egg" directory. You can run the
following to do that, then re-run your pypm command:""",

        "",
        '  {0} "{1}" "{1}.bak"'.format(rename_cmd, setuptools_egg),
        "",

        """See the ActivePython FAQ for more information:""",

        "http://docs.activestate.com/activepython/%s/faq.html#pypm" % pyver,

        "* * *",
        ]

    for para in paragraphs:
        if para == "":
            print(file=sys.stderr) # newline
        else:
            for line in textwrap.wrap(para):
                print(line, file=sys.stderr)

    # XXX: not API friendly
    raise SystemExit(6)


# Add path to dependent libraries
external_dir = path.abspath(path.join(path.dirname(__file__), 'external'))
external_dir_pyX = path.join(external_dir, '%d' % sys.version_info[0])
assert path.exists(external_dir), 'missing: %s' % external_dir
assert path.exists(external_dir_pyX), 'missing: %s' % external_dir_pyX
sys.path.insert(0, external_dir)
sys.path.insert(0, external_dir_pyX)


# expand six.moves
import pypm.common.backport 


# Convenient function for API use; see ``pypm.client.command.main``
def cmd(argv):
    from pypm.client.command import main
    return main(argv)
