#!/usr/bin/env python

"""
case.submit - Submit a cesm workflow to the queueing system or run it if there is no queueing system.   A cesm workflow may include multiple jobs.
"""

from standard_script_setup import *
from CIME.case        import Case

###############################################################################
def parse_command_line(args, description):
###############################################################################
    parser = argparse.ArgumentParser(
        usage="""\n{0} [<casedir>] [--verbose]
OR
{0} --help
\033[1mEXAMPLES:\033[0m
    \033[1;32m# Setup case \033[0m
    > {0}

    \033[1;32m# Setup case, request mail at job begin and job end \033[0m
    > {0} -m begin,end
""".format(os.path.basename(args[0])),
        description=description,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )

    CIME.utils.setup_standard_logging_options(parser)

    parser.add_argument("caseroot", nargs="?", default=os.getcwd(),
                        help="Case directory to setup")

    parser.add_argument("--job", "-j",
                        help="Name of the job to be submitted, default is case.run"
                        " can be any of the jobs listed in env_batch.xml")

    parser.add_argument("--no-batch", action="store_true",
                        help="Do not submit jobs to batch system, run locally.")

    parser.add_argument("--prereq",
                        help="Specify a prerequiset job id, this job will not start until the "
                        "job with this id is completed (batch mode only)")

    parser.add_argument("--resubmit", action="store_true",
                        help="Used with tests only, to continue rather than restart a test. ")

    parser.add_argument("--skip-preview-namelist", action="store_true",
                        help="Skip calling preview-namelist during case.run")

    CIME.utils.add_mail_type_args(parser)

    parser.add_argument("-a", "--batch-args",
                        help="Used to pass additional arguments to batch system. ")

    args = CIME.utils.parse_args_and_handle_standard_logging_options(args, parser)

    CIME.utils.resolve_mail_type_args(args)

    return args.caseroot, args.job, args.no_batch, args.prereq, \
        args.resubmit, args.skip_preview_namelist, args.mail_user, args.mail_type, \
        args.batch_args

###############################################################################
def _main_func(description):
###############################################################################
    caseroot, job, no_batch, prereq, resubmit, skip_pnl, \
        mail_user, mail_type, batch_args = parse_command_line(sys.argv, description)

    with Case(caseroot, read_only=False) as case:
        case.submit(job=job, no_batch=no_batch, prereq=prereq, resubmit=resubmit,
               skip_pnl=skip_pnl, mail_user=mail_user, mail_type=mail_type,
               batch_args=batch_args)

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