#!/bin/sh
#
# %W%  %G% SMI
# Portions Copyright (c) 1996, 1997 Sun Microsystems, Inc.
# All Rights Reserved.
#
#
# Copyright (c) 1994, 1997 Innosoft International, Inc.
# All Rights Reserved.
#
# return - shell script to return old messages.
#

#
# Check IMTA_TAILOR is set
#
if [ ! -f "$IMTA_TAILOR" ]; then
  echo "imta_tailor not set"
  exit 1
fi


##################################################################
# shell function to get a variable's value
# from tailor file.
##################################################################
getTailorValue() {
  y=`/bin/grep "^$1=" $IMTA_TAILOR |/bin/awk -F= '{print $2}' | head -1`
  echo $y
}

##################################################################
# shell function to get some values from tailor file.
##################################################################
getValuesFromTailor() {
  BINPATH=`getTailorValue IMTA_BIN`
  PRGMPATH=`getTailorValue IMTA_PROGRAM`
}

##################################################################
# try to read and reinitialize all the paths from the tailor file
# if it exists.
##################################################################
if test -f $IMTA_TAILOR; then
  getValuesFromTailor
fi

##################################################################
# if IMTA_VERIFY_RETURN is 1, do a set -x
##################################################################
verify_return=`getTailorValue IMTA_VERIFY_RETURN`
if [ -z "$verify_return" ]
then
  verify_return=0
fi
if [ $verify_return -eq 1 ]
then
  set -x
fi

##################################################################
# real action begins now.
##################################################################

# get and increment the number of times we have run since the last reboot.
return_count=`$BINPATH/get_count return`

# return old messages
$BINPATH/return

#
# clean out old unconfirmed cookies
#
pmdf_mailserv_cookie_retention=`getTailorValue IMTA_MAILSERV_COOKIE_RETENTION`
if [ -z "$pmdf_mailserv_cookie_retention" ]
then
  pmdf_mailserv_cookie_retention=7
elif [ $pmdf_mailserv_cookie_retention -lt 0 ]
then 
  pmdf_mailserv_cookie_retention=7
fi
QUEUEPATH=`getTailorValue IMTA_QUEUE`
mailserv_spool=mailserv/spool/
mailserv_cookie_path=$QUEUEPATH$mailserv_spool
if [ -d $mailserv_cookie_path ]; then
  for file in `find $mailserv_cookie_path -name "*.??" -ctime +$pmdf_mailserv_cookie_retention -print`
  do
    rm -f $file
  done
fi

#
# clean out old unconfirmed data transfer failure flags
#
pmdf_tcp_flag_retention=`getTailorValue IMTA_TCP_FLAG_RETENTION`
if [ -z "$pmdf_tcp_flag_retention" ]
then
  pmdf_tcp_flag_retention=7
elif [ $pmdf_tcp_flag_retention -lt 0 ]
then 
  pmdf_tcp_flag_retention=7
fi
QUEUEPATH=`getTailorValue IMTA_QUEUE`
tcp_spool="tcp*/spool"
tcp_data_failed_spool_path=$QUEUEPATH$tcp_spool
if [ -d $tcp_data_failed_spool_path ]; then
  for file in `find $tcp_data_failed_spool_path -name "*.data-failed" -ctime +$pmdf_tcp_flag_retention -print`
  do
    rm -f $file
  done
fi

# startup a new mail.log
split_period=`getTailorValue IMTA_RETURN_SPLIT_PERIOD`
if [ -z "$split_period" ]
then
  split_period=1
elif [ $split_period -lt 1 ]
then
  split_period=1
fi
if [ `expr $return_count % $split_period` -eq 0 ]
then
  primary=`getTailorValue IMTA_PRIMARY_LOG`
  secondary=`getTailorValue IMTA_SECONDARY_LOG`
  tertiary=`getTailorValue IMTA_TERTIARY_LOG`
  if [ -f $secondary ]
  then
    cat $secondary >> $tertiary
    chmod 700 $tertiary
    rm $secondary
  fi
  if [ -f $primary ]
  then
    mv $primary $secondary
  fi
fi

# startup a new connection.log
split_period=`getTailorValue IMTA_RETURN_SPLIT_PERIOD`
if [ -z "$split_period" ]
then
  split_period=1
elif [ $split_period -lt 1 ]
then
  split_period=1
fi
if [ `expr $return_count % $split_period` -eq 0 ]
then
  primary=`getTailorValue IMTA_PRIMARY_CONNECTION_LOG`
  secondary=`getTailorValue IMTA_SECONDARY_CONNECTION_LOG`
  tertiary=`getTailorValue IMTA_TERTIARY_CONNECTION_LOG`
  if [ -f $secondary ]
  then
    cat $secondary >> $tertiary
    chmod 700 $tertiary
    rm $secondary
  fi
  if [ -f $primary ]
  then
    mv $primary $secondary
  fi
fi

# do any site specific stuff
clean_period=`getTailorValue IMTA_RETURN_CLEANUP_PERIOD`
if [ -z "$clean_period" ]
then
  clean_period=1
elif [ $clean_period -lt 1 ]
then
  clean_period=1
fi
if [ `expr $return_count % $clean_period` -eq 0 ]
then
  if [ -f $PRGMPATH/bin/daily_cleanup ]
  then
    $PRGMPATH/bin/daily_cleanup
  fi
fi
