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

import sys
import optparse

import logging

import checkm

from StringIO import StringIO

# quieten the Checkm logger
logger = checkm.logger
logger.setLevel(logging.ERROR)

def _option_parser():
    parser = optparse.OptionParser()
    parser.add_option("-a", "--algorithm", dest="alg",
                  help="Algorithm to use to hash files",
                  default="md5")
    parser.add_option("-v", "--verbose", dest="verbose",
                  action="store_true",
                  help="Log information to stdin as it goes",
                  default=False)
    parser.add_option("-r", "--recursive", dest="recursive",
                  action="store_true",
                  help="Recursively scan through child directories",
                  default=False)
    return parser

if __name__ == '__main__':
    o = _option_parser()
    values, args = o.parse_args()
    if len(args)>0:
        cmd = args[0]
    else:
        cmd = ""
    filename,filepath = "checkm.txt", "."
    alg = values.alg
    recursive = values.recursive
    if values.verbose:
        logger.setLevel(logging.DEBUG)
    try:
        filename = args[1]
        filepath = args[2]
    except IndexError:
        pass

    if cmd == 'write':
        r = checkm.CheckmReporter()
        r.create_checkm_file(filepath, alg, filename, recursive=recursive, columns=5)
    elif cmd == 'check':
        r = checkm.CheckmReporter()
        results = r.check_checkm_hashes(filepath, filename)
        if results['fail']:
            print "Checkm failed for the following:"
            for filepath in results['fail']:
                record = results['fail'][filepath]
                print "Checkm record: %s" % (", ".join(record[0]))
                print "doesn't match scan result: %s" % (", ".join(record[1]))
                sys.exit("Failed checkm")
    elif cmd == 'print':
        r = checkm.CheckmReporter()
        s = StringIO()
        s = r.create_checkm_file(filepath, alg, s, recursive=recursive, columns=5)
        s.seek(0)
        print s.read()
        s.close()
    elif cmd == 'help' or cmd == "":
        print "write - writes the manifest to a file"
        print "    - checkm write [checkm filename [filepath]]"
        print "check - checks a filepath against a manifest"
        print "    - checkm check [checkm filename]"
        print "print - print out the result of a checkm"
        print "    - checkm print [checkm_filename [filepath]]"
    else:
        print "unknown command: %s" % cmd
        print "Try write, check or print"
        print "write - writes the manifest to a file"
        print "    - checkm write [checkm filename [filepath]]"
        print "check - checks a filepath against a manifest"
        print "    - checkm check [checkm filename]"
        print "print - print out the result of a checkm"
        print "    - checkm print [checkm_filename [filepath]]"
