#! /usr/bin/env python
import sys

from crds import bestrefs, pysh

if "--help" in sys.argv or "-h" in sys.argv:
    sys.argv = sys.argv[:1]   # truncate to fail usage

pysh.usage("""[-d] [-v] [-h] [--print-affected] <crds_context> <dataset_file(s)>...

-d                     dry run,  do not update file headers
-v                     verbose,  output additional diagnostic messages
-h                     help,  print this help
--print-affected       print files with updated bestrefs

NOTE:   DEPRECATED,  see safe_bestrefs instead

Updates dataset FITS files with best references recommended by <crds_context>.

<crds_context> is a CRDS context file, explicitly named e.g. hst_0004.pmap
<crds_context> can be specified abstractly,  e.g.  hst-edit or hst-operational
<crds_context> can be specified by date,  e.g.  hst-2013-01-29T12:00:00

<dataset_file(s)> are raw dataset files for which best references are
computed and updated.
""", 2)

DRY_RUN = False
if "-d" in sys.argv:
    sys.argv.remove("-d")
    DRY_RUN = True

context = sys.argv[1]
pars = "--files {} ".format(" ".join(sys.argv[2:]))
# if context != "hst-operational":  # hst-operational == default
pars = "--new-context " + context + " " + pars

if not DRY_RUN:
    pars += " --update-bestrefs"

pars = sys.argv[0] + " " + pars

sys.exit(bestrefs.BestrefsScript(argv = pars)())

