#!/bin/sh
#------------------------------------------------------------------------------
# /usr/local/htdocs/mailmanager/mail_view_stats.cgi - component of mailmanager
#                                                     package
#
# Copyright (c) 2004-2017  Marcus Herleb, marcus(at)herleb(dot)de
# Copyright (c) 2018-2018  The Eisfair Team, team(at)eisfair(dot)org
#
# Creation:     2004-04-01 mh
# Last Update:  $Id$
#
# This program 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 2 of the License, or
# (at your option) any later version..
#------------------------------------------------------------------------------

myname=`basename $0`
tag=`date +%d.%m.%Y`
uhr=`date +%T`
RTIME=5
exim_bin_dir=/usr/sbin
exim_spool_dir=/var/spool/exim
exim_log_file=mainlog
log_dir=/var/log
fetchmail_log_file=fetchmail.log

# HTML-Header
echo "Content-Type: text/html; charset=iso-8859-1"
echo "Cache-control: no-store" # HTTP/1.1 (or no-cache?)
echo "Pragma: no-cache"        # HTTP/1.0
echo "<html><head>"
echo
echo "<title>EIS Mail Manager</title>"
echo '<LINK REL=STYLESHEET TYPE="text/css" HREF="../main.css">'
. ../prnscript.js
echo "</head><body><pre>"
echo "<center><font size=5 title='hier f&uuml;r Ausdruck klicken' onclick=custom_print()><u>$QUERY_STRING Statistik :</u></font><br><br>"
echo '<input type="button" value="Fenster schliessen" onclick="parent.window.self.close();"></center>'

if [ "$QUERY_STRING" = "EXIM" ]
then
    if [ -f /usr/bin/perl ]
    then
        if [ -f $exim_spool_dir/log/$exim_log_file ]
        then
            export LESSCHARSET=latin1
            ${exim_bin_dir}/eximstats $exim_spool_dir/log/$exim_log_file | less > EXIM.stat
        fi
    else
        echo "Could not find perl interpreter, please install to use this function!"
    fi

elif [ "$QUERY_STRING" = "Fetchmail" ]
then
    if [ -f $log_dir/$fetchmail_log_file -a -s $log_dir/$fetchmail_log_file ]
    then
        export LESSCHARSET=latin1
        /usr/bin/less +G $log_dir/$fetchmail_log_file > Fetchmail.stat
    fi

elif [ "$QUERY_STRING" = "Panic" ]
then
    exim_log_file=paniclog
    if [ -f $exim_spool_dir/log/$exim_log_file -a -s $exim_spool_dir/log/$exim_log_file ]
    then
        export LESSCHARSET=latin1
        /usr/bin/less +G $exim_spool_dir/log/$exim_log_file > Panic.stat
    fi

elif [ "$QUERY_STRING" = "Reject" ]
then
    exim_log_file=rejectlog
    if [ -f $exim_spool_dir/log/$exim_log_file -a -s $exim_spool_dir/log/$exim_log_file ]
    then
        export LESSCHARSET=latin1
        /usr/bin/less +G $exim_spool_dir/log/$exim_log_file > Reject.stat
    fi
fi

tr "\012" ";" < $QUERY_STRING.stat > /tmp/$QUERY_STRING.tmp
sed -n -e 's/;/<br>/gp' /tmp/$QUERY_STRING.tmp
echo "</pre></body></html>"