#!/bin/bash
#
#  Update the catalogs from their constituent parts.
#  Use "source ./Update" if $SCHED has been set to other than its
#  login value.
#
cd $SCHED/catalogs
echo Working on catalogs in $SCHED

echo Delete the old catalogs.
chmod u+w stations.dat
chmod u+w freq.dat
chmod u+w messages.txt
chmod u+w locations.dat
chmod u+w peak.cmd
chmod u+w peak_RDBE_DDC.cmd
chmod u+w peak_RDBE_PFB.cmd

/bin/rm stations.dat
/bin/rm freq.dat
/bin/rm messages.txt
/bin/rm -f blankline
/bin/rm version
/bin/rm locations.dat
/bin/rm freq_RDBE.dat
/bin/rm stations_RDBE.dat
/bin/rm peak.cmd
/bin/rm peak_RDBE_PFB.cmd
/bin/rm peak_RDBE_DDC.cmd

# Make a line to indicate the date the catalog was
# assembled. 

cat <<EOF > version

!  --  This file created by Update.  Do not edit.  --
!  VERSION is the date and time when this catalog was
!  assembled from the master subcatalogs.
 
 VERSION = "`LC_ALL=en_EN.utf8 date -u +%Y%b%d-%T`"

EOF

echo Make a new Master_stations file.
cat Master_NRAO/stations_top.dat \
    version \
    Master_NRAO/stations_US.dat \
    Master_JIVE/stations_EVN.dat \
    Master_NRAO/stations_APT.dat \
    Master_NRAO/stations_DSN.dat \
    Master_ATNF/stations_ATNF.dat \
    Master_JIVE/stations_KVN.dat \
    Master_NRAO/stations_mm.dat \
    Master_NRAO/stations_geodesy.dat \
       > stations.dat


echo Make the new stations_RDBE.dat file.
cat Master_NRAO/stations_RDBE_top.dat \
    version \
    Master_NRAO/stations_RDBE_US.dat \
    Master_JIVE/stations_EVN.dat \
    Master_NRAO/stations_APT.dat \
    Master_NRAO/stations_DSN.dat \
    Master_ATNF/stations_ATNF.dat \
    Master_JIVE/stations_KVN.dat \
    Master_NRAO/stations_mm.dat \
    Master_NRAO/stations_geodesy.dat \
       > stations_RDBE.dat


echo Make the new freq.dat file.
cat Master_NRAO/freq_top.dat \
    version \
    Master_NRAO/freq_VLBA.dat \
    Master_NRAO/freq_VLA.dat \
    Master_NRAO/freq_GB.dat \
    Master_NRAO/freq_Arecibo.dat \
    Master_JIVE/freq_EVN.dat \
    Master_NRAO/freq_APT.dat \
    Master_NRAO/freq_DSN.dat \
    Master_ATNF/freq_ATNF.dat \
    Master_NRAO/freq_mm.dat \
    Master_NRAO/freq_geodesy.dat \
       > freq.dat

echo Make the new freq_RDBE.dat file.
#  Took out:     Master_NRAO/freq_RDBE_EB.dat \

cat Master_NRAO/freq_RDBE_top.dat \
    version \
    Master_NRAO/freq_RDBE_VLBA.dat \
    Master_NRAO/freq_RDBE_GB.dat \
    Master_ATNF/freq_ATNF.dat \
    Master_NRAO/freq_Arecibo.dat \
    Master_JIVE/freq_EVN.dat \
    Master_NRAO/freq_APT.dat \
    Master_NRAO/freq_DSN.dat \
    Master_NRAO/freq_mm.dat \
    Master_NRAO/freq_geodesy.dat \
      > freq_RDBE.dat


echo Make the new locations.dat file.
echo   \*\*\*\*  Be sure to update the symlink Master_NRAO/locations.gsfc
cat Master_NRAO/locations.top \
    version \
    Master_NRAO/locations.gsfc \
    Master_NRAO/locations_VLA.dat \
    Master_NRAO/locations_DSN.dat \
    Master_NRAO/locations_extras.dat \
    Master_ATNF/locations_ATNF.dat \
    Master_JIVE/locations_KVN.dat \
       > locations.dat

echo "   " > blankline
echo Make the new messages.txt file.
cat Master_NRAO/messages_NRAO.txt \
    blankline \
    Master_JIVE/messages_VEX.txt \
    blankline \
       > messages.txt
echo ++end >> messages.txt

#  Auto reference pointing control files.

echo Make the new peakxxx.cmd files
sed s:zzzz:pt7mm.set:  Master_NRAO/peak.cmd | \
sed s:lzzz:pt7mm.set:  >  peak.cmd

sed s:zzzz:ptd7mm.set:  Master_NRAO/peak.cmd | \
sed s:lzzz:ptdl7mm.set:  > peak_RDBE_DDC.cmd

sed s:zzzz:ptr7mm.set:  Master_NRAO/peak.cmd | \
sed s:lzzz:ptr7mm.set:   > peak_RDBE_PFB.cmd


echo 
echo Prohibit write on the new catalogs.
chmod u-w stations.dat
chmod u-w freq.dat
chmod u-w messages.txt
chmod u-w locations.dat
chmod u-w peak.cmd
chmod u-w peak_RDBE_DDC.cmd
chmod u-w peak_RDBE_PFB.cmd

/bin/rm blankline
