#!/bin/bash

#
# Copyright (C) 2019 Nethesis S.r.l.
# http://www.nethesis.it - info@nethesis.it
#
# This script is part of Dante.
#
# Dante is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# Dante is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Dante.  If not, see COPYING.
#

# Count received mails

TMP_FILE=$(mktemp)
LOG="/var/log/maillog"
# Create a valid regexp
DAY=$(date +"%b\s+%-d")

# Exit if dovecot is not installed or log is empty
if [ ! -f $LOG ] || [ ! -f /etc/e-smith/db/configuration/defaults/dovecot/type ]; then 
    exit 1
fi

PREVIOUS_LOG=$(find $(dirname $LOG) -name maillog-\* -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
if [ -f "$PREVIOUS_LOG" ]; then
    zgrep -E "$DAY" $PREVIOUS_LOG*  > $TMP_FILE
fi

# Match also dates like "Jul  1"
grep -E "$DAY" /var/log/maillog >> $TMP_FILE

value=0

value=$(grep -E 'postfix\/lmtp\[.*status=sent' $TMP_FILE | wc -l)

id=$(basename $0)
type=$(basename $0 | cut -d'-' -f2)
chartType=$(basename $0 | cut -d'-' -f3)
unit="number"
aggregation="sum"
trend="percentage"

echo -n "{\"value\":$value,\"type\":\"$type\",\"minerId\":\"$id\",\"aggregationType\":\"$aggregation\",\"unit\":\"$unit\",\"trendType\":\"$trend\"}"

rm -f $TMP_FILE 2>/dev/null

