#!/bin/sh
###############
# shell script: do.showMailRules
# created: jw: 04-Mar-2022
#  modify: jw: 05-Mar-2022 fix first last Rule logic
#          jw: 05-Mar-2022 syncedRules -> SyncedRules
#          jw: 06-Mar-2022 include StopEvaluatingRules also V8 V9
#          jw: 07-Mar-2022 add V6 for Mojave Mail 12.4
#          jw: 08-Mar-2022 generalize Mail verisons V6, V8, V9,  ...
#
# version: $Id: do.showMailRules,v 1.9 2022/03/08 17:17:25 jw Exp $
# args:
#   REQUIRED
#      none
#   OPTIONAL
#      if there is an arg, show the unsynced file
# returns:
#      output text to shell
# description:
#    parse the synced or Unsynced Mail Rules plist files to show name and
#    where the message is sent (CopyToMailboxURL)
#    if the rule has StopEvaluatingRules, then use ->
#    if not have     StopEvaluatingRules, then use !>
#
# set -u
# set -x
ME=`whoami`
inDir=""
if [ ! -f ~/Library/Mail/PersistenceInfo.plist ]
then
    printf "Cannot find Mail for user %s\n" "${ME}"
    exit
fi

DD=""
LastDir=""
while read -r line 
do
    if [ -n "${LastDir}" ]
    then
	DD=`echo ${line} | sed 's/\<string\>//' | sed 's/\<.*//' | sed 's/\n//'`
	LastDir=""
    fi
    if [ -n "`echo ${line} | grep '<key>'`" ]
    then
	KeyLine=`echo ${line} | sed 's/\<key\>//' | sed 's/\<.*//' | sed 's/\n//'`
    else
	KeyLine=""
    fi
    if [ "${KeyLine}" = "LastUsedVersionDirectoryName" ]
    then
	LastDir=Yes
    fi
done <  ~/Library/Mail/PersistenceInfo.plist 

if [ -d ~/Library/Mail/${DD}/MailData ]
then
    inDir=~/Library/Mail/${DD}/MailData
fi

if [ -d "${inDir}" ]
then
    printf "reading from Mail directory: %s\n%38s\n" "${inDir}" "`date`"
else
    printf "no Mail info found for user %s\n"  "${ME}"
    exit
fi
case $# in
    1) inFileName=UnsyncedRules;;
    *) inFileName=SyncedRules;;
esac

inFileName=${inFileName}.plist
inFile=${inDir}/${inFileName};
CopyToMailboxURL=""
RuleName=""
URL=""
Name=""
FirstAllCriteria="Yes"
StopEvaluatingRules=""
NumRules=`expr 0`
SepLine="......................................\n"
while read -r line 
do
  if [ -n "`echo ${line} | grep '<key>'`" ]
  then 
   KeyLine=`echo ${line} | sed 's/\<key\>//' | sed 's/\<.*//' | sed 's/\n//'`
  else
   KeyLine=""
  fi
  if [ "${KeyLine}" = "AllCriteriaMustBeSatisfied" ] && [ -n "${FirstAllCriteria}" ]
  then
      HOST=`hostname -s`
      if [ ! -n "${HOST}" ]
      then
	  HOST="<unknown>"
      fi
      printf "host: %s\n" ${HOST}
      printf $SepLine
      FirstAllCriteria=""
      NumRules=`expr 0 + 1`
      continue
  fi
  if [ "${KeyLine}" = "AllCriteriaMustBeSatisfied" ]
  then
      #     printf "%38s -> %s\n" "${Name}" "${URL}"
      StopFalse=`echo "${StopEvaluatingRules}" | grep alse`
      if [ -n "${StopEvaluatingRules}" ] && [ ! -n "${StopFalse}" ]
      then
	  printf "%38s -> %s\n" "${Name}" "${URL}"
      else
	  printf "%38s !> %s\n" "${Name}" "${URL}"
      fi
    NumRules=`expr ${NumRules} + 1`
    URL=""
    Name=""
    StopEvaluatingRules=""
  fi
  

 if [ -n "${CopyToMailboxURL}" ]
 then
  URL=${line}
  URL=`echo ${URL} | sed 's/\<string\>//' | sed 's/\<.*//' | sed 's/\n//'`
  CopyToMailboxURL=""
  continue
 fi
 if [ -n "${RuleName}" ]
 then
  Name=${line}
  Name=`echo ${Name} | sed 's/\<string\>//' | sed 's/\<.*//' | sed 's/\n//'`
  RuleName=""
  continue
 fi
# ...............................
 if [ "${KeyLine}" = "CopyToMailboxURL" ]
 then
     CopyToMailboxURL=${KeyLine}
 fi
 if [ "${KeyLine}" = "RuleName" ]
 then
     RuleName=${KeyLine}
 fi
 if [ "${KeyLine}" = "StopEvaluatingRules" ]
 then
     StopEvaluatingRules=${KeyLine}
 fi
done < ${inFile}

if [ -n "${URL}" ] || [ -n "${Name}" ]
then
    if [ -n "${StopEvaluatingRules}" ]
    then
	printf "%38s -> %s\n" "${Name}" "${URL}"
    else
	printf "%38s !> %s\n" "${Name}" "${URL}"
    fi
fi

printf ${SepLine}
printf "Note: if StopEvaluatingRules missing or false, print !> connector\n"
printf "%3d Rules in %s\n" ${NumRules} ${inFileName}
printf "  (%s) on (%s)\n" "${inDir}" "${HOST}"
printf "%30s\n"  "`date`"
printf "version$Id: do.showMailRules,v 1.9 2022/03/08 17:17:25 jw Exp $\n"
#Vers: $Id: do.showMailRules,v 1.9 2022/03/08 17:17:25 jw Exp $
