#!/bin/sh -f

if [ -z "$DISPLAY" ]; then
    exec mhn "$@"
fi

X=/tmp/smhn$$
trap "rm -f $X" 0 1 2 3 13 15

B="`basename $0`"
P="`mhparam -nocomponent $B`"

H=mhl.headers M=
F=
for A in $P $*; do
    case $A in
	-file|-form)
		F="$A"
		;;

	-show)	F=
		;;

	*)	  if [ "$F" = -file ]; then
		    L=$A F=
		elif [ "$F" = -form ]; then
		    H=$A F=
		    continue
		elif [ "$A" = "-" ]; then
		    L=$A
		else
		    L="`mhpath $A`"
		fi
		if [ -z "$M" ]; then
		    M="$L"
		else
		    M="$M $L"
		fi
		;;
    esac
done

for A in $M; do
    if [ $A = - ]; then
	cat > $X
	A=$X
    fi
    C=`scan -format %{content-type} -file $A | sed -e 's%;.*%%' | tr A-Z a-z`
    if [ -z "$C" ]; then
	T=text	S=plain
    else
	T=`echo "$C" | awk -F/ '{ print $1 }'`
	S=`echo "$C" | awk -F/ '{ print $2 }'`
    fi
    if [ "$T" != multipart \
	    -o \(    "$S" != mixed -a "$S" != alternative -a "$S" != digest \
		  -a "$S" != parallel \) ]; then
	mhn -show -form $H -file $A
    else
	MSGNAME="`basename $A`" \
	swish -messaging -messagebody $A -file /usr/local/lib/mh/showmime.tcl
    fi
done

exit 0
