# Copyright 2016 Mycroft AI, Inc.
#
# This file is part of Mycroft Core.
#
# Mycroft Core is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mycroft Core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mycroft Core.  If not, see <http://www.gnu.org/licenses/>.


import shutil
import subprocess

import os
from setuptools import find_packages

from mycroft.util.log import LOG

__author__ = 'seanfitz'

BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))


def place_manifest(manifest_file):
    shutil.copy(manifest_file, "MANIFEST.in")


def get_version():
    version = None
    try:
        from mycroft.version import CORE_VERSION_STR
        version = CORE_VERSION_STR
    except Exception as e:
        try:
            version = "dev-" + subprocess.check_output(
                ["git", "rev-parse", "--short", "HEAD"]).strip()
        except subprocess.CalledProcessError, e2:
            version = "development"
            LOG.debug(e)
            LOG.exception(e2)

    return version


def required(requirements_file):
    with open(os.path.join(BASEDIR, requirements_file), 'r') as f:
        requirements = f.read().splitlines()
        return [pkg for pkg in requirements if not pkg.startswith("--")]


def find_all_packages(where):
    packages = find_packages(where=where, exclude=["*test*"])
    return [
        os.path.join(where, pkg.replace(".", os.sep))
        for pkg in packages] + [where]
