#!/bin/sh
USAGE="usage: foreign_fillafos.sh filename "
VERS="${USHutil}/${member} 1999-12-15 09:25L"
#
#______________________________________________________________________________
# NAME     : foreign_fillafos.sh
# Author   : Peter Henrichsen
# Purpose  : This script runs the fillafos program to reblock the afos product
#          : file that was created on a foreign workstation. Then 
#          : post the reblocked AFOS graphic file.
#
# History  : 1999-06-30 Peter Henrichsen
#          : 1999-12-15 Peter Henrichsen modify to use USHutil and to check
#          :  sndfrc from snd2forgn.
#
# Location : This script is found on hp36 as:
#          : /tmp_mnt/export/sgi73/peterhen/util/scripts/ibm/
#            foreign_fillafos.sh
#          : & on ncosp as          
#          : /nwprod/util/ush/foreign_fillafos.sh or
#          : /nfsuser/g01/wx12ph/util/ush/foreign_fillafos.sh           
#
# Usage    : This script is called when an AFOS file has been created on
#            a workstation and is to be sent.
#
#            The AFOS file is passed as the first argument (full path). 
#            
#                Arg1 is afosfile fullpath                   
#  AN EXAMPLE:
#
# foreign_filafos.sh  /tmpuser/dbnprd/fnafos/NMCGPHL8P  
#       where:
#	      "/tmpuser/dbnprd/fnafos/NMCGPHL8" is the afos file full-path.
#           
#
#
#  Remarks : This script assumes that the following values have been exported
#            by the parent shell:
#            
#            DATA EXECutil envir JOB SENDCOM SENDDBN 
#            jlogfile model pcom pgmout utilities 
#             
#
#  Note    : If you ARE NOT running as dbnprd and are testing
#            this script: Then set envir=test and SENDCOM=NO 
#            in the parent job.
#            If you ARE running as dbnprd and are testing
#            this script: Then set envir=prod and SENDCOM=NO
#            in the parent job.                     
# 
#_______________________________________________________________________________
cd $DATA

print "$USAGE"
print "$VERS"    
msg="$member HAS STARTED!"
postmsg "$jlogfile" "$msg"
integer   istat
INFILE="$1"
export filename=`basename ${INFILE}`
export afosout=`echo ${filename} | awk 'BEGIN{FS="."} {print $1$2}'` 

pgm=fillafos

export pgm; prep_step
export OUTFILE=$afosout.$pid
export LOGMSG=${PIL}msg.$pid
export XLFRTEOPTS="unit_vars=yes"
set -x

#input files

export XLFUNIT_11="$INFILE" # the input afos file to block.

# output files

export XLFUNIT_62="$DATA/$OUTFILE"  # completed blocked afos file.

export XLFUNIT_75="$DATA/$LOGMSG" #the consol message file. 
   
      
#____________________________________________________________________
#
#    now run the fillafos program !
#
#____________________________________________________________________

startmsg
$EXECutil/fillafos  2>> errfile
istat=$err 
echo fillafos: Ended with return code = $istat
if test "$istat" = 0
then
 err=$?;export err; err_chk
 

 echo fillafos: Ended with return code = $istat
    
 kinpil=$PILNUM  
 filkey=FILLED
 export filkey
 grep $filkey $DATA/$LOGMSG >> $PIL.$$.line
   if [ -s $PIL.$$.line ]
   then
      jinpil=`awk '{print $4}' $PIL.$$.line`
   
      if test "${jinpil}" = "${kinpil}"
      then 
       inpil=$jinpil   
      else
       inpil=$kinpil             
      fi

#       get description of this afos product for the jlogfile.

        export  amsg1=`egrep  ${inpil} $FIXutil/forgnpil.dat \
           |  cut -f2 -d","`  
  
      if test "$envir" = 'prod'
      then

       if test "$SENDCOM" = 'YES' 
       then
        SENDNAME=afos.${inpil}.$USR
#
#       convert SENDNAME to "lower case".
#
        export sendname=`echo  $SENDNAME | tr '[A-Z]' '[a-z]' `
     
        path=$pcom/afos 
        mkdir -m 775 -p  $path >/dev/null 2>&1
          
        cp $DATA/$OUTFILE $path/$sendname
              
        rm $DATA/$OUTFILE

#______________________________________________________________
#    start the procedure to send the  AFOS product.
#______________________________________________________________
    
         if test "$SENDDBN" = 'YES'    
         then
          export  KEY=AFOS
          export  MODEL=$model
          
          sndfrc=0
           echo $sndfrc >$DATA/sndfrc 
 
 
          $USHutil/snd2forgn \
           $KEY $MODEL $job $path/$sendname
           
           read sndfrc < $DATA/sndfrc
                
           if test $sndfrc -eq '0'
           then
             
            msg1="Afosmap, $inpil $amsg1, WAS POSTED!" 
            postmsg "$jlogfile" "$msg1"
            msg="$member HAS COMPLETED NORMALLY!"
           else
              msg="ERROR AFOS MAP $amsg1 NOT POSTED!"
              postmsg "$jlogfile" "$msg"
              msg="${pgm}: ABNORMAL STOP = $sndfrc!:" 
           fi
            rm  $DATA/sndfrc                   
         else
          msg="$member testing SENDDBN = ${SENDDBN}, $inpil not sent!"    
         fi
       else
        msg="$member testing SENDCOM = ${SENDCOM}, $inpil not sent!"       
       fi
      else
       msg="$member testing envir = ${envir}, $inpil not sent!"     
      fi
   else
    echo filkey $filkey is not in the $PIL.$$.line

    msg="ERROR from $pgm, $PIL not $filkey or SENT !"
   fi
else
   msg="${pgm}: ABNORMAL STOP BLOCKING $PIL, STOP = $istat!"
  
fi   
rm $PIL.$$.line
rm  $DATA/$LOGMSG  
postmsg "$jlogfile" "$msg"



 
