#!/bin/sh

################################################################
# Creates Web Index Files then posts them to web server
# Does this for the Homeland Security Hysplit Runs
#
# 18 March 2008 - PDF, kmz (Google Earth)
################################################################
set -x

if [ "$run_type" = "on_demand" ]
then
   export org=${org:-sdm}
   export PDY=`${USHhysplit}/hiresw_get_hls_nest.sh | awk '{print $2}'`
   export cyc=`${USHhysplit}/hiresw_get_hls_nest.sh | awk '{print $3}'`
   export run_id=`${USHhysplit}/hiresw_get_hls_nest.sh | awk '{print $4}'`
   export MODEL=`${USHhysplit}/hiresw_get_hls_nest.sh | awk '{print $5}'`
else
   export run_id="*"
fi

##############################################################
# Make html index files
##############################################################
${USHhysplit}/hysplit_web_index_hls.sh $run_type

##############################################################
# Send output to web (Silver Spring and Boulder)
##############################################################

${USHhysplit}/hysplit_canned_send_web.sh $MACHINE_SSMC &
ssmc_pid=$!
${USHhysplit}/hysplit_canned_send_web.sh $MACHINE_BLDR &
bldr_pid=$!
wait $ssmc_pid
errsum_ssmc=$?
wait $bldr_pid
errsum_bldr=$?

if [ "$errsum_bldr" -ne '0' ]
then
  set +x
  echo "*******************************************************************"
  echo "*************************** FATAL ERROR ***************************"
  echo "*HYSPLIT output DID NOT mirror successfully to BOULDER ($MACHINE_BLDR)*" 
  echo "*                   Set this job to complete and                  *"
  echo "*  Run '/utility/jhyspt_recover' when the server is back online   *"
  echo "*   Contact the WOC for server availability problems/questions    *"
  echo "*******************************************************************"
  set -x
else
  echo "On Demand HYSPLIT successfully mirrored to BLDR ($MACHINE_BLDR)"
  if [ "$SENDSMS" == "YES" ]
  then
     $SMSBIN/setev bldr
  fi
fi

if [ "$errsum_ssmc" -ne '0' ]
then
  set +x
  echo "*******************************************************************"
  echo "*************************** FATAL ERROR ***************************"
  echo "*HYSPLIT output DID NOT mirror successfully to SSMC ($MACHINE_SSMC)   *"
  echo "*                   Set this job to complete and                  *"
  echo "*  Run '/utility/jhyspt_recover' when the server is back online   *"
  echo "*   Contact the WOC for server availability problems/questions    *"
  echo "*******************************************************************"
  set -x
else
  echo "On Demand HYSPLIT successfully mirrored to SSMC ($MACHINE_SSMC)"
  if [ "$SENDSMS" == "YES" ]
  then
     $SMSBIN/setev ssmc
  fi
fi

if [ "$errsum_ssmc" -ne '0' ] || [ "$errsum_bldr" -ne '0' ]; then
  let err=errsum_ssmc+errsum_bldr
  export err; err_chk;
fi
exit
