#!/usr/bin/env python

"""
check_case verifies that the case is set up correctly
"""

from standard_script_setup import *

from CIME.utils import expect
from CIME.case  import Case
from CIME.check_lockedfiles import check_lockedfiles
from CIME.preview_namelists import create_namelists

import argparse, doctest

logger = logging.getLogger(__name__)

###############################################################################
def parse_command_line(args, description):
###############################################################################

    parser = argparse.ArgumentParser(
        usage="""\n{0} [--verbose]
OR
{0} --help
OR
{0} --test

\033[1mEXAMPLES:\033[0m
    \033[1;32m# Run \033[0m
    > {0}
""".format(os.path.basename(args[0])),

description=description,

formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

    CIME.utils.setup_standard_logging_options(parser)

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

###############################################################################
def _main_func(description):
###############################################################################

    if ("--test" in sys.argv):
        test_results = doctest.testmod(verbose=True)
        sys.exit(1 if test_results.failed > 0 else 0)

    parse_command_line(sys.argv, description)

    check_lockedfiles()

    with Case(read_only=False) as case:
        create_namelists(case)
        build_complete = case.get_value("BUILD_COMPLETE")

    if not build_complete:
        expect(False,
               "Please rebuild the model interactively by calling case.build")

    logger.info( "check_case OK ")

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

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