#! /usr/bin/sh
set -xva
# ##############################################################
# 
# Script name:  exjobviews.sh.sms 
# Script description:  This script sets up the jobview program.
#
# Author:   David Ma         Org: NP11       Date: 2004-12-04
#
# Abstract: It generates all environment variables, and data files.  
#           It also sends the HTML page to the web server. 
#
# Script history log:
# 2004-12-05  Turned on the Node count collection.
# 2004-12-04  Modified to run on blue and white machines.
# 2003-03-15  Implemented the node count collection.
# 2002-10-11  Implemented into production on IBM
# 2003-02-25  Added the checking of the msg flag from jobview_init
# 2003-02-27  Added the counting of jobs.  It makes sure 
#             does not miss any jobs.
# 2005-11-13  Added the node count to the LastSMSJobs.dat file, also
#             changed the collection of node count with a more
#             robust processes.
#  
################################################################
i=0
webpath="/com/web/prod/ptmp/web/sendweb/"
rzdmpath="/home/people/nco/nwprod/nwpara/JobView"
qmach=`hostname | cut -c 1`
ppgrm="${ushscript}/mkbar_wt.pl"
ddat="temp16"${qmach}
lddat="temp17"${qmach}
smain="${ushscript}/jobview_init"
sjobs="${ushscript}/jobview_run"
stimes="${ushscript}/jobview_out"
JobHist="${COMIN}/LastSMSJobs.dat"
IDLEjobs="idle_jobs"
IDLEjobsbkup="idle_jobs_bkup"
JHlist=`ls -l ${JobHist}`
JHmonth=`echo ${JHlist} | cut -d " " -f 6`
JHday=`echo ${JHlist} | cut -d " " -f 7`
JHtime=`echo ${JHlist} | cut -d " " -f 8`
htmldat="jobview"${qmach}".html"
llqjobs=${utilexec}/"lljobs"
GetJobs=${utilexec}/"getjobs"
# webhost="lnx321.ncep.noaa.gov"
webhost="rzdm.ncep.noaa.gov"
jobsnow="jobs_now${qmach}"
jobsrun="jobs_running${qmach}"
lqjobview="${ushscript}/lqjob_view"
lqclasscount="llclasscount.log"
jfile="jobs_running${qmach}"
jhtml="jobs_now${qmach}.html"
tfile="ttsdd${qmach}"
Qmach="${envir}"
node_file="Node_file"
old_node_file="Old_Node_file"
rm -f idle_jobs_bkup

################################################################
smsrunlog="SMS_RUN_LOG"
tPDYm1=`${utilscript}/finddate.sh $PDY s-1`
tPDY12=`${utilscript}/finddate.sh $PDY s-12 | cut -d " " -f 12 `
tPDY11=`${utilscript}/finddate.sh $PDY s-11 | cut -d " " -f 11 `
tPDY10=`${utilscript}/finddate.sh $PDY s-10 | cut -d " " -f 10 `
rm -f ${com}/${smsrunlog}/SMS${tPDY10}.dat ${com}/${smsrunlog}/SMS${tPDY11}.dat ${com}/${smsrunlog}/SMS${tPDY12}.dat

cd ${DATA}
#

export utilscript
export utilexec tfile Qmach
export ushscript jobsnow jobsrun lddat jhtml jfile JobHist
export COMIN COMOUT llqjobs GetJobs
export DATA ddat sjobs stimes smain JobHist qmach
export qhr qmm qsec IDLEjobs IDLEjobsbkup node_file old_node_file
echo
cp ${webpath}${ddat} ${lddat}
cp ${webpath}${node_file} ${old_node_file}

qtime=`date -u`
qhr=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 1`
qmm=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 2`
qsec=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 3`
((qjtime=$qhr*3600+$qmm*60+$qsec))
cp ${webpath}${IDLEjobs} .
touch ${IDLEjobsbkup}
cat ${IDLEjobs}
msg="JOBVIEW - Start jobview program $qtime $qjtime"
sh $utilscript/postmsg.sh "$jlogfile" "$msg"
date -u > ${ddat}
echo "" > ttsai
  ${smain} > errfile
cat ${ddat}
#notake=`grep TAKE ttsai`
#notake=${notake:-""}
#if [ "$notake" != "" ]
#then
 cp ttsai ${COMOUT}/ttsai
#fi
cat ttsai
qtime=`date -u`
msg="JOBVIEW - After jobview program "
sh $utilscript/postmsg.sh "$jlogfile" "$msg $qtime"
${lqjobview}
qtime=`date -u`
msg="LQJOB_VIEW"
sh $utilscript/postmsg.sh "$jlogfile" "JOBVIEW - Finished getting all current jobs ran last time  $msg $qtime "
#####################################################3
#cat lljob.log | egrep "climprod|nwprod" |
#while read line
#do
#  outline=`echo $line | awk '{print $10}'`
#echo "output file $outline"
#  outfile=`echo $line | awk '{print $2}'`
#  if [ -s $outline ]
#  then
#    egrep "ncep.noaa.gov|pid" $outline > $outfile
#    cp $outfile ${webpath}
#echo "cp file to $webpath"
#  fi
#done

###################################################3#
qtime=`date -u`
msg=`perl ${ppgrm} ${ddat} ${htmldat} ${JHmonth}","${JHday}" "${JHtime} `
sh $utilscript/postmsg.sh "$jlogfile" "JOBVIEW - Created Webpage at  $msg $qtime "

cp ${jhtml} ${webpath}${jhtml}
cp ${htmldat} ${webpath}${htmldat}


if [ "$SENDWEB" = "YES" ]
then
  ping -c 2 ${webhost}
  pingerr=$?

  qtime=`date -u`
  msg="JOBVIEW - Ping Returned Status $pingerr"
  sh $utilscript/postmsg.sh "$jlogfile" "$msg $qtime"

  if [ $pingerr -eq 0 ]
  then
     scp ${webpath}* nwprod@${webhost}:${rzdmpath}/.
  fi
fi
qtime=`date -u`
msg="JOBVIEW - Webpage moved to webserver $qtime"
sh $utilscript/postmsg.sh "$jlogfile" "$msg "
grep 20001 ${ddat} >> $jlogfile
# grep 20001 ${ddat} >> /u/wx11mm/testing
#
jobcount=`cat errfile1`
jobdone=`cat ${ddat} | wc -l`
cat errfile1
echo "jobcount and jobdone $jobcount $jobdone"
if [ $jobcount -eq ${jobdone}-1 ]
then
  errmsg="0 job missing"
else
  ((jobdif=${jobcount}-${jobdone}+1))
  cat ${ddat}
  echo "-----------------------------------------"
  cat lljobtmp
  errmsg="$jobdif jobs missing"
fi
#####################################################
#####################################################
  cat /dev/null > tmp_node_file
  cat ${node_file} ${old_node_file} >> tmp_node_file
  sort -k1 tmp_node_file | uniq > ${old_node_file}
  rm -f tmp_node_file
##########################################################
cp ${jobsrun} ${COMOUT}/${jobsrun}
cp ${jobsnow} ${COMOUT}/${jobsnow}
#cp ${jhtml} ${webpath}${jhtml}
cp ${tfile} ${COMOUT}/${tfile}
cat ${ddat} | sort -k 1 >  ${COMOUT}/${ddat}
#cp ${htmldat} ${webpath}/${htmldat}
cp ${IDLEjobsbkup} ${COMOUT}/${IDLEjobs}
cp ${lqclasscount} ${COMOUT}/${lqclasscount}
cp lljob.log ${COMOUT}/.
cp lljob.log ${COMOUT}/log_dir/.
lljobfile=`ls -1rt ${COMOUT}/log_dir/lljob*.log.gz | head -1 | sed "s/.gz//g" ` 
cp lljob.log ${lljobfile}
gzip -f ${lljobfile}
cp lljobtmp.lst ${COMOUT}/.
###############################################
ls -rlt
cp ${old_node_file} ${COMOUT}/${node_file}
cp tmp_node_file ${COMOUT}/tmp_node_file
cp ${COMOUT}/dd ${COMOUT}/${IDLEjobs}
cp ttsai ${COMOUT}/ttsai
###############################################
cat ${COMOUT}/${IDLEjobs}
ls -rlt ${COMOUT}
qtime=`date -u`
qhr=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 1`
qmm=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 2`
qsec=`echo $qtime | cut -d " " -f 4 | cut -d ":" -f 3`
((qejtime=$qhr*3600+$qmm*60+$qsec))
((qjtime=$qejtime-$qjtime))
msg="JOBVIEW - Webpage copied to storage $qtime $qejtime $qjtime ERROR:  $errmsg"
sh $utilscript/postmsg.sh "$jlogfile" "$msg "

chmod 755 ${COMOUT}/${htmldat} ${COMOUT}/${ddat}

# clean up the log file if it is getting too big
jlog=`ls -lrt $jlogfile`
jsize=`echo $jlog | cut -d " " -f 5`
if [ $jsize -gt 2500000 ]
then
  rm -f $jlogfile
  touch $jlogfile
fi
