#!/usr/bin/env python3
"""
 CIJOE test analyser
"""
from __future__ import print_function
import argparse
import sys
import os
import cij.analyser
import cij.runner
import cij.util
import cij


def parse_args():
    """Parse command-line arguments for cij_analyser"""

    # Parse the Command-Line
    prsr = argparse.ArgumentParser(
        description='cij_analyser - CIJOE Test Analyser',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    prsr.add_argument(
        '--output',
        help="Path to test output",
        default=os.getcwd()
    )
    prsr.add_argument(
        '--preqs',
        help="Path to performance requirements yaml file",
        default=None,
        required=True,
    )

    args = prsr.parse_args()

    args.output = cij.util.expand_path(args.output)
    if not os.path.exists(args.output):
        cij.err(f"rprtr:output: {args.output}, does not exist")
        return None

    args.trun_fpath = cij.runner.yml_fpath(args.output)
    if not os.path.exists(args.trun_fpath):
        cij.err(f"rprtr:trun_fpath: {args.trun_fpath}")
        return None

    return args


def main():
    """
    Parse environment variables and command-line arguments constructing a
    configuration for which to invoke the analyser
    """

    args = parse_args()
    if args is None:
        cij.err("rprtr: failed parsing command-line args")
        return 1

    rcode = cij.analyser.main(args)
    if rcode:
        cij.err(f"rprtr: rcode: {rcode}, error while analysing {args.output}")

    return rcode


if __name__ == "__main__":
    sys.exit(main())
