#!/bin/sh -vx
#
#  This script is EXECed directly by DBNet in order to run the
#  dcmap decoder on the data file given in the first argument.
#
#    Usage: ./run_dcmap.sh <map_filename>
#
#  Once this is done, the data file itself is then compressed
#  within its native directory for more efficient short-term
#  storage.
#

# The gzip line must be the first, noncomment line in this script
# so that stdin is processed correctly
exec >> /iodprod/dbnet/ldm/logs/dcmap.log 2>&1
echo "START -- PID=" $$ " ----" `date` "----------------------------"

gzip -d > ${1}/$$.${2}
madisfilename=${1}/`echo ${2} | cut -c1-13`
hhmm=`date -u +%H%M` 
decoderfilename=${madisfilename}.${hhmm}
mv ${1}/$$.${2} ${decoderfilename}

. /iodprod/dbnet/.profile

export MADIS_STATIC=/nwprod/lib/sorc/madis/static
export MADIS_DATA=/dcom/us007003/ldmdata

ln -sf ${decoderfilename} ${madisfilename}

nice /nwprod/exec/decod_dcmap -v 2 -d /dcom/us007003/decoder_logs/decod_dcmap.log  ${decoderfilename} /nwprod/fix/bufrtab.002

rm -f ${madisfilename}

#
#  Compress the decoder input file within its native directory,
#  in order to conserve disk space for these large files!
#

gzip ${decoderfilename}

#
#  Explicitly set the script return code to 0, in order to prevent
#  the "compress" return code from becoming the script return code
#  (and thereby prevent DBNet from re-running the script for this
#  particular data file if there is a problem with the compress!)
#

echo "END ---- PID=" $$ " ----" `date` "----------------------------"
exit 0
