#!/bin/ksh
# snapshotexim.ksh
# RJM Programming
#
# Eg.
#    echo '/Applications/MAMP/htdocs/ "2022-12-20 11:4[01]"' > snapshotexim.txt
#    ksh ./snapshotexim.ksh  # may populate /Applications/MAMP/htdocs/snapshotexim.php
# ... or ...
#    echo '' > snapshotexim.txt
#    ksh ./snapshotexim.ksh /Applications/MAMP/htdocs/ "2022-12-20 11:4[01]"  # may populate /Applications/MAMP/htdocs/snapshotexim.php

  diris="RelevantHomeDir/";
  if [ "`uname`" == "Darwin" ]; then
    diris="/Application/MAMP/htdocs/";
  fi
  findthis=":";
  newfind="";
  vl="/var/log/";

  if [ ! -z "${1}" ]; then
    diris="$1";
    if [ ! -z "${2}" ]; then
      findthis="$2";
    elif [ -f "${diris}exim_mainlog" ]; then 
      findthis=`tail -1 "${diris}exim_mainlog" | cut -f1,2 -d' ' | awk 'BEGIN{FS=":"} {print $1 ":" $2}'`;
    fi 
  elif [ -f "${diris}exim_mainlog" ]; then 
    findthis=`tail -1 "${diris}exim_mainlog" | cut -f1,2 -d' ' | awk 'BEGIN{FS=":"} {print $1 ":" $2}'`;
  fi
  
  if [ -f "snapshotexim.txt" ]; then
    newfind=`cat "snapshotexim.txt" | sed '/"/s///g'`;
    if [ ! -z "${newfind}" ]; then
      diris=`echo "${newfind}" | cut -f1 -d' '`;
      xdis=`echo "${newfind}" | cut -f2 -d' '`;
      xxdis=`echo "${newfind}" | cut -f3 -d' '`;
      if [ ! -z "${xxdis}" ]; then
        findthis=`echo "${xdis} ${xxdis}"`;
      fi
    fi
    if [ -f "${diris}exim_mainlog" ]; then
     vl="${diris}";
    fi
    if [ "${findthis}" == ":" -o "${findthis}" == "" ]; then
    exit;
    else 
    rm -f  "${diris}snapshotexim.txt";
    minu=`cat "${vl}exim_mainlog" | grep "${findthis}"`;
    echo "<?php  if (isset(\$_POST['relevantargname'])) { echo file_get_contents('./snapshotexim.php'); exit; } /* ${minu} */ ?>" > ${diris}snapshotexim.php
    # cat ${diris}snapshotexim.php
    fi
  elif [ -f "${diris}snapshotexim.txt" ]; then
    newfind=`cat "${diris}snapshotexim.txt" | sed '/"/s///g'`;
    if [ ! -z "${newfind}" ]; then
      diris=`echo "${newfind}" | cut -f1 -d' '`;
      xdis=`echo "${newfind}" | cut -f2 -d' '`;
      xxdis=`echo "${newfind}" | cut -f3 -d' '`;
      if [ ! -z "${xxdis}" ]; then
        findthis=`echo "${xdis} ${xxdis}"`;
      fi
    fi
    if [ -f "${diris}exim_mainlog" ]; then
     vl="${diris}";
    fi
    if [ "${findthis}" == ":" -o "${findthis}" == "" ]; then
    exit;
    else
    rm -f  "${diris}snapshotexim.txt";
    minu=`cat "${vl}exim_mainlog" | grep "${findthis}"`;
    echo "<?php if (isset(\$_POST['relevantargname'])) { echo file_get_contents('./snapshotexim.php'); exit; } /* ${minu} */ ?>" > ${diris}snapshotexim.php
    # cat ${diris}snapshotexim.php
    fi
  fi
  
  if [ -f "${diris}snapshotexim.txt" ]; then
    if [ "${findthis}" == ":" -o "${findthis}" == "" ]; then
    exit;
    else
    rm -f  "${diris}snapshotexim.txt";
    minu=`cat "${vl}exim_mainlog" | grep "${findthis}"`;
    echo "<?php if (isset(\$_POST['relevantargname'])) { echo file_get_contents('./snapshotexim.php'); exit; } /* ${minu} */ ?>" > ${diris}snapshotexim.php
    # cat ${diris}snapshotexim.php
    fi
  fi
  
  exit;
