# -*- python -*-

import lsst.sconsUtils
import glob
import os

# scons steals our environment away, so we have to reinstate it
env = lsst.sconsUtils.env.Clone()
for name in ("PYTHONPATH", "LD_LIBRARY_PATH",
             "DYLD_LIBRARY_PATH", "PATH"):
    if name in os.environ:
        env.AppendENVPath(name, os.environ[name])

# we may need an explicit library load path specified in the command
libpathstr = lsst.sconsUtils.utils.libraryLoaderEnvironment()

# We always run these commands with an explicit python rather than relying on
# the shebang
python = "{} python".format(libpathstr)

for camera in ["latiss", "lsstCam", "imsim", "phosim", "ts8", "ucd", "ts3", "comCam", "comCamSim", "lsstCamSim"]:
    # Invoke the bin.src variant so that we do not depend on the shebang
    # target having been run
    commandInst = env.Command(f"{camera}.yaml", "",
                              f"{python} bin.src/generateCamera.py --path {camera}:lsstCam:. $TARGET")

    env.Depends(commandInst, lsst.sconsUtils.targets["version"])
    env.Depends(commandInst, lsst.sconsUtils.targets["python"])
    env.Depends(lsst.sconsUtils.targets["tests"], f"{camera}.yaml")
    dirList = ["lsstCam"]
    if os.path.isdir(camera):
        dirList.append(camera)

    for d in dirList:
        for f in glob.glob(os.path.join(d, "*.yaml")):
            env.Depends(commandInst, f)

    for f in ["cameraHeader.yaml", "cameraTransforms.yaml", "rafts.yaml", env.Glob(f"R[0-9][0-9].yaml")]:
        env.Depends(commandInst, f)
