#!/bin/sh
#
# @(#) wrap-sendmail - wrapper for use with squirrelmail and TMDA
# @(#) $Id: wrap-sendmail,v 1.2 2002/03/11 20:56:36 bduncan Exp bduncan $
#
# Description:
#   - this script depends on $username being set from a modified
#     version of squirrelmail.  See the README file for details.
#   - basically, the $username environment variable needs to be
#     set from the popen() function in smtp.php
#   - this also has a temporary fix for a problem I was noticing
#     with Squirrelmail 1.2.5 where Carriage Returns were getting
#     through on mail being sent out.  Didn't happen on 1.2.4.
#     This is a temporary fix which works for me, but your mileage
#     may vary...  I'm still not sure where this is coming from.
#   - the "tee" which is commented out, was just something I'd used
#     to prove that there were carriage returns coming through.    (CRLF's)
#     as far as I know, we should only be seeing linefeed characters (LF's)
#     (which I was in 1.2.4)
#   - passthrough normal sendmail interface if $username is not set.
#   - if all the ducks aren't in order for being able to execute tmda
#     then fallback to regular sendmail interface..
#
# SETUP:
#   - make sure mods are made as per README file
#   - modify environment variables TMDAMAIL and SENDMAIL for your environment
#   - you may or may not need the NUKECR, set it to "true" or "false".  Try
#     false first probably, or uncomment the "tee", and use "od -c" to look
#     at the resulting file.  If you see "\r" in the file, you may need it.
#
# NOTES:
#   - None of this may be needed if you have a qmail setup.  I've been
#     getting reports from users with qmail that the wrapper was not
#     needed at all.  I don't have qmail to test this hypothesis.
#
##

# USER SETTABLE - Adjust to taste...
#
TMDAMAIL=/usr/local/tmda/bin/tmda-sendmail
SENDMAIL=/usr/sbin/sendmail
NUKECR=true
TMPFILE=/var/withcr.txt
export TMDAMAIL SENDMAIL NUKECR TMPFILE


if [ -n "$username" ]; then
  LOGNAME=$username
  # TMDA looks for these, or QMAILUSER
  USER=$LOGNAME
  USERNAME=$LOGNAME

  #
  # There are probably prettier ways of doing this, but it works for me.
  #
  HOME=`awk -F: '$1 ~ /^'$USER'$/ { print $6 }' /etc/passwd`
  NAME=`awk -F: '$1 ~ /^'$USER'$/ { print $5 }' /etc/passwd`
  TMDARC=$HOME/.tmdarc

  export USER NAME USERNAME LOGNAME TMDARC HOME
fi


# if all our ducks aren't in order, fall through and execute the regular
# sendmail-style binary.
#
if [ -n "$username" -a -r $HOME/.tmdarc -a -d $HOME/.tmda -a -f $HOME/.forward ]; then
  # tee $TMPFILE |
  tr -d '\015'   | $TMDAMAIL $*  >> /tmp/wrap-sendmail.out 2>&1
else
  # tee $TMPFILE |
  tr -d '\015'   | $SENDMAIL $*
fi

