#!/bin/ksh -p
#  exp_mail: mail transport mechanism
#  $Id: //depot/stable/explorer/lib/exp_mail#3 $
EXP_MAIL_VERSION='$Revision: #3 $'
export EXP_MAIL_VERSION

EXP_VERSION=4.0

if [ -n "${DO_VERSION}" ]
then
    LIB=${0##*/}
    echo "${LIB} version: ${EXP_MAIL_VERSION}"
    exit
fi

# Source common functions
. ${EXP_LIB}/exp_common

# Message IDs
msgidn=0
gen_messageid () {
    ((msgidn=msgidn + 1))
    msgid="<explorer.`date -u +\%Y\%m\%d\%H\%M\%S`.$$.$msgidn.${HOSTID}@`uname -n`>"
}

# Must have EXP_FILE, EXP_TARGET, EXP_TMPDIR and EXP_MAILTO
require 'test -f "${EXP_FILE}"'
require 'test -n "${EXP_TARGET}"'
require 'test -n "${EXP_TMPDIR}"'
require 'test -n "${EXP_MAILTO}"'
require "cd ${EXP_TMPDIR}"

ARCH=`uname -p`
EXP_FNAME=${EXP_TARGET##*/}
FILE="${EXP_TMPDIR}/${EXP_FILE}"

# Expand EXP_SUBJECT if necessary
if [ -z "${EXP_SUBJECT}" ]
then
    EXP_SUBJECT=${EXP_FNAME}
else
    # Translate %F to EXP_FNAME
    EXP_SUBJECT=`echo ${EXP_SUBJECT} | sed -e "s~%F~${EXP_FNAME}~g"`

    # Translate %C to EXP_CUSTOMER_NAME
    EXP_SUBJECT=`echo ${EXP_SUBJECT} | sed -e "s~%C~${EXP_CUSTOMER_NAME}~g"`

    # Translate %I to EXP_CONTRACT_ID
    EXP_SUBJECT=`echo ${EXP_SUBJECT} | sed -e "s~%I~${EXP_CONTRACT_ID}~g"`

    # Translate %E to EXP_ID
    EXP_SUBJECT=`echo ${EXP_SUBJECT} | sed -e "s~%E~${EXP_ID}~g"`

    # Translate %H to hostid
    EXP_SUBJECT=`echo ${EXP_SUBJECT} | sed -e "s~%H~${HOSTID}~g"`
fi

# If NOMIME is set, use uuencode.
if [ ! -z ${EXP_NOMIME} ]
then
# uuencode the mail message, NO MIME encoding.

    	TEXT=`gettext "uuencode selected, manual build of messages using uuencode"`
   	logfile "${TEXT}"
    	# SPLIT was based on mpack which uses size -
    	#     rough conversion from bytes to lines
    	EXP_EMAIL_SPLIT=`echo "${EXP_EMAIL_SPLIT} * .0162" | bc | awk -F. '{ print $1 }'`

    	# Get output, uuencoded
    	cat ${FILE} | \
    	uuencode "${EXP_FILE}" | \
    	split -l ${EXP_EMAIL_SPLIT} - ${EXP_FNAME}.body.

	# Mail header
    	gen_messageid
    	cat <<-EOF >${EXP_FNAME}.head
		Subject: ${EXP_SUBJECT}
		Message-ID: $msgid
EOF
    
	# Construct headers for each message
    	total=`ls -1 ${EXP_FNAME}.body.* | wc -l | sed -e 's/^ *//'`
    	if [ "${total}" -gt 1 ]
    	then
       		# More than one message, keep original header in first message
		mv ${EXP_FNAME}.body.aa ${EXP_FNAME}.first
		mv ${EXP_FNAME}.head ${EXP_FNAME}.body.aa
		echo >> ${EXP_FNAME}.body.aa
		cat ${EXP_FNAME}.first >> ${EXP_FNAME}.body.aa
		rm -f ${EXP_FNAME}.first

		msgnum=1
		gen_messageid
		mimeSplitId=$msgid
		lastmsgid=$topmsgid

		# Fix each message
		for j in ${EXP_FNAME}.body.*
		do
	    		# Clean up old mail header
	    		rm -f ${EXP_FNAME}.head

	    		# New mail header
	    		gen_messageid
	    		cat <<-EOF >${EXP_FNAME}.head
			Subject: ${EXP_SUBJECT} ($msgnum of $total)
			Message-ID: $msgid
EOF
	    		((msgnum=msgnum + 1))

	    		# Reconstruct message
	    		cat ${EXP_FNAME}.head > ${EXP_FNAME}.body
	    		test -n "$lastmsgid" && echo "References: $lastmsgid" >> ${EXP_FNAME}.body
	    		echo "" >> ${EXP_FNAME}.body
	    		cat $j >> ${EXP_FNAME}.body
	    		mv ${EXP_FNAME}.body $j

	    		lastmsgid=$msgid
		done

		# Clean up
		rm -f ${EXP_FNAME}.head ${EXP_FNAME}.body
    	else
		# Use multipart/mixed
		cat <<-EOF >${EXP_FNAME}.body
		Subject: ${EXP_SUBJECT}
		Message-ID: $msgid

EOF
		# put README in body
		cat ${EXP_TARGET}/README >>${EXP_FNAME}.body

		# now the attachment header
		cat <<-EOF >>${EXP_FNAME}.body

		--${EXP_FNAME}

EOF
    
		# now the attachment
		cat ${EXP_FNAME}.body.* >> ${EXP_FNAME}.body
		cat <<-EOF >>${EXP_FNAME}.body
		--${EXP_FNAME}--
EOF

		# Clean up
		rm -f ${EXP_FNAME}.head ${EXP_FNAME}.body.*
    	fi

else
	# Attempt to use mpack if available (check in C locale)
	LC_SAVE=${LC_ALL}
	export LC_ALL=C
	MPACK=`type mpack 2>&1 | sed 's/.*is //'`
	export LC_ALL=${LC_SAVE}

	# No mpack, use our own method
	if echo "${MPACK}" | egrep -s -e "no"
	then
    		# logfile "mpack not available, manual build of MIME messages"
    		TEXT=`gettext "mpack not available, manual build of MIME messages"`
   	 	logfile "${TEXT}"
    		# SPLIT was based on mpack which uses size -
    		#     rough conversion from bytes to lines
    		EXP_EMAIL_SPLIT=`echo "${EXP_EMAIL_SPLIT} * .0162" | bc | awk -F. '{ print $1 }'`

    		# Get output in base64
    		cat ${FILE} | \
    		uuencode "${EXP_FILE}" | \
    		nawk -f ${EXP_LIB}/uu2b64 | \
    		split -l ${EXP_EMAIL_SPLIT} - ${EXP_FNAME}.body.

    		# Mail header
    		gen_messageid
    		cat <<-EOF >${EXP_FNAME}.head
			Subject: ${EXP_SUBJECT}
			Message-ID: $msgid
			MIME-Version: 1.0
			Content-type: application/octet-stream; name="${EXP_FILE}"
			Content-description: explorer output file: ${EXP_FNAME} (tar/compressed)
			Content-transfer-encoding: base64
EOF
    
    		# Construct headers for each message
    		total=`ls -1 ${EXP_FNAME}.body.* | wc -l | sed -e 's/^ *//'`
    		if [ "${total}" -gt 1 ]
    		then
        		# More than one message, keep original header in first message
			mv ${EXP_FNAME}.body.aa ${EXP_FNAME}.first
			mv ${EXP_FNAME}.head ${EXP_FNAME}.body.aa
			echo >> ${EXP_FNAME}.body.aa
			cat ${EXP_FNAME}.first >> ${EXP_FNAME}.body.aa
			rm -f ${EXP_FNAME}.first

			msgnum=1
			gen_messageid
			mimeSplitId=$msgid
			lastmsgid=$topmsgid

			# Fix each message
			for j in ${EXP_FNAME}.body.*
			do
	    		# Clean up old mail header
	    		rm -f ${EXP_FNAME}.head

	    		# New mail header
	    		gen_messageid
	    		cat <<-EOF >${EXP_FNAME}.head
			Subject: ${EXP_SUBJECT} ($msgnum of $total)
			Message-ID: $msgid
			MIME-Version: 1.0
			Content-type: Message/partial; id="$mimeSplitId"; number=${msgnum}; total=$total
EOF
	    		((msgnum=msgnum + 1))

	    		# Reconstruct message
	    		cat ${EXP_FNAME}.head > ${EXP_FNAME}.body
	    		test -n "$lastmsgid" && echo "References: $lastmsgid" >> ${EXP_FNAME}.body
	    		echo "" >> ${EXP_FNAME}.body
	    		cat $j >> ${EXP_FNAME}.body
	    		mv ${EXP_FNAME}.body $j

	    		lastmsgid=$msgid
			done

			# Clean up
			rm -f ${EXP_FNAME}.head ${EXP_FNAME}.body
    		else
			# Use multipart/mixed
			cat <<-EOF >${EXP_FNAME}.body
			Subject: ${EXP_SUBJECT}
			Message-ID: $msgid
			MIME-Version: 1.0
			Content-type: multipart/mixed; boundary="${EXP_FNAME}"

			This is a multi-part message in MIME format.
			--${EXP_FNAME}
			Content-type: text/plain; charset=us-ascii
			Content-transfer-encoding: 7bit

EOF
			# put README in body
			if [ -f ${EXP_TARGET}/README ]
			then
				cat ${EXP_TARGET}/README >>${EXP_FNAME}.body
			else
				gzcat ${EXP_FNAME}|tar xf - ${EXP_TARGET}/README
			fi

			# now the attachment header
			cat <<-EOF >>${EXP_FNAME}.body

			--${EXP_FNAME}
			Content-type: application/octet-stream; name="${EXP_FILE}"
			Content-description: explorer output file: ${EXP_FNAME} (tar/compressed)
			Content-transfer-encoding: base64

EOF
    
			# now the attachment
			cat ${EXP_FNAME}.body.* >> ${EXP_FNAME}.body
			cat <<-EOF >>${EXP_FNAME}.body
			--${EXP_FNAME}--
EOF

			# Clean up
			rm -f ${EXP_FNAME}.head ${EXP_FNAME}.body.*
    		fi

		# Got mpack, use it
	else
    	# logfile "using mpack to construct MIME messages"
    	TEXT=`gettext "using mpack to construct MIME messages"`
    	logfile "${TEXT}"
    	logfile "MPACK=${MPACK}"

    	# Use mpack to pack in MIME format
    	${MPACK} -m ${EXP_EMAIL_SPLIT} -s "${EXP_SUBJECT}" \
        	-d "${EXP_TARGET}/README" -o ${EXP_FNAME}.body ${FILE}
	fi
fi

# Mail output
total=`ls -1 ${EXP_FNAME}.body* | wc -l | sed -e 's/^ *//'`

# logfile NOTNOISE "${EXP_FILE} split into $total parts"
TEXT=`gettext "%s split into %s parts"`
TEXT=`printf "${TEXT}" "${EXP_FILE}" "${total}"`
logfile NOTNOISE "${TEXT}"

# Rename if only one part
[ -f ${EXP_FNAME}.body ] && mv ${EXP_FNAME}.body ${EXP_FNAME}.body.01

# Set from address?
if [ -n "${EXP_REPLY}" ]
then
    ARGS="-oi -t -f ${EXP_REPLY}"
else
    ARGS="-oi -t"
fi

# Send each part
for j in ${EXP_FNAME}.body*
do
    # Send message to sendmail
    {
	echo To: ${EXP_MAILTO}
	test -n "${EXP_REPLY}" && echo "From: ${EXP_REPLY}"
	test -n "${EXP_REPLY}" && echo "Reply-to: ${EXP_REPLY}"
	cat $j
    } | /usr/lib/sendmail ${ARGS}
done

# logfile NOTNOISE "${EXP_FILE} sent via mail to ${EXP_MAILTO}"
TEXT=`gettext "%s sent via mail to %s"`
TEXT=`printf "${TEXT}" "${EXP_FILE}" "${EXP_MAILTO}"`
logfile NOTNOISE "${TEXT}"

# Clean up
rm -rf ${EXP_FNAME}.body* ${DES}
