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

# Copyright 2017 by Leipzig University Library, http://ub.uni-leipzig.de
#                   The Finc Authors, http://finc.info
#                   Martin Czygan, <martin.czygan@uni-leipzig.de>
#
# This file is part of some open source application.
#
# Some open source application 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.
#
# Some open source application 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
#
# @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>

"""
Check external tools dependencies.

There is a tool called verchew for a more generic approach:

* https://github.com/jacebrowning/verchew

Can it handle programs, that do not report their version? -- https://github.com/jacebrowning/verchew/pull/38
"""

import os


def which(program):

    def is_exe(fpath):
        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            path = path.strip('"')
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file

    return None


class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

if __name__ == '__main__':
    deps = [
        ('7z', 'http://www.7-zip.org/7z.html'),
        ('csvcut', 'http://csvkit.rtfd.org/'),
        ('curl', 'https://curl.haxx.se/'),
        ('filterline', 'http://github.com/miku/filterline/releases'),
        ('flux.sh', 'https://github.com/culturegraph/metafacture-core/wiki'),
        ('groupcover', 'http://github.com/miku/groupcover/releases'),
        ('hurrly', 'http://github.com/miku/hurrly/releases'),
        ('jq', 'https://stedolan.github.io/jq/'),
        ('marctexttoxml', 'https://github.com/miku/marc21/tree/master/cmd/marctexttoxml'),
        ('metha-sync', 'http://github.com/miku/metha/releases'),
        ('microblob', 'http://github.com/miku/microblob/releases'),
        ('oaicrawl', 'https://github.com/miku/oaicrawl'),
        ('pigz', 'https://zlib.net/pigz/'),
        ('solrbulk', 'http://github.com/miku/solrbulk/releases'),
        ('span-import', 'http://github.com/miku/span/releases'),
        ('unzip', 'http://www.info-zip.org/pub/infozip/'),
        ('wget', 'https://www.gnu.org/software/wget/'),
        ('xmllint', 'http://xmlsoft.org/xmllint.html'),
        ('yaz-marcdump', 'http://www.indexdata.com/yaz'),
    ]

    for program, msg in sorted(deps):
        if which(program) is None:
            print('%sxx\t%s%s\t%s' % (bcolors.FAIL, bcolors.ENDC, program, msg))
        else:
            print('%sok\t%s%s' % (bcolors.OKGREEN, bcolors.ENDC, program))
