# -*- python -*-

import lsst.sconsUtils
import glob
import os

# scons steals our environment away, so we have to reinstate it
env = lsst.sconsUtils.env.Clone()
environ = dict((name, os.environ[name]) for name in ("PYTHONPATH", "LD_LIBRARY_PATH",
                                                     "DYLD_LIBRARY_PATH", "PATH"))
env.Append(**environ)
# scons also hacks sys.path, so we need to set PYTHONPATH very explicitly at the start of each command.
python = "PYTHONPATH=%s " % environ["PYTHONPATH"]

dep = "#python/lsst/obs/subaru/_subaruLib.so" # Dependency required for running the python scripts

defectsDatList = glob.glob("defects/*/defects.dat")
for defectsDat in defectsDatList:
    command = "%s bin.src/genDefectFits.py HSC %s --force" % (python, os.path.join("hsc", defectsDat))
    env.AlwaysBuild(env.Command(defectsDat + "-fits", [defectsDat, dep], command))

command = "%s bin.src/genDefectRegistry.py --root hsc/defects" % (python)
registry = env.AlwaysBuild(env.Command("defects/defectRegistry.sqlite3",
                                       [d + "-fits" for d in defectsDatList] + [dep], command))
