import shutil
import sys
import os
from setuptools import setup, find_packages, __version__
from pip.req import parse_requirements
import data


v = sys.version_info
if sys.version_info < (3, 5):
    msg = "FAIL: Requires Python 3.5 or later, " \
          "but setup.py was run using {}.{}.{}"
    v = sys.version_info
    print(msg.format(v.major, v.minor, v.micro))
    # noinspection PyPackageRequirements
    print("NOTE: Installation failed. Run setup.py using python3")
    sys.exit(1)

# Change to ioflo's source directory prior to running any command
try:
    SETUP_DIRNAME = os.path.dirname(__file__)
except NameError:
    # We're probably being frozen, and __file__ triggered this NameError
    # Work around this
    SETUP_DIRNAME = os.path.dirname(sys.argv[0])

if SETUP_DIRNAME != '':
    os.chdir(SETUP_DIRNAME)

SETUP_DIRNAME = os.path.abspath(SETUP_DIRNAME)

METADATA = os.path.join(SETUP_DIRNAME, 'plenum', '__metadata__.py')
# Load the metadata using exec() so we don't trigger an import of ioflo.__init__
exec(compile(open(METADATA).read(), METADATA, 'exec'))

BASE_DIR = os.path.join(os.path.expanduser("~"), ".plenum")
CONFIG_FILE = os.path.join(BASE_DIR, "plenum_config.py")
POOL_TXN_FILE = os.path.join(BASE_DIR, "pool_transactions_sandbox")

if not os.path.exists(BASE_DIR):
    os.makedirs(BASE_DIR)

setup(
    name='plenum-3pc-batch',
    version=__version__,
    description='Plenum Byzantine Fault Tolerant Protocol',
    long_description='Plenum Byzantine Fault Tolerant Protocol',
    url='https://github.com/evernym/plenum',
    download_url='https://github.com/evernym/plenum/tarball/{}'.
        format(__version__),
    author=__author__,
    author_email='dev@evernym.us',
    license=__license__,
    keywords='Byzantine Fault Tolerant Plenum',
    packages=find_packages(exclude=['test', 'test.*', 'docs', 'docs*']) + [
        'data', ],
    package_data={
        '': ['*.txt', '*.md', '*.rst', '*.json', '*.conf', '*.html',
             '*.css', '*.ico', '*.png', 'LICENSE', 'LEGAL', 'plenum']},
    include_package_data=True,
    data_files=[(
        (BASE_DIR, ['data/pool_transactions_sandbox', ])
    )],
    install_requires=['ledger-3pc-batch==0.2.7', 'stp-3pc-batch==0.1.5',
                      'evernym-state==0.1.4', 'jsonpickle',
                      'portalocker==0.5.7', 'prompt_toolkit==0.57', 'pygments',
                      'ioflo==1.5.4', 'semver', 'base58', 'orderedset',
                      'sortedcontainers==1.5.7', 'psutil', 'pip', 'leveldb',
                      'rlp'],
    extras_require={
        'stats': ['python-firebase']
    },
    setup_requires=['pytest-runner'],
    tests_require=['pytest', 'pytest-xdist'],
    scripts=['scripts/plenum', 'scripts/init_plenum_keys',
             'scripts/start_plenum_node',
             'scripts/generate_plenum_pool_transactions',
             'scripts/gen_steward_key', 'scripts/gen_node',
             'scripts/export-gen-txns', 'scripts/get_keys',
             'scripts/udp_sender', 'scripts/udp_receiver']
)

if not os.path.exists(CONFIG_FILE):
    with open(CONFIG_FILE, 'w') as f:
        msg = "# Here you can create config entries according to your " \
              "needs.\n " \
              "# For help, refer config.py in the sovrin package.\n " \
              "# Any entry you add here would override that from config " \
              "example\n"
        f.write(msg)

DATA_DIR = os.path.dirname(data.__file__)
shutil.copyfile(os.path.join(DATA_DIR, "pool_transactions_sandbox"),
                POOL_TXN_FILE)


# TODO: This code should not be copied here.
import getpass
import os
import shutil
import sys


def getLoggedInUser():
    if sys.platform == 'wind32':
        return getpass.getuser()
    else:
        if 'SUDO_USER' in os.environ:
            return os.environ['SUDO_USER']
        else:
            return getpass.getuser()


def changeOwnerAndGrpToLoggedInUser(directory, raiseEx=False):
    loggedInUser = getLoggedInUser()
    try:
        shutil.chown(directory, loggedInUser, loggedInUser)
    except Exception as e:
        if raiseEx:
            raise e
        else:
            pass


changeOwnerAndGrpToLoggedInUser(BASE_DIR)


@@ -2214,17 +2214,17 @@
 ch==0.1.
-5
+8
 ',%0A     

increment stp version