#!/bin/csh
#########################################################################
# launch_prod
#
# This script checks for an active LDM at each site, then puts the 
# product file in the "outgoing" directory for the LDM.
#
# Input:
#	$1	The type of product
#		    Valid types are:
#			text
#			graphic
#			grib
#			vgf
#
#	$2	The complete path for the file to send
#		There can be no colons (:) in the path
#		    text 	the filename portion must be the AWIPS
#			   	identifier of the form CCCCNNNXXX,
#		    	   	e.g., KWNHTAV10.
#		    graphic	No special instructions
#		    grib	No special instructions
#		    vgf		No special instructions
#		    
##
# Log:
# S. Jacobs	4/00	Created
# S. Jacobs	5/00	Updated all centers information
# S. Jacobs	5/00	Added mailing of problems to administrator
# M. Klein     12/09    Change path to search for IFCP_npds code
#                       and log launch_prod activity.
# M. Klein     01/10    Modify to use another version of IFCP_npds.
# M. Klein     01/14/10 Rename IFCP to original executable name
# A. Robson    02/19/10 Changed IP address to $NETAPPIP
# M. Klein     02/14/12 Add PDS attached to OPCN. 
# M. Klein     07/02/12 If running at NCWCP, use different IP for OPCN.
# M. Klein     07/16/12 Change SPC to launch through SPCN.
# A. Robson    02/05/12 Added HPCN NCWCP pds
# M. Klein     04/15/13 Changed TPC to NHCN for AWIPS 2 upgrade
#########################################################################

# Get exact start time of launch_prod.
set start_launch = `date -u +"%Y%m%d %H:%M:%S"`

# Notes about logging launch_prod activity:
#  1. Logs will be initially written locally, then appended to the main daily log on NCOSRV.
#     The main daily logs are located in /export-4/$NETAPPIP/hpcops/launch_logs.
#  3. Logs are written locally to the directory set to $DBNROOT.  This will be 
#     $HOME/.launch_log/log and will be recreated each time launch_prod runs.

# Make sure to call the IFCP_npds program that PMB maintains, not one in HPC's bin directories.
# The executable is located in /export-4/$NETAPPIP/ncoops on NCOSRV.
setenv IFCP_DIR   /export-4/ncosrvnfs-cp/ncoops

# Set up a directory locally to write the log file.
# Note that the logging program in IFCP will ALWAYS put the log file entry in a "log" directory, so 
# there must be present a "log" directory under $DBNROOT for this to work.  See the setenv below.
setenv LOGDIR     ${HOME}/.launch_logs
set logfile = ${LOGDIR}/launch_log_local.txt

# Set environment variable for central log directory on NCOSRV
setenv CENTRAL_LOG  /export-4/$NETAPPIP/hpcops/launch_logs

rm -Rf ${LOGDIR}
mkdir ${LOGDIR}

set prod_sent = 0
set status = 0
set primary = 0
set ldmname = 0

# There are 2 arguments required for launch_prod.
# ${1} -- The type of file being sent. (e.g. text, vgf, graphic, etc...)
# ${2} -- The actual file being sent.

set full = $2
set name = `basename $2`

# Check each site for an active LDM.
# Change this list to have the local site first.
# Note: As of 12/14/09, AWC has 2 valid LDM directories, /awc/ops/ldmpds and /awc/ops/ldmpds2

#foreach site (  OPCN AWC NHCN SPC )
foreach site ( HPCN OPCN SPC AWC NHCN )
#foreach site ( SPC AWC NHCN OPCN  )
#foreach site ( AWC SPC OPCN NHCN )
    if  ( $site == "WWB" )  then
	set ipadd  = "140.90.103.28"
	#set ipadd  = "140.90.103.288"
	set ldmdir = "/usr1/ldmaw"
        #set ldmdir = "/usr1/error"
        set ldmname = "HPC Product Distribution System"
	set primary = 0
    else if ( $site == "OPCN" ) then
	set ipadd = "140.90.100.89"
	set ldmdir = "/usr1/ldm"
	set ldmname = "OPCN AWIPS 2 Product Distribution System"
	set primary = 1
    else if ( $site == "HPCN" ) then
        set ipadd = "140.90.100.88"
        set ldmdir = "/usr1/ldm"
        set ldmname = "HPCN AWIPS 2 Product Distribution System"
        set primary = 1
    else if  ( $site == "AWC" )  then
	set ipadd  = "172.16.251.90"
	set ldmdir = "/awc/ops/ldmpds"
        set ldmname = "AWC Product Distribution System"
	set primary = 0
    else if  ( $site == "SPC" )  then
 	set ipadd  = "140.90.173.226"
	set ldmdir = "/users/ldm"
#       set ipadd  = "checkov.spc.noaa.gov"
	#set ldmdir = "/home/ldmcheckov"
        set ldmname = "SPC Product Distribution System"
	set primary = 0
    else if  ( $site == "NHCN" )  then
	set ipadd  = "140.90.176.119"
	set ldmdir = "/usr1/ldm"
        set ldmname = "NHC Product Distribution System"
	set primary = 0
    endif

    
    # Use IFCP to check the status of the LDM inside the AWIPS firewall.
    # The script has been tested for incorrect/unavailable entries of $ldmdir and $ipadd.
    #   a. If the $ldmdir is incorrect, the return code from the IFCP program will
    #      be 1.
    #   b. If the IP address is incorrect or unavailable, it'll return a status of 1.
    #
    # If the return status is 1, go to the next site to send out product

    set fullddate = `date -u "+%Y%m%d"`
    set currdatetime = `date -u "+%H:%MZ %Y%m%d"`

    ${IFCP_DIR}/IFCP_npds -1 fork -B ${ldmdir}/ncep_bin/check_ldm -r ${ipadd} >& /dev/null

    # If the return status from the LDM check is 0, attempt to launch the file.
    # and check status of the send. The IFCP program copies the file to the "outgoing" 
    # directory on lnx349. 

    if  ( $status == 0 )  then
        ${IFCP_DIR}/IFCP_npds -m ${full} -p ${ldmdir}/data/outgoing/${name}:${1} -r ${ipadd}

        # If the return status from the IFCP command is 0, check the log file for errors before indicating
        # success. Am checking for the text "file sent successfully".
        if ( ${status} == 0 ) then
            echo "Product successfully launched via the ${ldmname}"
            echo "Product successfully launched via the ${ldmname}" >> ${logfile}
	    echo "Product: ${name}     Delivery time: ${currdatetime}" >> ${logfile}
	    set prod_sent = 1
	    break
        else
            echo "There is an error sending ${name} via the ${ldmname}.  Try the next PDS."
	    echo "There is an error sending ${name} via the ${ldmname}.  Try the next PDS." >> ${logfile}
        endif
    else 
        echo "There is a problem accessing the ${ldmname}.  Trying the next PDS."
	echo "LDM check of site ${site} failed.  Trying the next PDS." >> ${logfile}
    endif
end

set end_launch = `date -u +"%Y%m%d %H:%M:%S"`

# If all sites are down, there is a serious problem.
if  ( $prod_sent == 0 )  then
    echo " "
    echo " Your product has not been distributed."
    echo " "
    echo " There is a serious problem. Contact DTB or the SDM."
    echo " "
    echo "Error: Product ${name} NOT DELIVERED" >> ${logfile}
    set exitstatus = 1 
else 
    set exitstatus = 0
endif

echo "Start of launch: ${start_launch} -- end of launch: ${end_launch}" >> ${logfile}
repeat 2 echo " " >> ${logfile}

# Append the log file from the local .launch_log directory to that on the central location
# which is: /export-4/$NETAPPIP/hpcops/launch_logs/
if ( -e ${CENTRAL_LOG}/launch_log.${fullddate} ) then
    cat ${logfile} >> ${CENTRAL_LOG}/launch_log.${fullddate}
else
    mkdir -p ${CENTRAL_LOG}
    cat ${logfile} > ${CENTRAL_LOG}/launch_log.${fullddate}
    chmod -f 777 ${CENTRAL_LOG}/launch_log.${fullddate}
endif

exit ${exitstatus}
