#!/bin/csh

if ($#argv != 1) then
     echo "Usage: $0 dir1"
     echo "creates pysyn_cdbs directories for files to be"
     echo "delivered to SSB for ETC testing."
endif

# Base directory to and from
setenv dir1 /grp/hst/cdbs/deliveries/etc/$1
setenv dircdbs /grp/hst/cdbs/

#Save command line in argument dir1
#set dir1 = $1

echo $dir1
echo $dircdbs

if (! -d $dir1) then
    mkdir $dir1
    cd $dir1
    mkdir comp calobs calspec extinction grid mtab etc
    cd comp
    mkdir acs cos stis wfc3 nicmos nonhst ota
    cd ../etc
    mkdir background source
else
    echo "Do you want to make new directories for your working directory? "
    set mkd = $<
    set answer=`echo $mkd | sed 's/[Yy][Ee][Ss]/y/ig'`
    if ($answer != "" && $answer == 'y') then
       echo "Making the directory tree for the ETC delivery"
       cd $dir1 
       mkdir comp calobs calspec extinction grid mtab etc
       cd comp
       mkdir acs cos stis wfc3 nicmos nonhst ota
       cd ../etc
       mkdir background source
     else 
          echo " "
          #echo "Ok bye!"
          #exit 1
     endif
endif

# All the directories
echo "copying all the CDBS Tree to $dir1"
#echo " "
echo "Do you want to copy the grid files to your working directory? "
set mkd = $<
set answer=`echo $mkd | sed 's/[Yy][Ee][Ss]/y/ig'`
if ($answer != "" && $answer == 'y') then
    echo "copying the files in $dircdbs/grid/ to $dir1/grid/"
    cp -R -f $dircdbs/grid/* $dir1/grid/.
endif

echo " "
echo "Do you want to copy the CALSPEC files to your working directory? "
set mkd = $<
set answer=`echo $mkd | sed 's/[Yy][Ee][Ss]/y/ig'`
if ($answer != "" && $answer == 'y') then
    echo "copying the files in $dircdbs/calspec/ to $dir1/calspec/"
    cp -R -f $dircdbs/calspec/* $dir1/calspec/.

endif

echo " "
echo "Do you want to copy the CALOBS files to your working directory? "
set mkd = $<
set answer=`echo $mkd | sed 's/[Yy][Ee][Ss]/y/ig'`
if ($answer != "" && $answer == 'y') then
    echo "copying the files in $dircdbs/calobs/ to $dir1/calobs/"
    cp -R -f $dircdbs/calobs/* $dir1/calobs/.
endif

echo " "
echo "Do you want to copy the EXTINCTION files to your working directory? "
set mkd = $<
set answer=`echo $mkd | sed 's/[Yy][Ee][Ss]/y/ig'`
if ($answer != "" && $answer == 'y') then
    echo "copying the files in $dircdbs/extinction/ to $dir1/extinction/"
    cp -R -f $dircdbs/extinction/* $dir1/extinction/.
endif

echo " "
echo "Do you want to copy the BACKGROUND files to your working directory? "
set mkd = $<
set answer=`echo $mkd | sed 's/[Yy][Ee][Ss]/y/ig'`
if ($answer != "" && $answer == 'y') then
    echo "copying the files in $dircdbs/bakground/ to $dir1/bakground/"
    cp -R -f $dircdbs/etc/background/* $dir1/etc/background/.
endif

echo " "
echo "Do you want to copy the SOURCE files to your working directory? "
set mkd = $<
set answer=`echo $mkd | sed 's/[Yy][Ee][Ss]/y/ig'`
if ($answer != "" && $answer == 'y') then
    echo "copying the files in $dircdbs/etc/source to $dir1/etc/source"
    cp -R -f $dircdbs/etc/source/* $dir1/etc/source/.
endif

echo " "
echo "Done copying the files"

if ( -e ./tmp.dat) then
    echo 'tmp.dat exists, removing it'
    rm -f tmp.dat
endif
if ( -e ./mjd.dat) then
    echo 'mjd.dat exists, removing it'
    rm -f mjd.dat
endif

echo "provide with the MJD for operational ETC for the new mjd.dat file "
echo " if none provided it will use last used value"
set mjd_val = $<

if ($mjd_val == "") then
    echo " "
    echo " =====> No [mjd] value given for the operational ETC, leaving the old value"
    cp -f $dircdbs/etc/mjd.dat ./tmp.dat
else 
    echo " "
    echo "=====> You have selected  $mjd_val for the public ETC"
    if (! -e $dircdbs/etc/mjd.dat) then
         echo " "
         echo 'no $dir1cdbs/mjd.dat file to modify'
    else
        awk -v v1=$mjd_val ' \
            { if (index($0, "etc") != 0 && index($0, "hst") != 0) \
                  print "    " $1, $2, $3, $4, "       "$5, v1 ; \
              else \
               print $0 \
           }' $dircdbs/etc/mjd.dat > ./tmp.dat

        #awk ' { RS = ":" } ; {$1 = $mjd_val; print $0, $1, $2}' mjd.dat
    endif
    cp -f tmp.dat  mjd.dat
endif

echo " "
echo "provide with the MJD for the BIT ETC for the new mjd.dat file "
echo " if none provided it will use last used value"
set mjd_val_bit = $<

if ($mjd_val_bit == "") then
    echo " "
    echo "===== > No mjd value given for the BIT ETC, leaving the old value"
    cp -f tmp.dat  mjd.dat
else if ($mjd_val_bit != "") then
    echo " "
    echo "=====> You have selected  for the ETC BIT: $mjd_val_bit"
    awk -v v1=$mjd_val_bit ' \
        { if (index($0, "bit") != 0  && index($0, "hst") != 0) \
               print "    " $1, $2, $3, $4, "       "$5, v1 ; \
          else \
           print $0 \
       }' ./tmp.dat > mjd.dat

    #awk ' { RS = ":" } ; {$1 = $mjd_val; print $0, $1, $2}' mjd.dat
endif

if ($mjd_val != "" || $mjd_val_bit != "") then
     mv mjd.dat $dir1/etc/.
else
     cp $dircdbs/etc/mjd.dat $dir1/etc/mjd.dat
endif

echo " "
echo "=======> provide with the name of the new TMC file, even is the same as the old one "
set tmc_file = $<
cp -f $dircdbs/mtab/$tmc_file $dir1/mtab/$tmc_file 
echo " "
echo "=======> provide with the name of the new TMG file "
set tmg_file = $<
cp -f $dircdbs/mtab/$tmg_file $dir1/mtab/$tmg_file 
echo " "
echo "=======> provide with the name of the new TMT file , even is the same as the old one"
set tmt_file = $<
cp -f $dircdbs/mtab/$tmt_file $dir1/mtab/$tmt_file 

if ( -e tmp.dat) then
    rm -f tmp.dat
endif

if (! -e $dircdbs/etc/pysyn_lookup.dat) then
     echo " "
     echo "not $dircdbs/pysyn_lookup.dat file to modify"
else
     if ( -e ./pysyn_lookup.dat) then
          rm -f ./pysyn_lookup.dat
     endif
     echo " "
     echo "===> Making a local copy of pysyn_lookup.dat in /grp/hst/cdbs/etc/"
     cp -f $dircdbs/etc/pysyn_lookup.dat pysyn_lookup.dat
endif

if ($tmc_file == "") then
    echo " "
    echo "No TMC fila name given, please give me the name of the last"
    echo " TMC file in the /grp/hst/cdbs/mtab/ directory: "
    set tmc_file = $<
    cp -f $dircdbs/mtab/$tmc_file $dir1/mtab/$tmc_file
    echo " "
    set tmc = 1
    echo "the TMC file is $tmc_file"
endif

if ($tmc_file != "") then
    echo " "
    echo "You have selected  $tmc_file for the COMPTAB parameter"
    if (! -e ./pysyn_lookup.dat) then
         echo 'TMC step: no local pysyn_lookup.dat file to modify'
         set tmc = 0
    else
        set ihst = 0
        awk -v v1=$tmc_file ' \
            { if (index($0, "hst") != 0) {\
                  v2 = $0 ;\
                  ihst = 1; \
                  print v2 } \
               else if (index($0, "comptable") != 0 && ihst == 1) {\
                  gsub(/[ \t]+$/,"", v1); \
                  print "        " $1, "\x27$PYSYN_CDBS/mtab/" v1 "\x27,"} \
               else if (index($0, "jwst") != 0) { \
                  v2 = $0 ;\
                  ihst = 2; \
                  print v2 }     \
               else \
                  print $0 \
           }' pysyn_lookup.dat > tmp.dat
           set tmc = 1
    endif
else
    echo "No TMC file given. Exiting program"
    exit 1
endif

if ($tmg_file == "") then
    echo " "
    echo "No TMG fila name given, please give me the name of the last"
    echo " TMG file in the /grp/hst/cdbs/mtab/ directory: "
    set tmg_file = $<
    cp -f $dircdbs/mtab/$tmg_file $dir1/mtab/$tmg_file
    echo " "
    set tmg = 1
    echo "the TMG file is $tmg_file"
endif

if ($tmg_file == "") then
    echo " "
    echo "No TMG file name given. Aborting"
    set tmg = 0
    exit 1
    cp -f ./tmp.dat ./tmp1.dat
else
    echo " "
    echo "You have selected  $tmg_file for GRAPHTAB parameter"
    if (! -e tmp.dat) then
         echo 'no tmp.dat file to modify'
         set tmg = 0
    else
        set ihst = 0
        awk -v v1=$tmg_file ' \
            { if (index($0, "hst") != 0) {\
                  v2 = $0 ;\
                  ihst = 1; \
                  print v2 } \
               else if (index($0, "graphtable") != 0 && ihst == 1) {\
                  gsub(/[ \t]+$/,"", v1); \
                  print "        " $1, "\x27$PYSYN_CDBS/mtab/" v1 "\x27," } \
               else if (index($0, "jwst") != 0) { \
                  v2 = $0 ;\
                  ihst = 2; \
                  print v2 }     \
               else \
                  print $0 \
           }' tmp.dat > tmp1.dat
           set tmg = 1
    endif    
endif

if ($tmt_file == "") then
    echo " "
    echo "No TMT fila name given, please give me the name of the last"
    echo " TMT file in the /grp/hst/cdbs/mtab/ directory: "
    set tmt_file = $<
    cp -f $dircdbs/mtab/$tmt_file $dir1/mtab/$tmt_file
    echo " "
    set tmt = 1
    echo "the TMT file is $tmt_file"
endif

if ($tmt_file == "") then
    echo " " 
    echo "No TMT file name given. Exiting program"
    set tmt = 0
    exit 1
    cp -f tmp1.dat tmp2.dat
else
    echo " "
    echo "You have selected  $tmt_file for THERMTAB parameter" 
    if (! -e tmp1.dat) then
         echo 'In TMT step: no tmp1.dat file to modify'
         set tmt = 0
    else
        set ihst = 0
        awk -v v1=$tmt_file ' \
            { if (index($0, "hst") != 0) {\
                  v2 = $0 ;\
                  ihst = 1; \
                  print v2 } \
               else if (index($0, "thermtable") != 0 && ihst == 1) {\
                  gsub(/[ \t]+$/,"", v1); \
                  print "        " $1, "\x27$PYSYN_CDBS/mtab/" v1 "\x27," } \
               else if (index($0, "jwst") != 0) { \
                  v2 = $0 ;\
                  ihst = 2; \
                  print v2 }     \
               else \
                  print $0 \
           }' tmp1.dat > tmp2.dat
           set tmt = 1
    endif
endif

echo "writing information to pysyn_lookup.dat file"
cp -f tmp2.dat ./pysyn_lookup.dat
mv ./pysyn_lookup.dat $dir1/etc/.
python /grp/hst/cdbs/deliveries/scripts/read_dict.py $dir1/etc/mjd.dat 
python /grp/hst/cdbs/deliveries/scripts/read_dict.py $dir1/etc/pysyn_lookup.dat 

if ( -e tmp.dat) then
    rm -f tmp.dat
endif
if ( -e tmp1.dat) then
    rm -f tmp1.dat
endif
if ( -e tmp2.dat) then
    rm -f tmp2.dat
endif

#cp $dircdbs/etc/pysyn_lookup.dat $dir1/etc/.

cd $dir1/mtab/
echo $tmc_file
if ($tmc_file != "") then
    python /grp/hst/cdbs/deliveries/scripts/tmc_tmt_delivery.py $tmc_file $1 outfile_tmc
    python /grp/hst/cdbs/deliveries/scripts/tmc_tmt_delivery.py $tmt_file $1 outfile_tmt
    source outfile_tmc
    source outfile_tmt
    /bin/rm -f outfile_*
    cd ../..
else
    echo "no TMT or TMC files given. Cannot copy Synphot and Thermal files to the comp directory"
endif
done:
    exit 0
error:
    exit 1
