#!/bin/sh -
#
# fvwmbug - create a bug report and mail it to the bug address
#         - adapted from equivalent `bashbug' script
#
# The bug address could depend on the release status of FVWM.  Currently
# it doesn't.
#

PATH=/bin:/usr/bin:/usr/local/bin:$PATH
export PATH

: ${EDITOR=emacs}

TEMP=`mktemp -q "/tmp/fvwm-bug.XXXXXX"`

# Figure out how to echo a string without a trailing newline
N=`echo 'hi there\c'`
case "$N" in
*c)	n=-n c= ;;
*)	n= c='\c' ;;
esac

trap 'rm -f $TEMP $TEMP.x; exit 1' 1 2 3 13 15
trap 'rm -f $TEMP $TEMP.x' 0

# Who is mail from?
: ${USER=${LOGNAME-`whoami`}}

# Who is mail to?
if test $# -eq 1 ; then
    BUGADDR=$1
elif test $# -eq 0 ; then
    LOCAL=root
    WORKERS=fvwm-workers@fvwm.org

    if test "$LOCAL"; then
	echo "Do you want to send the report to the local maintainer <$LOCAL>,"
	echo "the FVWM workers <$WORKERS>, or both?"
	echo $n "Send report to local, workers, both? " $c
	read ans
	case "$ans" in
	l*|L*) BUGADDR=$LOCAL;;
	f*|F*|w*|W*) BUGADDR=$WORKERS;;
	b*|B*) BUGADDR=$LOCAL,$WORKERS;;
	*) echo "[Defaulting to LOCAL]"; BUGADDR=$LOCAL;;
	esac
    fi
else
    echo >&2 "usage: fvwmbug [address]"
    exit 1
fi

UN=
if (uname) >/dev/null 2>&1; then
	UN=`uname -a`
fi

if [ -f /usr/lib/sendmail ] ; then
	RMAIL="/usr/lib/sendmail"
elif [ -f /usr/sbin/sendmail ] ; then
	RMAIL="/usr/sbin/sendmail"
else
	RMAIL=rmail
fi


: ${FVWM_DATADIR=/usr/local/share/fvwm}
: ${FVWM_MODULEDIR=/usr/local/libexec/fvwm}
: ${FVWM_USERDIR=unset}


cat > $TEMP <<EOF
From: ${USER}
To: ${BUGADDR}
Subject: [50 character or so descriptive subject here (for reference)]

Configuration Information [Automatically generated, do not change]:
uname output: $UN
Compiler: cc
Compilation CFLAGS: -O2 -fno-strict-aliasing -pipe
prefix: /usr/local

FVWM Version:	2.4.20
FVWM_MODULEDIR:	$FVWM_MODULEDIR
FVWM_DATADIR:	$FVWM_DATADIR
FVWM_USERDIR:	$FVWM_USERDIR

Description:
	[Detailed description of the problem, suggestion, or complaint.]

Repeat-By:
	[Describe the sequence of events that causes the problem
	to occur.]

Fix:
	[Description of how to fix the problem.  If you don't know a
	fix for the problem, don't include this section.]
EOF

chmod u+w $TEMP
cp $TEMP $TEMP.x

trap '' 2		# ignore interrupts while in editor

until $EDITOR $TEMP; do
	echo "$0: editor \`$EDITOR' exited with nonzero status."
	echo "$0: Perhaps it was interrupted."
	echo "$0: Type \`y' to give up, and lose your bug report;"
	echo "$0: type \`n' to re-enter the editor."
	echo $n "$0: Do you want to give up? $c"

	read ans
	case "$ans" in
	[Yy]*) exit 1 ;;
	esac
done

trap 'rm -f $TEMP $TEMP.x; exit 1' 2	# restore trap on SIGINT

if cmp -s $TEMP $TEMP.x
then
	echo "File not changed, no bug report submitted."
	exit
fi

echo $n "Send bug report? [y/n] $c"
read ans
case "$ans" in
[Nn]*)	exit 0 ;;
esac

${RMAIL} $BUGADDR < $TEMP || {
	cat $TEMP >> $HOME/dead.fvwmbug
	echo "$0: mail failed: report saved in $HOME/dead.fvwmbug" >&2
}

exit 0
