#!/bin/ksh
# date_ps_ef.ksh
# Report on world.php or filename as argument 1

mimetype="Content-Type: text/html\\n"
emailfrom="rmetcalfe@rjmprogramming.com.au"
emailto="rmetcalfe15@gmail.com"
emailcc="rmetcalfe@rjmprogramming.com.au"
emailbcc=""
subjmiddle="Date ps-ef world.php Report"
lastdate="`date`"
subjis="First ${subjmiddle} at ${lastdate}"
lastcur=""
filen=""
flist=""
prefix="$0"
ppid=""
plain="plain"
hprefix=""
hsuffix=""

attachrest="\\n<html><head><style> tr:nth-child(even) {background-color: #CCC !important;} tr:nth-child(odd) {background-color: #FFF !important;} </style></head><body><h1>First ${subjmiddle} at ${lastdate}</h1><table style='border:5px dotted olive;width:70%;height:100%;margin: 5 5 5 5;font-family:Courier-New;'><tr><th>UID</th><th>PID</th><th>PPID</th><th>C</th><th>STIME</th><th>TTY</th><th>TIME</th><th>CMD</th></tr>`cat date_ps_ef_first.txt | sed '/^/s//<tr><td>/g' | sed 's/ \{1,\}/<\/td><td>/g' | awk '{ t=0; while (getline \$0) { if (t == 0) { print \"<tr style=background-color:#e7e7e7 !important;\" substr(\$0,3);  } else { print \$0; } t=1-t; } }' | sed '/$/s//<\/td><\/tr>/g'`</table></body></html>"
if [ ! -z "$1" ]; then
 while [ ! -z "$1" ]; do
  if [ "`echo \"${1}\" | tr -d '@'`" != "`echo \"${1}\"" ]; then
   if [ "`echo \"${emailto}\" | tr -d ' '`" == "`echo \"${emailto}\"" ]; then
     emailto="${1} "
   elif [ "`echo \"${emailcc}\" | tr -d ' '`" == "`echo \"${emailcc}\"" ]; then
     emailcc="${1} "
   else
     emailbcc="BCC: ${1}\\n"
   fi
   prefix="${prefix} ${1}"
  elif [ -f "${1}" ]; then
   if [ ! -z "${mimetype}" ]; then
    filen="${1}"
    mimetype=""
    shortf=""
    basechars="`base64 ${filen} | tr -d \"\\n\"`"
    i=0
    flast="`echo ${1} | tr '/' ' ' | sed '/\\\\/s// /g'`"
    for filebits in ${flast}; do
        shortf=${filebits}
        ((i=i+1))
    done
    subjmiddle="$shortf"
    if [ "`echo \"${subjis}\" | sed '/^First /s///g'`" != "`echo \"${subjis}\"`" ]; then
        subjis="First ${subjmiddle} at ${lastdate}"
    fi
    attachrest="Mime-Version: 1.0\\nContent-Type: multipart/mixed; boundary=\"19032019ABCDE\"\\n\\n--19032019ABCDE\\nContent-Type: text/${plain}; charset=\"iso-8859-1\"\\nContent-Transfer-Encoding: 8bit\\n\\n${hprefix}${subjis}${hsuffix}\\n\\n--19032019ABCDE\\nContent-Type: application/octet-stream; name=\"${shortf}\";\\nContent-Transfer-Encoding: base64\\nContent-Disposition: attachment;filename=\"${shortf}\"\\n\\n${basechars}\\n--19032019ABCDE--"
   elif [ -z "${flist}" ]; then
    ppid="$$"
    prefix="${prefix} ${ppid}"
    flist="${1}"
   else
    flist="${flist} ${1}"
   fi
  elif [ "`echo \"${1}\" | tr '[A-Z]' '[a-z]' | sed '/^http/s///g'`" != "`echo \"${1}\" | tr '[A-Z]' '[a-z]'" ]; then
   if [ ! -z "${mimetype}" ]; then
    lastcur=" "
    filen="`echo \"${1}\" | sed '/^https:/s//http:/g' | sed '/^HTTPS:/s//http:/g' | sed '/^Https:/s//http:/g'"
    cfilen="`curl ${filen}`"
    #cfilen="`php ./inout.php ${filen}`"
    filen="${1}"
    mimetype=""
    shortf=""
    basechars="`echo -n \"${cfilen}\" | base64 | tr -d \"\\n\"`"
    #basechars="${cfilen}"
    i=0
    flast="`echo ${1} | tr '/' ' ' | sed '/\\\\/s// /g'`"
    for filebits in ${flast}; do
        shortf=${filebits}
        ((i=i+1))
    done
    subjmiddle="$shortf"
    if [ "`echo \"${subjis}\" | sed '/^First /s///g'`" != "`echo \"${subjis}\"`" ]; then
        subjis="First ${1} at ${lastdate}"
    fi
    attachrest="Mime-Version: 1.0\\nContent-Type: multipart/mixed; boundary=\"19032019ABCDE\"\\n\\n--19032019ABCDE\\nContent-Type: text/${plain}; charset=\"iso-8859-1\"\\nContent-Transfer-Encoding: 8bit\\n\\n${hprefix}${subjis}${hsuffix}\\n\\n--19032019ABCDE\\nContent-Type: application/octet-stream; name=\"${shortf}\";\\nContent-Transfer-Encoding: base64\\nContent-Disposition: attachment;filename=\"${shortf}\"\\n\\n${basechars}\\n--19032019ABCDE--"
   elif [ -z "${flist}" ]; then
    ppid="$$"
    prefix="${prefix} ${ppid}"
    flist="${1}"
   else
    flist="${flist} ${1}"
   fi
  elif [ "`echo \"${1}\" | tr -d ' ' | tr -d '^'`" != "`echo \"${1}\"" ]; then
   prefix="${prefix} `echo \"${1}\" | tr ' ' '^'`"
   subjis="`echo \"${1}\" | tr '^' ' ' | sed \"/ at date/s// at ${lastdate}/g\" | sed \"/\[date\]/s//${lastdate}/g\"`"
   plain="html"
   hprefix="<html><body><h1>"
   hsuffix="</h1></body></html>"
   attachrest="Mime-Version: 1.0\\nContent-Type: multipart/mixed; boundary=\"19032019ABCDE\"\\n\\n--19032019ABCDE\\nContent-Type: text/${plain}; charset=\"iso-8859-1\"\\nContent-Transfer-Encoding: 8bit\\n\\n${hprefix}${subjis}${hsuffix}\\n\\n--19032019ABCDE\\nContent-Type: application/octet-stream; name=\"${shortf}\";\\nContent-Transfer-Encoding: base64\\nContent-Disposition: attachment;filename=\"${shortf}\"\\n\\n${basechars}\\n--19032019ABCDE--"
  else
   if [ "`echo \"${1}\" | sed '/^0/s///g' | sed '/^1/s///g' | sed '/^2/s///g' | sed '/^3/s///g' | sed '/^4/s///g' | sed '/^5/s///g' | sed '/^6/s///g' | sed '/^7/s///g' | sed '/^8/s///g' | sed '/^9/s///g'`" != "`echo \"${1}\"" ]; then
   if [ "`echo \"${1}\" | sed '/0$/s///g' | sed '/1$/s///g' | sed '/2$/s///g' | sed '/3$/s///g' | sed '/4$/s///g' | sed '/5$/s///g' | sed '/6$/s///g' | sed '/7$/s///g' | sed '/8$/s///g' | sed '/9$/s///g'`" != "`echo \"${1}\"" ]; then
   ppid="${1}"
   #if [ "`ps -ef | grep ' ${1} ' | grep -v 'grep'`" ]; then
   #  exit
   #fi
   fi
   fi
  fi
  shift
 done
 emailfrom="`echo ${emailfrom} | tr -d ' '`"
 emailto="`echo ${emailto} | tr -d ' '`"
 emailcc="`echo ${emailcc} | tr -d ' '`"
fi


if [ ! -z "${flist}" ]; then
    i=0
    for xfilebits in ${flist}; do
        ksh ${prefix} ${xfilebits} &
        ((i=i+1))
    done
fi

while [ 0 -lt 1 ]; do
   if [ -z "${flist}" ]; then
   if [ ! -z "${ppid}" ]; then
   if [ "`ps -ef | grep ' ${ppid} ' | grep -v 'grep'`" ]; then
     exit
   fi
   fi
   fi
   if [ ! -z "${mimetype}" ]; then
     cur="`ps -ef | grep '/world.php' | grep -v 'grep ' | wc -l | tr -d ' '`"
   else 
     cur="`ls -l ${filen}`"
   fi
   echo "`date` $cur"
   if [ "$lastcur" != "$cur" ]; then
     if [ -z "`echo \"${lastcur}\" | tr -d ' '`" ]; then
       lastdate="`date`"
       if [ ! -z "${mimetype}" ]; then
         echo "`ps -ef | grep '/world.php' | grep -v 'grep '`" > date_ps_ef_first.txt
       fi
       subjis="`echo \"${subjis}\" | sed \"/ with file/s// with ${subjmiddle}/g\" | sed \"/\[file\]/s//${subjmiddle}/g\"`"
       attachrest="`echo \"${attachrest}\" | sed \"/ with file/s// with ${subjmiddle}/g\" | sed \"/\[file\]/s//${subjmiddle}/g\"`"
       echo -e "${mimetype}From: ${emailfrom}\nTo:  ${emailto}\nCC: ${emailcc}\n${emailbcc}Subject: ${subjis}\n${attachrest}" | sendmail -t -f ${emailto}
       if [ ! -z "${mimetype}" ]; then
         attachrest="\\n<html><body><h1>Changed ${subjmiddle} at ${lastdate}</h1><textarea rows=${cur} style='background-color:#e7e7e7;border:5px dotted olive;width:70%;height:100%;margin: 5 5 5 5;font-family:Courier-New;'>`diff date_ps_ef_first.txt date_ps_ef_next.txt`</textarea></body></html>"
       fi
       if [ -z "${cur}" ]; then
         exit
       fi
     else 
       thisdate="`date`"
       if [ ! -z "${mimetype}" ]; then
         subjis="Changed ${subjmiddle} at ${thisdate}"
         echo "`ps -ef | grep '/world.php' | grep -v 'grep '`" > date_ps_ef_next.txt
       else
         basechars="`base64 ${filen} | tr -d \"\\n\"`"
         if [ "`echo \"${subjis}\" | sed '/^First /s//Changed /g'`" != "`echo \"${subjis}\"`" ]; then
           subjis="Changed ${subjmiddle} at ${thisdate}"
           attachrest="Mime-Version: 1.0\\nContent-Type: multipart/mixed; boundary=\"19032019ABCDE\"\\n\\n--19032019ABCDE\\nContent-Type: text/${plain}; charset=\"iso-8859-1\"\\nContent-Transfer-Encoding: 8bit\\n\\n${hprefix}${subjis}${hsuffix}\\n\\n--19032019ABCDE\\nContent-Type: application/octet-stream; name=\"${shortf}\";\\nContent-Transfer-Encoding: base64\\nContent-Disposition: attachment;filename=\"${shortf}\"\\n\\n${basechars}\\n--19032019ABCDE--"
         else
           subjis="`echo \"${subjis}\" | sed \"/${lastdate}/s//${thisdate}/g\"`"
           attachrest="Mime-Version: 1.0\\nContent-Type: multipart/mixed; boundary=\"19032019ABCDE\"\\n\\n--19032019ABCDE\\nContent-Type: text/${plain}; charset=\"iso-8859-1\"\\nContent-Transfer-Encoding: 8bit\\n\\n${hprefix}${subjis}${hsuffix}\\n\\n--19032019ABCDE\\nContent-Type: application/octet-stream; name=\"${shortf}\";\\nContent-Transfer-Encoding: base64\\nContent-Disposition: attachment;filename=\"${shortf}\"\\n\\n${basechars}\\n--19032019ABCDE--"
         fi 
       fi
       lastdate="${thisdate}"
       subjis="`echo \"${subjis}\" | sed \"/ with file/s// with ${subjmiddle}/g\" | sed \"/\[file\]/s//${subjmiddle}/g\"`"
       echo -e "${mimetype}From: ${emailfrom}\nTo:  ${emailto}\nCC: ${emailcc}\n${emailbcc}Subject: ${subjis}\n${attachrest}" | sendmail -t -f ${emailto}
       if [ ! -z "${mimetype}" ]; then
         cat date_ps_ef_next.txt > date_ps_ef_first.txt
       fi
     fi
     lastcur="$cur"
   fi
   if [ ! -z "${mimetype}" ]; then
     if [ -z "${mimetype}" ]; then
       ps -ef | grep '/world.php' | grep -v 'grep '
     fi
   fi
   sleep 30 
done
exit

