fatal() {
  for line in "$@"; do
    echo "$line" >&2
  done
  exit 100;
}

grephdr() {
  # Search for the header line, and produce an error if it didn't match.
  egrep "^$*$" "$QQHDR" >/dev/null 2>&1 || \
  {
    echo "Header is missing or wrong $1 line:";
    grep -i "^$1" "$QQHDR"
    BUG="${BUG} headers"
    prompt "..............: "
  }
  # Remove any found lines from the header file
  grep -iv "^$1" "$QQHDR" >"${TMP}hdr"
  mv -f "${TMP}hdr" "$QQHDR"
}

grepbody() {
  egrep "^$*$" "$QQBODY" >/dev/null 2>&1 || \
  {
    echo "Body is missing or wrong $1 line:";
    grep -i "^$1" "$QQBODY"
    BUG="${BUG} body"
    prompt "..............: "
  }
}

grepbodynot() {
  egrep "^$*" "$QQBODY" >/dev/null 2>&1 && \
  {
    echo "Body contains extraneous $1 line:";
    grep -i "^$1" "$QQBODY"
    BUG="${BUG} body"
    prompt "..............: "
  }
}

grephdr_std() {
  grephdr Message-ID: "<[0123456789]*\\.[0123456789]*\\.ezmlm@${HOST}>"
  grephdr Delivered-To: "responder for ${LIST}@${HOST}"
  grephdr MIME-Version: 1.0
}

grephdr_list() {
  grephdr Mailing-List: "contact ${LIST}-help@${HOST}; run by ezmlm"
  grephdr List-Help: "<mailto:${LIST}-help@${HOST}>"
  grephdr List-Post: "<mailto:${LIST}@${HOST}>"
  grephdr List-Subscribe: "<mailto:${LIST}-subscribe@${HOST}>"
  if [ -n "$*" ]; then
    grephdr List-Unsubscribe: "<mailto:${LIST}-unsubscribe@${HOST}>"
  fi
}

grephdr_manage() {
  grephdr_std
  grephdr_list
  grephdr Date: '..* ... .... ..:..:.. [-+]....'
  grephdr From: "${LIST}-help@${HOST}"
  grephdr To: "test@example.org"
  grephdr Content-Type: "text/plain; charset=.*"
}

grephdr_empty() {
  # Use this after all other grephdr checks to ensure nothing else was output
  test -s "$QQHDR" && \
  {
    echo "Headers contained extra lines:"
    cat "$QQHDR"
    BUG="${BUG} headers"
  }
}

checkenv()
{
  msg="$1"
  head -n 1 "$QQENV" | grep "^F$2$" >/dev/null \
  || fatal "envelope for $msg has wrong sender"
  test $# = $(wc -l <"$QQENV") \
  || fatal "envelope for $msg has wrong number of recipients"
  shift 2
  for rcpt in "$@"; do
    grep "^T$rcpt$" "$QQENV" >/dev/null \
    || fatal "envelope for $msg is missing recipient $rcpt"
  done
}

###############################
# message generating function #
###############################
make_body()
{
  echo "This is a simple message body"
  echo "--bound123ary"
  echo "Content-type: Text/pLAIn"
  echo
  echo "plain text"
  echo "--bound123ary"
  echo "Content-type: texT/Html"
  echo
  echo "html text"
  echo "--bound123ary--"
  echo
  echo "junk after boundary"
  return 0
}

qqclean()
{
  rm -f "$QQBODY" "$QQENV" "$QQHDR" "$QQMSG" "$QQMSG".*
}

make_message()
{
  echo "ReCEIved: #LAST#"
  echo "ReCeIved: #PENULTIMATE#"
  echo "retuRN-RECeipt-to: nobody"
  echo "To: $TO"
  echo "CC: "
  echo " $CC"
  echo "FROM: $FROM"
  if [ ! -z "$CONTENT" ]; then
	echo "MIME-Version: 1.0"
	echo "Content-type: $CONTENT;"
	echo " boundary=bound123ary${AFTERBOUND}"
  fi
  if [ ! -z "$SUBJECT" ]; then
	echo "Subject: $SUBJECT"
  fi
  echo
  make_body
  return 0
}

#########################
# Tests for file status #
#########################
exists() {
  for file in "$@"; do
    test -e "$file" || return 1
  done
  return 0
}

msgexists() {
  test -s "$QQMSG" -a -s "${QQENV}"
}

#######################################
# Extract parts of the message header #
#######################################
gethdr() {
    sed -e "/^$1:/!d; s/[^ ]* *//;" "$QQHDR"
}

##########################################
# Run ezmlm-manage and check the results #
##########################################
check_manage() {
    local name=$1
    local options=$2
    local exit=$3
    local exitmsg="$4"
    local text="$5"
    local sender="$6"
    ${EZBIN}/ezmlm-manage -a $options "$DIR" < "$TMP" > "$ERR" 2>&1
    local e=$?
    test $e -eq $exit || fatal "ezmlm-manage ($name) $exitmsg (exit $e)"
    if [ -n "$text" ]
    then
	grep "ezmlm-manage: info: sending $text$" "$ERR" >/dev/null 2>&1 \
	    || fatal "ezmlm-manage ($name) sent wrong text (should be $text)"
    fi
    if [ -n "$sender" ]
    then
	shift 6
	checkenv $name "$sender" "$@"
    fi
}
