#!/bin/sh -
#
# gawkbug - create a bug report and mail it to the bug address
#
# All mail defaults to bug-gawk@gnu.org. Unlike the original bashbug
# script, we don't differentiate on the release status.
#
# Copyright (C) 1996-2022 Free Software Foundation, Inc.
#
#   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 3 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/>.

#
# configuration section:
#	these variables are filled in by the make target in Makefile
#
MACHINE="s390x"
OS="linux-gnu"
CC="gcc"
CFLAGS="-g -O2 -ffile-prefix-map=/build/gawk-CcrlfA/gawk-5.2.1=. -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG"
VERSION="5.2.1"
MACHTYPE="s390x-ibm-linux-gnu"

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

# Check if TMPDIR is set, default to /tmp
: ${TMPDIR:=/tmp}

#Securely create a temporary directory for the temporary files
TEMPDIR=$TMPDIR/gbug.$$
(umask 077 && mkdir "$TEMPDIR") || {
	echo "$0: could not create temporary directory" >&2
	exit 1
}

TEMPFILE1=$TEMPDIR/gbug1
TEMPFILE2=$TEMPDIR/gbug2
        
USAGE="Usage: $0 [--help] [--version] [bug-report-email-address]"
VERSTR="GNU gawkbug, version ${VERSION}"

do_help= do_version=

while [ $# -gt 0 ]
do
	case "$1" in
	--help)		shift ; do_help=y ;;
	--version)	shift ; do_version=y ;;
	--)		shift ; break ;;
	-*)		echo "gawkbug: ${1}: invalid option" >&2
			echo "$USAGE" >&2
			exit 2 ;;
	*)		break ;;
	esac
done

if [ -n "$do_version" ]
then
	echo "${VERSTR}"
	exit 0
fi

if [ -n "$do_help" ]
then
	echo "${VERSTR}"
	echo "${USAGE}"
	echo
	cat << HERE_EOF
Gawkbug is used to send mail to the Gawk maintainers
for when Gawk doesn't behave like you'd like, or expect.

Gawkbug will start up your editor (as defined by the shell's
EDITOR environment variable) with a preformatted bug report
template for you to fill in. The report will be mailed to the
bug-gawk mailing list by default.

Please see https://www.gnu.org/software/gawk/manual/html_node/Bugs.html
for bug reporting instructions.  Reading that information before
repoting a bug will make everyones' lives easier.

If you invoke gawkbug by accident, just quit your editor without
saving any changes to the template, and no bug report will be sent.
HERE_EOF
	exit 0
fi

# 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

BUGGAWK=bug-gawk@gnu.org

BUGADDR="${1-$BUGGAWK}"

if [ -z "$DEFEDITOR" ] && [ -z "$EDITOR" ]
then
	if [ -x /usr/bin/editor ]
	then
		DEFEDITOR=editor
	elif [ -x /usr/bin/vim ]
	then
		DEFEDITOR=vim
	elif [ -x /usr/bin/gvim ]
	then
		DEFEDITOR=gvim
	elif [ -x /usr/local/bin/ce ]
	then
		DEFEDITOR=ce
	elif [ -x /usr/local/bin/emacs ]
	then
		DEFEDITOR=emacs
	elif [ -x /usr/contrib/bin/emacs ]
	then
		DEFEDITOR=emacs
	elif [ -x /usr/bin/emacs ]
	then
		DEFEDITOR=emacs
	elif [ -x /usr/bin/xemacs ]
	then
		DEFEDITOR=xemacs
	elif [ -x /usr/bin/nano ]
	then
		DEFEDITOR=nano
	elif [ -x /usr/contrib/bin/jove ]
	then
		DEFEDITOR=jove
	elif [ -x /usr/local/bin/jove ]
	then
		DEFEDITOR=jove
	elif [ -x /usr/bin/vi ]
	then
		DEFEDITOR=vi
	else
		echo "$0: No default editor found: attempting to use vi" >&2
		DEFEDITOR=vi
	fi
fi


: ${EDITOR=$DEFEDITOR}

: ${USER=${LOGNAME-`whoami`}}

trap 'rm -rf "$TEMPDIR"; exit 1' 1 2 3 13 15
trap 'rm -rf "$TEMPDIR"' 0

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

if [ -f /usr/lib/sendmail ]
then
	RMAIL="/usr/lib/sendmail"
	SMARGS="-i -t"
elif [ -f /usr/sbin/sendmail ]
then
	RMAIL="/usr/sbin/sendmail"
	SMARGS="-i -t"
else
	RMAIL=rmail
	SMARGS="$BUGADDR"
fi

INITIAL_SUBJECT='[50 character or so descriptive subject here (for reference)]'

cat > "$TEMPFILE1" <<EOF
From: ${USER}
To: ${BUGADDR}
Subject: ${INITIAL_SUBJECT}

Configuration Information [Automatically generated, do not change]:
Machine: $MACHINE
OS: $OS
Compiler: $CC
Compilation CFLAGS: $CFLAGS
uname output: $UN
Machine Type: $MACHTYPE

Gawk Version: $VERSION

Attestation 1:
	I have read https://www.gnu.org/software/gawk/manual/html_node/Bugs.html.
	Yes / No  [ Choose one. If "No", then                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          