#!/bin/sh
#
# Delete expired emails
# Copyright 2005-2006 Florian Daeumling <jam@qv90.de>, Jaballa Software
# Licensed under the GNU GPL. For full terms see the file COPYING.
#

# test mode?
testing=<<TESTING>>
# logging level
logging=<<LOGGING>>
# expire data directory
data_dir="<<DATA_DIR>>"
# use locate command?
locate=<<LOCATE>>
# pointer to users mail directory
mail_dir="<<MAIL_DIR>>"
# host name
host="<<HOSTNAME>>"

if [ $testing == 0 ]; then
 log="logger -t expire"
else
 log="logger -s -t expire "
fi

if [ $logging == 2 ]; then
 $log "Starting $0 $@"
fi

case "$1" in
 scan)					# scan boxes
    usr=$2
    mbox=$3
    days=$4
    case "$locate" in
    0)
     mail_dir=`echo $mail_dir | sed s"<SM_USER>""$usr"g`
     mdir=1
     if [ "$host" != "localhost" ]; then
      if [ `rsh $host ls $maildir >/dev/null` ]; then
       mdir=""
      fi
     else
      if [ ! -d $mail_dir ]; then
       mdir=""
      fi
     fi
     if [ "$mdir" == "" ]; then
      $log "+++ File not found in '$mail_dir' on '$host'"
      exit 1
     fi
     ;;
      
    1)			 		# use locate command?
     if [ "$host" != "localhost" ]; then
      list=`rsh $host "locate $usr.expire" 2>&1`
     else
      list=`locate $usr.expire 2>&1`
     fi
     wrk=$mail_dir
     mdir=""
     for path in $list; do
      if [ `expr match "$path" "$wrk"` -gt 0 ]; then
       mdir=${path%/*}
       break
      fi
     done 
     if [ "$mdir" == "" ]; then
      $log "+++ File '$usr.expire' not found in '$mail_dir' on '$host'"
      exit 1
     else
      mail_dir=$mdir
     fi
     ;;
    
    2)					# use find command?
     if [ "$host" != "localhost" ]; then
      mdir=`rsh $host "find $mail_dir -name $usr.expire -printf %h" 2>&1`
     else
      mdir=`find $mail_dir -name $usr.expire -printf %h`
     fi
     if [ "$mdir" == "" ]; then
      $log "+++ File '$usr.expire' not found in '$mail_dir' on '$host'"
      exit 1
     else
      mail_dir=$mdir
     fi
     ;;
    esac
    
    if [ $logging == 2 ]; then
     $log "User mail directory on '$host' is '$mail_dir'"
    fi

    if [ "$mbox" == "INBOX" ]; then	# build mail directory
     mdir=$mail_dir"/cur"
    else
     mdir=$mail_dir"/."$mbox"/cur"
    fi
    # find and delete files
    cmd="find $mdir -daystart -mtime +$days -type f "
    if [ $logging == 2 ]; then
     $log "Checking '$mdir' for expiration '$days' days"
     if [ "$host" != "localhost" ]; then
      cmd="$cmd -printf \"%f\n\" "
     else
      cmd="$cmd -printf %f\n "
     fi
    fi
    # really delete files?
    if [ $testing == 0 ]; then
     if [ "$host" != "localhost" ]; then
      cmd="$cmd -exec rm '{}' ';' "
     else
      cmd="$cmd -exec rm {} ; "
     fi
    fi
    if [ "$host" != "localhost" ]; then
     cmd="rsh $host $cmd"
    fi
    list=`$cmd`
    rc=$?
    if [ $rc != 0 ]; then
     $log "+++ Error on '$host' - $list"
     exit 1
    fi
    cnt=0
    for file in $list; do
     if [ $logging == 2 ]; then
      $log "Deleting '$file' on '$host'"
     fi
     let cnt++
    done
    if [ $logging == 2 ]; then
     $log "$cnt files deleted in '$mdir'"
    fi
    ;;
    
 *)				 	# call from crontab?
    if [ $logging == 2 ]; then
     $log "Expire plugin configurations files '$data_dir'"
    fi
    list=`ls $data_dir` 
    for usr in $list; do
     if [ $logging == 1 ]; then
      $log "Starting check for user '$usr'"
     fi
     if [ $logging == 2 ]; then
      $log "Using configuration file '$data_dir$usr'"
     fi
     list=`cat $data_dir$usr`
     for p in $list; do
      if [ "$save" == "" ]; then
       save=$p
      else
       if [ "$save" != "" ];  then
        $0 scan $usr $save $p
	save=""
       fi
      fi
     done
    done
    ;;
esac
exit 0
