#!/usr/bin/env python

"""
build model executable
"""

import sys, os

_CIMEROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..","..","..","..")
sys.path.append(os.path.join(_CIMEROOT, "scripts", "Tools"))

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

logger = logging.getLogger(__name__)

###############################################################################
def buildlib(bldroot, installpath, case): # pylint: disable=unused-argument
###############################################################################
    casebuild = case.get_value("CASEBUILD")
    caseroot  = case.get_value("CASEROOT")
    cimeroot  = case.get_value("CIMEROOT")
    num_esp   = case.get_value("NUM_COMP_INST_ESP")

    expect((num_esp is None) or (int(num_esp) == 1), "ESP component restricted to one instance")

    with open(os.path.join(casebuild, "cplconf", "Filepath"), "w") as out:
        out.write(os.path.join(caseroot, "SourceMods", "src.drv") + "\n")
        out.write(os.path.join(cimeroot, "src", "drivers", "mct", "main") + "\n")

    with open(os.path.join(casebuild, "cplconf", "CCSM_cppdefs"), "w") as out:
        out.write("")

###############################################################################
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()
