#!/usr/bin/env python3

"""
build cice library
"""
import sys, os

_CIMEROOT = os.environ.get("CIMEROOT")
if _CIMEROOT is None:
    raise SystemExit("ERROR: must set CIMEROOT environment variable")

_LIBDIR = os.path.join(_CIMEROOT, "scripts", "Tools")
sys.path.append(_LIBDIR)

from standard_script_setup import *
from CIME.buildlib         import parse_input
from CIME.case             import Case
from CIME.utils            import expect, run_bld_cmd_ensure_logging, safe_copy

logger = logging.getLogger(__name__)

###############################################################################
def buildlib(bldroot, installpath, case):
###############################################################################
    casebuild    = case.get_value("CASEBUILD")
    cice_blckx   = case.get_value("CICE_BLCKX")
    cice_blcky   = case.get_value("CICE_BLCKY")
    cice_mxblcks = case.get_value("CICE_MXBLCKS")
    cppdefs      = case.get_value("CICE_CPPDEFS")

    #-------------------------------------------------------
    # compute all cppdefs, unlike other components, needs to make a CIME_cppdefs file
    #-------------------------------------------------------

    cppdefs += " -DBLCKX={} -DBLCKY={} -DMXBLCKS={}\n".format(cice_blckx, cice_blcky, cice_mxblcks)
    with open(os.path.join(casebuild, "ciceconf", "CIME_cppdefs"), "w") as fd:
        fd.write(cppdefs)

###############################################################################
def _main_func():
###############################################################################
    caseroot, libroot, bldroot = parse_input(sys.argv)
    with Case(caseroot, read_only=False) as case:
        buildlib(bldroot, libroot, case)

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

if __name__ == "__main__":
    _main_func()
