#!/bin/sh
# otrs.cleanup - to cleanup not proccesed emails
# Copyright (C) 2001-2009 OTRS AG, http://otrs.org/
# --
# $Id: otrs.cleanup,v 1.8 2009/11/04 12:27:57 mn Exp $
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program 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 Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --

#OTRS_ROOT=/usr/local/otrs
OTRS_ROOT=$HOME
OTRS_SPOOLDIR=$OTRS_ROOT/var/spool
OTRS_POSTMASTER=$OTRS_ROOT/bin/otrs.PostMaster.pl

if ! test -e $OTRS_ROOT/RELEASE; then
    echo "$HOME is not OTRS root!"
    exit 1;
fi

# check otrs spool dir
echo " Checking otrs spool dir... "
for i in $OTRS_SPOOLDIR/* ; do
    # process old emails
    if echo $i | grep -v '*' >> /dev/null; then
        echo -n "   Starting otrs PostMaster... ($i) "
        if cat $i | $OTRS_POSTMASTER >> /dev/null 2>&1; then
            rm $i && echo "(remove email) done.";
        else
            echo "failed.";
        fi
    fi
done
echo " done."

