#!/bin/sh

##
# Get mail delivery data between timestamp $1 and timestamp $2
##

if [[ "$1" == "" || "$1" == "--help" || "$1" == "-h" ]]; then
  echo "Usage: $0 from-timestamp to-timestamp"
  echo
  exit 1
fi

FROM=$1

if [[ "$2" == "" ]]; then
  TO=`date -d now +%s`
else
  TO=$2
fi

DIR="$( cd "$( dirname "$0" )" && pwd )"
LOGDIR=/var/log/send
TAI64N2TAI=/usr/bin/tai64n2tai
MATCHUP=/usr/local/qmailanalog/bin/matchup
AWK=/usr/bin/awk
STRIP_VIRT="${DIR}/../lib/strip-virt.sh"
LOGS_BY_DATE="${DIR}/../lib/get-logs-by-date.sh"

files=`$LOGS_BY_DATE $1`

if [[ "$files" == "" ]]; then
  echo "Usage: $0 from-timestamp to-timestamp"
  echo
  exit 1
fi

cat $files \
   | $TAI64N2TAI \
   | $AWK -v x=$FROM -v y=$TO '{
             if( $1 > x && $1 < y )
               print
           }' \
   | $MATCHUP 5> /dev/null \
   | $STRIP_VIRT
