#!/usr/bin/env python3

"""
Drive ctest testing of scream's scripts. Because this includes
testing of jenkins scripts, it should always be run from a login node.
"""

from utils import check_minimum_python_version
check_minimum_python_version(3, 4)

import argparse, sys, pathlib

from scripts_ctest_driver import ScriptsCtestDriver

###############################################################################
def parse_command_line(args, description):
###############################################################################
    parser = argparse.ArgumentParser(
        usage="""\n{0} <ARGS> [--verbose]
OR
{0} --help

    \033[1mEXAMPLES (assumes user is on machine mappy):\033[0m
    \033[1;32m# Run tests \033[0m
    > cd $scream_repo/components/eamxx
    > ./scripts/{0} -m mappy
""".format(pathlib.Path(args[0]).name),
        description=description,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )

    parser.add_argument("-s", "--submit", action="store_true", help="Submit results to dashboad")

    parser.add_argument("-m", "--machine",
        help="Provide machine name. This is *always* required")

    parser.add_argument("-r", "--root-dir",
                        help="The root directory of the scream src you want to test. "
                        "Default will be the scream src containing this script.")

    parser.add_argument("-w", "--work-dir",
        help="The work directory where all the building/testing will happen. Defaults to ${root_dir}/ctest-build-scripts")

    parser.add_argument("-d", "--dry-run", action="store_true",
                        help="Do a dry run, commands will be printed but not executed")

    return parser.parse_args(args[1:])

###############################################################################
def _main_func(description):
###############################################################################
    scd = ScriptsCtestDriver(**vars(parse_command_line(sys.argv, description)))

    success = scd.scripts_ctest_driver()

    print("OVERALL STATUS: {}".format("PASS" if success else "FAIL"))

    sys.exit(0 if success else 1)

###############################################################################

if (__name__ == "__main__"):
    _main_func(__doc__)
