#!/usr/bin/env python3

"""
build cime component model library.   This buildlib script is used by all cime internal
components.
"""

import sys, os

_CIMEROOT = os.environ.get("CIMEROOT")
if _CIMEROOT == None:
    raise ValueError("ERROR: CIMEROOT not defined in buildlib_cmake.internal_components.")
sys.path.append(os.path.join(_CIMEROOT, "scripts", "Tools"))

from standard_script_setup import *
from CIME.buildlib import build_cime_component_lib, parse_input
from CIME.case import Case

def buildlib(bldroot, libroot, case, compname=None):
    if compname is None:
        thisdir = os.path.dirname(os.path.abspath(__file__))
        path, dir1 = os.path.split(thisdir)
        _, dir2 = os.path.split(path)
        if dir1 == "cime_config":
            compname = dir2
        else:
            compname = dir1.split('.')[1]
    build_cime_component_lib(case, compname, libroot, bldroot)

def _main_func(args):
    caseroot, libroot, bldroot = parse_input(args)
    with Case(caseroot) as case:
        buildlib(bldroot, libroot, case)

if __name__ == "__main__":
    _main_func(sys.argv)
