#!/usr/bin/env python

"""
This script determines if the required data files for your case exist on local disk in the appropriate subdirectory of
$DIN_LOC_ROOT. It automatically downloads missing data required for your simulation.

It is recommended that users on a given system share a common $DIN_LOC_ROOT directory to avoid duplication on
disk of large amounts of input data. You may need to talk to your system administrator in order to set this up.

This script should be run from $CASEROOT.

To verify the presence of required data use:
   ./check_input_data

To obtain missing datasets from the input data server(s) use:
   ./check_input_data --download

This script is automatically called by the case control system, when the case is built and submitted.
So manual usage of this script is optional.
"""
from standard_script_setup import *
from CIME.case import Case

import argparse

###############################################################################
def parse_command_line(args, description):
###############################################################################
    parser = argparse.ArgumentParser(
        description=description,
        formatter_class=argparse.RawTextHelpFormatter)

    CIME.utils.setup_standard_logging_options(parser)

    parser.add_argument("--protocol", default=None,
                        help="The input data protocol to download data.")

    parser.add_argument("--server", default=None,
                        help="The input data repository from which to download data.")


    parser.add_argument("-i", "--input-data-root",default=None,
                        help="The root directory where input data goes,\n"
                        "use xmlquery DIN_LOC_ROOT to see default value.")


    parser.add_argument("--data-list-dir", default="Buildconf",
                        help="Where to find list of input files")

    parser.add_argument("--download", action="store_true",
                        help="Attempt to download missing input files")

    parser.add_argument("--chksum", action="store_true",
                        help="chksum inputfiles against inputdata_chksum.dat (if available)")

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

    return args.protocol, args.server, args.input_data_root, args.data_list_dir, args.download, args.chksum

###############################################################################
def _main_func(description):
###############################################################################
    protocol, address, input_data_root, data_list_dir, download, chksum = parse_command_line(sys.argv, description)

    with Case() as case:
        sys.exit(0 if case.check_all_input_data(protocol=protocol,
                                                address=address,
                                                input_data_root=input_data_root,
                                                data_list_dir=data_list_dir,
                                                download=download,
                                                chksum=chksum) else 1)

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

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