#!/usr/bin/env python

import os.path
import sys
import re
import json
import glob

opt_verbose = False
re_header = re.compile("^([\.\-\w]+): (.*)$")
re_json   = re.compile("^[\{\[].*")
re_xml    = re.compile("^[\<].*$")

def msg(text):
    if opt_verbose:
        print(text)

def json_check(filename):
    json_data = ''
    header = {}
    with open(filename, 'r') as FILE:
        for line in FILE:
            if line.isspace():
                msg("isspace")
                continue

            m = re_header.search(line)
            if m:
                header[m.group(1)] = m.group(1)
                msg("header")
                continue

            m = re_json.search(line)
            if m:
                msg("json data")
                json_data += line
                continue

            m = re_xml.search(line)
            if m:
                # Ignore file
                msg("ignoring file")
                break

            msg("Unknown content type in %s" %(filename))
            return True

    try:
        if not json_data or json_data.isspace():
            msg("%s: no json data" %(filename))
        else:
            j = json.loads(json_data)
            msg("%s: %d objects" %(filename, len(j)))
        return True

    except:
        msg("%s: %s" %(filename, 'INVALID JSON'))
        return False

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: sys.argv[0] <filename>")
        sys.exit(1)

    for arg in sys.argv[1:]:
        for path in glob.glob(arg):
            if os.path.isfile(path):
                if not json_check(path):
                    print(path)
                continue
            for root, dirs, files in os.walk(path):
                for filename in files:
                    if not json_check(os.path.join(root, filename)):
                        print(filename)
