#!/bin/sh -
#
# fvwm-bug - 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.
#

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see: <http://www.gnu.org/licenses/>



PACKAGE="fvwm"
VERSION="2.7.0"
script=`basename $0`
address=""

while test $# -gt 0; do
	case "$1" in
		--help|-help|-h|-\?)
			cat <<EOF
This script is a part of $PACKAGE-$VERSION package.

Usage: $script [OPTIONS] [address]
Options:
	[-h]	[--help] [-?]
	[-v]	[--version] [-V]

address is an alternative email address to send the bug report to;
	by default, addresses of the local admin or developers are used
EOF
			exit 0 ;;

		--version|-version|-v|-V)
			echo "$VERSION"
			exit 0 ;;

		-*)
			echo >&2 "Unrecognized option $1 specified."
			echo >&2 "Run '$0 --help' to get the usage."
			exit 1 ;;

		*)
			if test x"$address" != x; then
				echo >&2 "You may specify only one address."
				echo >&2 "Run '$0 --help' to get the usage."
				exit 1
			fi
			address="$1" ;;
	esac
	shift
done

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

# If they don't have a preferred editor set, then use
if [ -n "$VISUAL" ]; then
  EDIT="$VISUAL"
elif [ -n "$EDITOR" ]; then
  EDIT="$EDITOR"
elif [ -x /bin/vitmp ]; then
  EDIT=vitmp
elif [ -x /usr/bin/vim ]; then
  EDIT=vim
elif [ -x /bin/vi ]; then
  EDIT=vi
else
  echo "${0##*/}: No default editor found: attempting to use vi" >&2
  EDIT=vi
fi

TEMP=`mktemp -t fvwm-bug.XXXXXXXXXX` || exit
TEMPx=`mktemp -t fvwm-bug.XXXXXXXXXX` || { rm -f -- "$TEMP"; exit 1; }

exit_handler()
{
	local rc=$?
	trap '' EXIT
	rm -f -- "$TEMP" "$TEMPx"
	exit $rc
}
trap exit_handler EXIT HUP INT PIPE TERM QUIT

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

# Who is mail to?
if test x"$address" = x; then
	LOCAL=
	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?"
		printf "Send report to (l)ocal, (w)orkers, (b)oth? "
		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
	BUGADDR="$address"
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


prefix=/usr
exec_prefix=/usr
datarootdir=${prefix}/share
datadir=/usr/share
libexecdir=/usr/lib
: ${FVWM_USERDIR:=unset}
FVWM_DATADIR=${datadir}/fvwm
FVWM_MODULEDIR=${libexecdir}/fvwm/2.7.0


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: $UN
compiler flags: i586-alt-linux-gcc -Wall -Wno-implicit-int -pipe -frecord-gcc-switches -Wall -g -O2 -flto=auto -march=i586 -mtune=generic -fno-strict-aliasing -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include

FVWM Version:	2.7.0
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

cp "$TEMP" "$TEMPx"

trap '' INT		# ignore interrupts while in editor

until $EDIT "$TEMP"; do
	echo "$0: editor \`$EDIT' 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."
	printf "$0: Do you want to give up? "

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

trap exit_handler INT	# restore trap on SIGINT

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

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

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

exit 0
