#!/usr/bin/python
import argparse
import pprint
import json
import os

import pych

def verify_conf(conf):
    """Sanity checks the configuration."""

    print("Checking installation...")
    print(" * Templates")
    for slang in conf["specializers"]:
        if not os.path.exists(conf["specializers"][slang]):
            print("Missing templates")

    print(" * Object Storage")
    try:
        for slang in conf["object_store"]["output_paths"]:
            o_path = conf["object_store"]["output_paths"][slang]
            path = os.sep.join([
                conf["object_store"]["root_path"],
                o_path,
                'write_test'
            ])
            with open(path, 'w') as fd:
                fd.write("test")
    except Exception as e:
        print("Error trying to write to object-store: %s", e)

    # Check that Chapel libraries are there
    print(" * Libraries")

    # Check that c-headers are there
    print(" * Headers")

    # Check that the commands are invokable
    print(" * Commands")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='Tool aiding the Python/Chapel (pyChapel)  module'
    )
    parser.add_argument(
        '--compile',
        metavar="source_file",
        type=str,
        help='Compile the given Chapel module source-code into a Python module.'
    )
    parser.add_argument(
        '--check',
        action="store_const",
        const=True,
        default=False,
        help="Check the pyChapel installation / configuration"
    )

    args = parser.parse_args()
    if args.check:
        if not pych.config:
            print("pyChapel: Cannot find configuration file (pych.json).")
        else:
            verify_conf(pych.config)
            print "If you did not stumble over any errors above you're all set!"
    elif args.compile:
        print "Compile this"
    else:
        parser.print_help()
