#!/usr/bin/env python
# -*- coding: utf-8 -*-
# © Copyright EnterpriseDB UK Limited 2015-2026 - All rights reserved.

from __future__ import absolute_import, division, print_function

__metaclass__ = type

import os
import sys
import traceback
import argparse

from tpaexec.test_compiler import TestCompiler


def main():
    """
    Given an input file and an output directory, compile and write output.

    This program tries to compile the input and write the compiled output
    to files under the output directory starting with index.yml, otherwise
    prints an error message and exits with a non-zero status.
    """

    prog = os.path.basename(sys.argv[0])
    p = argparse.ArgumentParser(
        prog=prog,
        usage=f"{prog} infile.t.yml /output/dir [options…]",
    )

    p.add_argument(
        "-v",
        "--verbose",
        dest="verbosity",
        action="count",
        help="increase verbosity",
        default=0,
    )

    p.add_argument(
        "--steps-from",
        dest="step_directories",
        nargs="+",
        metavar="DIR",
        help="location of custom step definitions",
        default=[],
    )

    p.add_argument("infile", help="path to input file")
    p.add_argument("outdir", help="path to output dir (which must exist)")

    args = p.parse_args()

    try:
        c = TestCompiler(options=vars(args))
        c.read_input(args.infile)
        c.write_output(args.outdir)

    except Exception as e:
        error = str(e)
        if f"'{args.infile}'" not in error:
            error = f"{args.infile}: {error}"
        print(f"ERROR: {error}", file=sys.stderr)
        if args.verbosity:
            tb = sys.exc_info()[2]
            tb = traceback.extract_tb(tb)[-1]
            print(f"From {tb[0]}:{tb[1]} (in {tb[2]})")
            print(f"\t{tb[3]}")
        sys.exit(-1)


if __name__ == "__main__":
    main()
