#!python
import argparse
import os
import sys
import traceback

import beastling.beastxml
import beastling.configuration
import beastling.extractor

def main():

    # Parse command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("config", help="Beastling configuration file (or XML file if --extract is used)", default=None)
    parser.add_argument("output", nargs="?", help="Output filename, no extension (default beastling)", default=None)
    parser.add_argument("--extract", default=False, action="store_true")
    parser.add_argument("--overwrite", default=False, action="store_true")
    parser.add_argument("--stdin", default=False, action="store_true")
    parser.add_argument("-v", "--verbose", default=False, action="store_true")
    args = parser.parse_args()

    if args.extract:
        extract(args)
    else:
        generate(args)

def extract(args):
    if not os.path.exists(args.config):
        sys.stderr.write("No such BEAST XML file: %s\n", args.config)
        sys.exit(1)
    try:
        messages = beastling.extractor.extract(args.config, args.overwrite)
    except Exception as e:
        sys.stderr.write("Error encountered while extracting BEASTling config and/or data files:\n")
        traceback.print_exc()
        sys.exit(1)
    for msg in messages:
        sys.stdout.write(msg)

def generate(args):

    # Build config object
    try:
        config = beastling.configuration.Configuration(basename=args.output, configfile=args.config, stdin_data=args.stdin)
        config.process()
    except Exception as e:
        sys.stderr.write("Error encountered while parsing configuration file:\n")
        traceback.print_exc()
        sys.exit(1)

    # If verbose mode is on, print any messages which were generated while
    # processing the configuration
    if args.verbose:
        for msg in config.messages:
            sys.stderr.write(msg+"\n")

    # Build XML file
    try:
        xml = beastling.beastxml.BeastXml(config)
    except Exception as e:
        sys.stderr.write("Error encountered while building BeastXML object:\n")
        traceback.print_exc()
        sys.exit(1)

    # Write XML file
    filename = args.output if args.output else config.basename+".xml"
    if os.path.exists(filename) and not args.overwrite:
        sys.stderr.write("File %s already exists!  Run beastling with the --overwrite option if you wish to overwrite it.\n" % filename)
        sys.exit(1)
    xml.write_file(filename)

if __name__ == "__main__":
    main()
