#!/usr/bin/env python
# -*- coding: utf-8 -*-

# The MIT License (MIT)
# Copyright © 2016, Jace Browning
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all wcopies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


from __future__ import unicode_literals

import os
import sys
import argparse
try:
    import configparser  # Python 3
except ImportError:
    import ConfigParser as configparser  # Python 2
import subprocess
import logging

__version__ = '0.2'

CONFIG_FILENAMES = ['.verchew', '.verchewrc', 'verchew.ini', '.verchew.ini']

log = logging.getLogger(__name__)


def main():
    args = parse_args()
    configure_logging(args.verbose)
    path = find_config()
    config = parse_config(path)
    if not check_dependencies(config):
        sys.exit(1)


def parse_args():
    parser = argparse.ArgumentParser()
    version = "%(prog)s v" + __version__
    parser.add_argument('--version', action='version', version=version)
    parser.add_argument('-v', '--verbose', action='count', default=0,
                        help="enable verbose logging")

    return parser.parse_args()


def configure_logging(count=0):
    if count == 0:
        level = logging.WARNING
    elif count == 1:
        level = logging.INFO
    else:
        level = logging.DEBUG

    logging.basicConfig(level=level, format="%(levelname)s: %(message)s")


def find_config(root=None, config_filenames=None):
    root = root or os.getcwd()
    config_filenames = config_filenames or CONFIG_FILENAMES

    path = None
    log.info("Looking for config file in: %s", root)
    log.debug("Filename options: %s", ", ".join(config_filenames))
    for filename in os.listdir(root):
        if filename in config_filenames:
            path = os.path.join(root, filename)
            log.info("Found config file: %s", path)
            return path

    msg = "No config file found in: {0}".format(root)
    raise RuntimeError(msg)


def parse_config(path):
    data = {}

    log.info("Parsing config file: %s", path)
    config = configparser.ConfigParser()
    config.read(path)

    for section in config.sections():
        data[section] = {}
        for name, value in config.items(section):
            data[section][name] = value

    return data


def check_dependencies(config):
    success = []

    for name, settings in config.items():
        show("Checking for {0}...".format(name), head=True)
        output = get_version(settings['cli'])
        if settings['version'] in output:
            show("✔ MATCHED: {0}".format(settings['version']))
            success.append("✔")
        else:
            show("✖ EXPECTED: {0}".format(settings['version']))
            success.append("✖")

    show("Results: " + " ".join(success), head=True)

    return "✖" not in success


def get_version(program):
    args = [program, '--version']

    show("$ {0}".format(" ".join(args)))
    try:
        raw = subprocess.check_output(args, stderr=subprocess.STDOUT)
    except OSError:
        output = "command not found"
    else:
        output = raw.decode('utf-8')
    log.debug("Command output: %r", output)

    show(output.splitlines()[0])

    return output


def show(text, start='', end='\n', head=False):
    """Python 2 and 3 compatible version of print."""
    if head:
        start = '\n'
        end = '\n\n'

    if log.getEffectiveLevel() < logging.WARNING:
        log.info(text)
    else:
        sys.stdout.write(start + text + end)


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