#!/bin/sh
set -e
#  Copyright (C) 2008 Neil Williams <codehelp@debian.org>
#
#  This package 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/>.

# not intended to be packaged until the buildd.emdebian.org
# pseudo-package is available.
PROG=embug_report

. /usr/lib/emdebian-tools/empbuilderlib

function usagehelp () {
    # print out help message
    cat <<EOF
$PROG - emdebian target package bug helper
version $OURVERSION

Syntax: $PROG [OPTIONS] [COMMAND] -- [REPORTBUG_OPTIONS]

Commands:
   --closes NUMBER:      The bug number to close (if not already
                          specified in the changelog itself).
-?|-h|--help|--version:  Print this help message and exit

Options:
-a|--arch:               Override the default cross-build architecture
                           (from dpkg-cross).

Specify any options for reportbug after '--' (options only, package
selection is done by $PROG.)

$PROG parses debian/changelog to obtain basic data about
the package.

If the Emdebian changelog contains the normal (Closes: #12345) entry or
if the --closes command is given, a log file is prepared to help close
the bug. This log file needs to be sent on using your normal email
client - at least until certain changes are made in bugs.debian.org.

If no changes file exists, the build log is checked for a build failure
message and a log file is prepared with information on the build failure.
The log file is then passed to reportbug so that you can add extra
information and automatically include the data to either a new bug or
as a follow-up message to an existing bug.

$PROG takes care of the package selection.

$PROG also ensures that new bugs and follow-up messages
are only sent to the bug tracker and not to the maintainer - this will
be resolved once the buildd.emdebian.org pseudo-package is available.

EOF
}

ARCH=`perl -MDebian::DpkgCross -e '&read_config; print &get_architecture;'`
RBUG=0
while [ -n "$1" ]; do
case "$1" in
	--help|-h|-\?|--version)
		usagehelp
		exit;
	;;
	-a|--arch)
		shift
		ARCH=$1
		# chomp the argument to --arch
		shift
	;;
	--closes)
		shift
		CLOSES=$1
		shift
	;;
	-- )
		shift
		RBUG=1
		continue
	;;
	*)
		if [ $RBUG == 0 ]; then
			echo "Unrecognised option: $1"
			echo
			usagehelp
			exit;
		else
			RBUGOPTS="$RBUGOPTS $1"
			shift
		fi
	;;
esac
done

check_pbuilder()
{
	WORKDIR=`perl -e 'use Cwd; use Emdebian::Tools; use Config::Auto; use Debian::DpkgCross; \
	&read_config; \
	my $w = &get_workdir; \
	$w = cwd if (! -d $w); \
	print $w;';`
	INIT=`echo $SRC | cut -b1`
	BUILD="$WORKDIR/pbuilder/result/$INIT/$SRC/trunk/${SRC}_${VER}_${ARCH}.build"
	if [ -f "$BUILD" ]; then
		TAIL=`tail -20 $BUILD > ../emdebian-tail.log`
		LOG="-i ../emdebian-tail.log --report-quiet"
	fi
}

PWD=`pwd`
echo "Working in: $PWD"
VER=`parsechangelog | grep Version | cut -d' ' -f2`
if [ ! -f "debian/changelog" ]; then
	echo "Please run $0 in an Emdebian package directory,"
	echo "or run $PROG --help for help."
	exit 1
fi
SRC=`parsechangelog | grep Source | cut -d' ' -f2`

BUILD="../${SRC}_${VER}_${ARCH}.build"
if [ -f "$BUILD" ]; then
	TAIL=`tail -20 $BUILD > ../emdebian-tail.log`
	LOG="-i ../emdebian-tail.log --report-quiet"
fi

if [ -z "$CLOSES" ]; then
	CLOSES=`parsechangelog | grep "^Closes" | cut -d' ' -f2`
fi
DM=`parsechangelog | grep Maintainer | cut -d' ' -f2-`
TEXT="The new version of $SRC has been uploaded to Emdebian."
SUMMARY1="A summary of the changes between this version and"
SUMMARY2="the previous Emdebian one is attached."
CNGH="../${SRC}_${VER}_${ARCH}.changes"
if [ -f "$CNGH" ]; then
	LOG="-i ../emdebian-close.log"
	echo Source: buildd.emdebian.org > ../emdebian-close.log
	echo >> ../emdebian-close.log
	# don't send the report at this stage.
	echo "Postponing closing the actual bug report until" >> ../emdebian-close.log
	echo "the buildd.emdebian.org pseudo-package is available." >> ../emdebian-close.log
	echo "Once re-assigned, send email to:" >> ../emdebian-close.log
	echo "$CLOSES-done@bugs.debian.org" >> ../emdebian-close.log
	echo >> ../emdebian-close.log
	echo $TEXT >> ../emdebian-close.log
	echo $SUMMARY1 >> ../emdebian-close.log
	echo $SUMMARY2 >> ../emdebian-close.log
	echo >> ../emdebian-close.log
	cat $CNGH >> ../emdebian-close.log
	echo "Now send ../emdebian-close.log to $CLOSES-quiet@bugs.debian.org"
	exit
else
	echo "Cannot find $CNGH - checking build-log instead $BUILD."
	FAIL=`grep "dpkg-buildpackage: failure" $BUILD || true`
	if [ -z "$FAIL" ]; then
		echo "No changes file, no failed build log either - checking chroot build."
		check_pbuilder
	fi
fi

echo reportbug $RBUGOPTS $LOG general
reportbug $RBUGOPTS $LOG general
