#!/bin/sh
# $Id: smtp.sh,v 1.58 2006/11/05 17:15:02 ca Exp $
# Copyright (c) 2003-2006 Sendmail, Inc. and its suppliers.
#	All rights reserved.
#
# By using this file, you agree to the terms and conditions set
# forth in the LICENSE file which can be found at the top level of
# the sendmail distribution.
#
#
# test program running the full system:
# qmgr, smar, smtpc, smtps
# using smtps and smtpc from statethreads/examples as source/sink
#
test -s stop && exit 1
#
# source directory of this (and other) shell script
SD=`dirname $0`
if test "${SD}" = "."
then
   SD=../chkmts
fi
#
# default settings
TAS=1
THREADS=1
RCPTS=1
TOTAL=1
TR=1
MSGS=""
INIT=true
RMDEFEDB=true
KEEP=false
WAIT=""
# debug flags
D="-d 5"
# server options
SRVOPTS=${MTA_SERVER_OPTIONS}
QOPTS=""

# works only with ksh on OSF/1
#if test X"`uname`" != "XOSF1"
#then
while getopts d:DkMn:s:T:t:V:w:Q: FLAG
do
  case "${FLAG}" in
    d) D="-d ${OPTARG}";;
    D) INIT=false;;
    M) RMDEFEDB=false;;
    k) KEEP=true;;
    n) RCPTS="${OPTARG}";;
    s) MSGS="${OPTARG}";;
    T) TAS="${OPTARG}";;
    t) THREADS="${OPTARG}";;
    V) SRVOPTS="${OPTARG}";;
    w) WAIT="${OPTARG}";;
    Q) QOPTS="${OPTARG}";;
    ?)
       echo "$0: unknown option ${FLAG}"
       echo "$0: options:"
       echo "-d n	set debug level"
       echo "-n n	n recipients per transaction"
       echo "-s n	send n messages with 1..n as body"
       echo "-T n	n transactions"
       echo "-t n	n threads"
       echo "-V opts	additional SMTP server options"
       echo "-w n	wait n seconds after sending messages for delivery"
       echo "-Q opts	additional qmgr options"
       exit 1
       ;;
  esac
done
shift `expr ${OPTIND} - 1`
if test "X"${MSGS} != "X"
then
  TOTAL="${MSGS}"
else
  TOTAL=`expr ${TAS} \* ${THREADS}`
fi
TR=`expr ${RCPTS} \* ${TOTAL}`
# how long to wait for the MTS to deliver all mails?
# maybe make it dependent on number of rcpts/transactions/threads?
# it seems the system is becoming slower :-(
if test "X"${WAIT} = "X"
then
  WAIT=`expr ${RCPTS} + 1 `
fi

# setup/initialize system
. ${SD}/mta-setup.sh
#fi
#

killit()
{
if test -s ${PIDS}
then
  # stop MTA components
  for i in `cat ${PIDS}`
  do
    kill ${i}
  done
fi
# ----------------
# thanks kids for not following the POSIX standard...
if test X"`uname`" = "XLinux"
then
  killall qmgr smar
fi
if test X"${SRVPID}" != "X"
then
  kill -TERM ${SRVPID}
fi
exit 1
}

SRVPID=""
# store the PIDs for the started programs
rm -f ${PIDS}
trap killit 2

# start SMTP sink
${STE}/smtps2 ${PIPELINING} -b ${H}:${SNKPORT} > ${SRVL} 2>&1 &
status=$?
SRVPID=$!
if test "x${status}" != "x0"
then
  echo "$0: FAIL: cannot start server ${status}"
  exit ${status}
fi
sleep 1

# version of output files
V=0
# path to programs
P=..
rm -f ${SMTPCSOCK}
# file to override settings
# test -f options && . ./options
# go for it
${P}/smar/smar ${D} $@ > a${V}.log 2>&1 &
echo $! >> ${PIDS}
# ${CHKD}/qmgr-running.sh "${SMARSOCK}" || killit
${P}/qmgr/qmgr -c 40 -w ${W4S} -W ${W4C} ${D} ${QOPTS} $@ > q${V}.log 2>&1 &
echo $! >> ${PIDS}
# wait a bit for startup such that the sockets are created
# export SMTPCSOCK
# ${CHKD}/qmgr-running.sh || killit

${P}/smtpc/smtpc -w ${W4S} -l . -P ${SNKPORT} ${D} $@ > c${V}.log 2>&1 &
echo $! >> ${PIDS}
${P}/smtps/smtps -w ${W4S} ${SRVOPTS} -l . ${D} -b localhost:${SRVPORT} $@ > s${V}.log 2>&1 &
echo $! >> ${PIDS}

# check that the MTA is running
export SRVPORT P
${CHKD}/mta-running.sh || killit

# send messages
if ${STE}/smtpc -s ${TOTAL} -r ${H}:${SRVPORT} -n "${RCPTS}" -T "${TAS}" -t "${THREADS}" > ${CLTL} 2>&1
then
  :
else
  cat ${CLTL}
  ERRS=`expr ${ERRS} + 1 `
fi

sleep ${WAIT}

# stop MTA components
for i in `cat ${PIDS}`
do
  kill ${i}
done
# ----------------
# thanks kids for not following the POSIX standard...
if test X"`uname`" = "XLinux"
then
  killall qmgr smar
fi

# send SMTP server a signal to dump its state
if kill -USR1 ${SRVPID}
then
  :
else
  echo "kill -USR1 failed: $?"
  exit 1
fi
sleep 2

# check state of SMTP server
if ${GREP} "^Address *${H}:${P}" ${SRVL} > /dev/null
then
  :
else
  echo "failed to listen on ${H}:${P}"
  ERRS=`expr ${ERRS} + 1 `
fi
if ${GREP} "^Thread.* 8/" ${SRVL} > /dev/null
then
  :
else
  echo "failed to start threads"
  ERRS=`expr ${ERRS} + 1 `
fi
# the number of requests can vary based on the scheduler...
RQS=`${GREP} "^Requests served *" ${SRVL} | sed -e 's/^Requests served *//' | head -n1`
if test "${RQS}" -lt "${TOTAL}"
then
  echo "wrong number of requests: expected=${TOTAL} got=${RQS}"
  ERRS=`expr ${ERRS} + 1 `
fi
if ${GREP} "^Recipients *${TR}$" ${SRVL} > /dev/null
then
  :
else
  echo "wrong number of recipients: expected=${TR}"
  ${GREP} "^Recipients" ${SRVL}
  ERRS=`expr ${ERRS} + 1 `
fi
if ${GREP} "total=${TOTAL} " ${CLTL} > /dev/null
then
  :
else
  echo "wrong number of mails sent: expected=${TOTAL}"
  ${GREP} "total=" ${CLTL}
  ERRS=`expr ${ERRS} + 1 `
fi

# ----------------
kill -TERM ${SRVPID}

# ----------------
if ${IDBR0} >/dev/null
then
  :
else
  echo "t-idbr-0 failed"
  ERRS=`expr ${ERRS} + 1 `
fi
# ----------------
for i in a c q s
do
  if egrep "${ERRORPAT}" ${i}${V}.log
  then
    ERRS=`expr ${ERRS} + 1 `
  fi
done

# ----------------
# remove IBDB only if no errors
if test "${ERRS}" = "0" -a "${KEEP}" = "false"
then
  rm -f ibdd/ibd00000000*
fi

# ----------------
if test "${ERRS}" != "0"
then
  echo "${ERRS} error(s)"
  exit 1
fi
exit 0


syntax highlighted by Code2HTML, v. 0.9.1