#!/bin/sh

# BEGIN DESCRIPTION
# SYNOPSYS
#     $program [DIR]
#
# DESCRIPTION
#     This program patches hscpipe 7.x so that it will be able to process
#     new narrow band filters.
#
#     If hscpipe 7.x has been set up (or to be specific, if \$OBS_SUBARU_DIR
#     has been set), DIR can be omitted.
#     If DIR is specified, it must be the path to hscpipe 7.x. For example,
#
#         $program /opt/hscpipe/7.9.1
# END DESCRIPTION

if [ "$1" = "-h" ] || [ "$1" = "--help" ]
then
    program=$(basename "$0")
    eval echo \""$(
        sed -n -e '/^# BEGIN DESCRIPTION/,/^# END DESCRIPTION/{s/^\(# \|#\)//;p}' "$0" |
        sed -n -e '1d;$d;p'
    )"\"

    exit 0
fi

if [ "$#" -eq 0 ] && [ -z "$OBS_SUBARU_DIR" ]
then
    echo "Please setup hscpipe 7.x first,"
    echo "or specify where hscpipe 7.x has been installed."
    echo ""
    echo "See --help."
    exit 1
fi

if [ "$#" -gt 1 ]
then
    shift
    echo "error: Unrecognized arguments:" "$@"
    echo ""
    echo "See --help."
    exit 1
fi

if [ "$#" -eq 1 ]
then
    echo "Searching $1 for 'obs_subaru' installation ..."
    found="$(find "$1" -path "*/obs_subaru" -exec find "{}" -type d -name "python" ";")"

    if [ -z "$found" ]
    then
        echo "error: Directory 'obs_subaru' is not found."
        exit 1
    fi
    if [ "$(echo "$found" | wc -l)" -ne 1 ]
    then
        echo "error: More than one 'obs_subaru' installation are found:"
        echo "$found"
        exit 1
    fi

    OBS_SUBARU_DIR="$(dirname "$found")"
    echo "Found OBS_SUBARU_DIR=$OBS_SUBARU_DIR"
fi

if ! cd "$OBS_SUBARU_DIR"
then
    echo "error: Cannot change directory to: \$OBS_SUBARU_DIR=$OBS_SUBARU_DIR"
    exit 1
fi

if [ "$(cksum config/hsc/colorterms.py)" != "118012843 3097 config/hsc/colorterms.py" ]
then
    echo "warning: File checksum indicates $OBS_SUBARU_DIR has already been modified."
    echo "Applying patches to this directory may mess it up. Proceed anyway?"
    answer="n"
    read -p "[y/N]" answer
    if [ -z "$(echo "$answer" | grep '^y')" ]
    then
        echo "Canceled."
        exit 1
    fi
fi

echo "Applying patches to $OBS_SUBARU_DIR..."

patch -p1 << "__eof__"
diff -ur old/config/hsc/colorterms.py new/config/hsc/colorterms.py
--- old/config/hsc/colorterms.py	2019-10-30 13:02:00.000000000 +0900
+++ new/config/hsc/colorterms.py	2020-01-20 13:21:08.868934557 +0900
@@ -20,6 +20,17 @@
         'y': Colorterm(primary="z", secondary="i", c0=0.01739708, c1=0.35652971, c2=0.00574408),
         'N816': Colorterm(primary="i", secondary="z", c0=0.00927133, c1=-0.63558358, c2=-0.05474862),
         'N921': Colorterm(primary="z", secondary="i", c0=0.00752972, c1=0.09863530, c2=-0.05451118),
+        'I945': Colorterm(primary="z", secondary="i",c0=0.008117, c1=0.234991, c2=-0.042255),
+        'N387': Colorterm(primary="u", secondary="g",c0=-0.709229, c1=0.310719, c2=-0.044107),
+        'N400': Colorterm(primary="u", secondary="g",c0=-0.396264, c1=-0.395133, c2=0.038688),
+        'N468': Colorterm(primary="g", secondary="r",c0=-0.059159, c1=-0.030881, c2=0.015356),
+        'N515': Colorterm(primary="g", secondary="r",c0=-0.032510, c1=-0.354440, c2=0.100832),
+        'N527': Colorterm(primary="g", secondary="r",c0=-0.029400, c1=-0.453037, c2=0.020922),
+        'N656': Colorterm(primary="r", secondary="i",c0=0.037014, c1=-0.538947, c2=0.052489),
+        'N718': Colorterm(primary="r", secondary="i",c0=-0.014742, c1=-0.787571, c2=0.237867),
+        'N926': Colorterm(primary="z", secondary="i",c0=0.009369, c1=0.130261, c2=-0.119282),
+        'N973': Colorterm(primary="z", secondary="i",c0=-0.005805, c1=0.220412, c2=-0.249072),
+        'N1010': Colorterm(primary="z", secondary="i",c0=0.015296, c1=0.794152, c2=0.465309),
     }),
     "ps1*": ColortermDict(data={
         'g': Colorterm(primary="g", secondary="r", c0=0.005905, c1=0.063651, c2=-0.000716),
@@ -40,5 +51,7 @@
         'N921': Colorterm(primary="z", secondary="y", c0=0.002039, c1=-0.477412, c2=-0.492151),
         'N973': Colorterm(primary="y", secondary="z", c0=-0.007775, c1=-0.050972, c2=-0.197278),
         'N1010': Colorterm(primary="y", secondary="z", c0=0.003607, c1=0.865366, c2=1.271817),
+        'N400': Colorterm(primary="g", secondary="r",c0=0.176542, c1=1.127055, c2=0.505502),
+        'N926': Colorterm(primary="z", secondary="y",c0=0.005230, c1=-0.574448, c2=-0.330899),
     }),
 }
diff -ur old/config/hsc/extinctionCoeffs.py new/config/hsc/extinctionCoeffs.py
--- old/config/hsc/extinctionCoeffs.py	2019-10-30 13:02:00.000000000 +0900
+++ new/config/hsc/extinctionCoeffs.py	2020-01-20 13:21:08.870934545 +0900
@@ -11,4 +11,16 @@
     "NB0387": 4.007,
     "NB0816": 1.458,
     "NB0921": 1.187,
+    "HSC-R2": 2.277,
+    "HSC-I2": 1.617,
+    "NB0400": 3.891,
+    "NB0468": 3.351,
+    "NB0515": 2.939,
+    "NB0527": 2.855,
+    "NB0656": 2.077,
+    "NB0718": 1.812,
+    "NB0926": 1.177,
+    "NB0973": 1.083,
+    "IB0945": 1.134,
+    "NB1010": 1.013,
 }
diff -ur old/config/hsc/mergeCoaddDetections.py new/config/hsc/mergeCoaddDetections.py
--- old/config/hsc/mergeCoaddDetections.py	2019-10-30 13:02:00.000000000 +0900
+++ new/config/hsc/mergeCoaddDetections.py	2020-01-20 13:21:08.872934532 +0900
@@ -1,2 +1,2 @@
 config.priorityList = ["HSC-I2", "HSC-I", "HSC-R2", "HSC-R", "HSC-Z", "HSC-Y", "HSC-G", "NB0921", "NB0816",
-                       "NB1010", "NB0387", "NB0515", ]
+                       "NB1010", "NB0387", "NB0515", "NB0400", "NB0468", "NB0515", "NB0527", "NB0656", "NB0718", "NB0926", "NB0973", "IB0945", ]
diff -ur old/config/hsc/mergeCoaddMeasurements.py new/config/hsc/mergeCoaddMeasurements.py
--- old/config/hsc/mergeCoaddMeasurements.py	2019-10-30 13:02:00.000000000 +0900
+++ new/config/hsc/mergeCoaddMeasurements.py	2020-01-20 13:21:08.874934519 +0900
@@ -1,2 +1,2 @@
 config.priorityList = ["HSC-I2", "HSC-I", "HSC-R2", "HSC-R", "HSC-Z", "HSC-Y", "HSC-G", "NB0921", "NB0816",
-                       "NB1010", "NB0387", "NB0515", ]
+                       "NB1010", "NB0387", "NB0515", "NB0400", "NB0468", "NB0515", "NB0527", "NB0656", "NB0718", "NB0926", "NB0973", "IB0945", ]
__eof__
