#!/bin/bash
MSGID=$1
[ -z "$MSGID" ] && echo "Usage : $0 [message-id]" && exit 1
DEBUG=0
[ "$2" == "-d" ] && DEBUG=1
MAX_LOG=9
function search_mailid () {
MSGID=$1
MAILLOG="$2"
[ ! -f "$MAILLOG" ] && MAILLOG="$MAILLOG.gz"
[ -f "$MAILLOG" ] && zgrep $MSGID $MAILLOG|head -n 1|sed 's/^.*\: \([^\:]*\): message-id.*$/\1/'
}
[ $DEBUG -eq 1 ] && echo "Search mail ID from message ID $MSGID ..."
res=$( search_mailid "$MSGID" /var/log/mail.log )
if [ -n "$res" ]
then
echo "$res"
exit 0
else
[ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log. Try with archive (Max : $MAX_LOG)"
for i in $( seq 1 $MAX_LOG )
do
res=$( search_mailid "$MSGID" /var/log/mail.log.$i )
if [ -n "$res" ]
then
[ $DEBUG -eq 1 ] && echo "Found from log file /var/log/mail.log.$i"
echo "$res"
exit 0
else
[ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log.$i"
fi
done
[ $DEBUG -eq 1 ] && echo "Not found in archive (Max : $MAX_LOG)"
fi
exit 1