#!/bin/sh

set -e

# This tool opens a new terminal window running reportbug against wnpp,
# with the content of the ITP file pre-filled in the report body, so
# that the ITP only needs to be reviewed in an editor and sent.
#
# Usage: pkgos-reportbug [itp-file] [subject]
# If no file is specified, itp.txt is used when it exists in the
# current directory. The subject defaults to:
# ITP: <package name> -- <description>, parsed out of the ITP file.
#
# Environment variables:
#   PKGOS_TERMINAL: force a terminal program (ex: konsole)
#   PKGOS_DEBPIPY_NO_REPORTBUG: if set, never open a window
# If there is no X display or no terminal program, the ITP file and the
# reportbug command to run are displayed instead.

if [ -n "${1}" ] ; then
	ITP_FILE=${1}
elif [ -e itp.txt ] ; then
	ITP_FILE=itp.txt
else
	echo "Usage: ${0} [itp-file] [subject]"
	echo "If no file is specified, itp.txt is used when it exists in the"
	echo "current directory."
	exit 1
fi

SUBJECT=${2}

if ! [ -e "${ITP_FILE}" ] ; then
	echo "Could not find the ITP file: ${ITP_FILE}"
	exit 1
fi

parse_itp_field () {
	# $1 = field label as found at the start of the line (ex: "* Package name")
	sed -n "s/^[[:space:]]*${1}[ ]*:[ ]*//p" "${ITP_FILE}" | head -n1
}

if [ -z "${SUBJECT}" ] ; then
	ITP_PKG=$(parse_itp_field '\* Package name')
	ITP_DESC=$(parse_itp_field 'Description')
	SUBJECT="ITP: ${ITP_PKG} -- ${ITP_DESC}"
fi

# A reportbug editor wrapper is used instead of --body-file, because
# --body-file makes reportbug skip the editor entirely, and send the
# report without a chance to review it. The wrapper inserts the ITP
# text into the report body, then runs the user's editor. The text
# isn't inserted twice: the ITP template of reportbug has a placeholder
# license value, which always starts with a parenthesis, unlike ours
# (which always starts with a letter).
# Severity is set to wishlist, as mandated for ITPs, and the additional
# CC menu is disabled, as there's nothing to add for an ITP.
EDITOR_WRAPPER=$(mktemp /tmp/pkgos-reportbug-editor.XXXXXX)
cat >${EDITOR_WRAPPER} <<WRAPEOF
#!/bin/sh
if [ -n "\$1" ] && ! grep -q '^\* License[ ]*:[ ]*[A-Za-z]' "\$1" ; then
	echo "" >>"\$1"
	echo "" >>"\$1"
	cat "${ITP_FILE}" >>"\$1"
fi
exec \${VISUAL:-\${EDITOR:-sensible-editor}} "\$@"
WRAPEOF
chmod +x ${EDITOR_WRAPPER}

REPORTBUG_CMD="reportbug -b wnpp --severity=wishlist --no-cc-menu --subject=\"${SUBJECT}\" --editor=${EDITOR_WRAPPER}"

find_terminal () {
	if [ -n "${PKGOS_TERMINAL}" ] && which "${PKGOS_TERMINAL}" >/dev/null 2>&1 ; then
		echo "${PKGOS_TERMINAL}"
		return 0
	fi
	case "${XDG_CURRENT_DESKTOP}" in
	*MATE*)
		which mate-terminal >/dev/null 2>&1 && echo mate-terminal && return 0 ;;
	*GNOME*|*Cinnamon*)
		which gnome-terminal >/dev/null 2>&1 && echo gnome-terminal && return 0 ;;
	*KDE*|*Plasma*)
		which konsole >/dev/null 2>&1 && echo konsole && return 0 ;;
	*XFCE*)
		which xfce4-terminal >/dev/null 2>&1 && echo xfce4-terminal && return 0 ;;
	*LXQt*)
		which qterminal >/dev/null 2>&1 && echo qterminal && return 0 ;;
	*LXDE*)
		which lxterminal >/dev/null 2>&1 && echo lxterminal && return 0 ;;
	esac
	for TERM_PROG in x-terminal-emulator mate-terminal gnome-terminal konsole xfce4-terminal qterminal lxterminal xterm ; do
		if which ${TERM_PROG} >/dev/null 2>&1 ; then
			echo ${TERM_PROG}
			return 0
		fi
	done
	return 1
}

if [ -n "${DISPLAY}" ] && [ -z "${PKGOS_DEBPIPY_NO_REPORTBUG}" ] && TERM_PROG=$(find_terminal) ; then
	case ${TERM_PROG} in
	mate-terminal|gnome-terminal)
		${TERM_PROG} --title="${SUBJECT}" -- bash -c "${REPORTBUG_CMD}" ;;
	xfce4-terminal)
		xfce4-terminal --title="${SUBJECT}" -x bash -c "${REPORTBUG_CMD}" ;;
	*)
		${TERM_PROG} -e bash -c "${REPORTBUG_CMD}" ;;
	esac
else
	echo "===> Please run the following command, then review and send the ITP:"
	echo "${REPORTBUG_CMD}"
	echo "===> ITP text (also saved in ${ITP_FILE}):"
	cat "${ITP_FILE}"
fi
