#!/bin/bash

# Copyright: 2010-2013, gregor herrmann <gregoa@debian.org>
# Licensed under the same terms as Perl (Artistic | GPL-1+)

# to be run from source directory

# environment:
# - BUILDDIR, defaults to ".."
# - ARCH, defaults to arch of .changes file

BUILDDIR=${BUILDDIR:-".."}
if [ ! -d "$BUILDDIR" ] ; then
	echo "E: Build directory \"$BUILDDIR\" does not exist."
	echo "   Maybe you forgot to set the BUILDDIR variable?"
	echo "   Exiting ..."
	exit 1
fi

rm -rf $BUILDDIR/*obsolete*
rm -f  $BUILDDIR/*_source.changes
rm -f  $BUILDDIR/*.dsc.asc

PACKAGE=$(head -1 debian/changelog | perl -pe 's|^([^\s]+) .*|$1|')
VERSION=$(head -1 debian/changelog | perl -pe 's|^.+ \((?:\d:)?(.+)\) .*|$1|')

if [ -z "$ARCH" ] ; then
	CHANGES=$(ls -1rt $BUILDDIR/${PACKAGE}_${VERSION}_*.changes | tail -n 1)
	ARCH=$(basename ${CHANGES##$BUILDDIR/${PACKAGE}_${VERSION}_} .changes)
else
	CHANGES=$BUILDDIR/${PACKAGE}_${VERSION}_${ARCH}.changes
fi

BUILD=$BUILDDIR/${PACKAGE}_${VERSION}_${ARCH}.build
DSC=$BUILDDIR/${PACKAGE}_${VERSION}.dsc

if [ ! -r "$CHANGES" -o ! -r "$DSC" ] ; then
	echo "E: Can't read \"$CHANGES\" or \"$DSC\"."
	echo "   Is BUILDDIR set correctly: \"$BUILDDIR\"?"
	echo "   Exiting ..."
	exit 1
fi

FORMAT=$(grep ^Format: $DSC | cut -f2 -d" ")

echo "lintian:"
echo "========"
#lintian -i -I --show-overrides --pedantic --color auto "$CHANGES"
#~/.lintianrc, lintian 2.5.1
lintian "$CHANGES"

if [ -x /usr/bin/blhc -a -r "$BUILD" ] && ! egrep -q "^Architecture: all$" "$DSC" ; then
	echo "blhc:"
	echo "===="
	blhc --buildd "$BUILD"
fi

read -n 1 -p "debc? y/N " DEBC
if [ "$DEBC" = "y" ]; then 
	debc -a $ARCH $CHANGES | less
fi
echo

read -n 1 -p "piuparts? y/N " PIU
if [ "$PIU" = "y" ]; then
	CHROOT=
	[ -f /var/cache/pbuilder/base.tar.gz ] && CHROOT="--pbuilder"
	[ -d /var/cache/pbuilder/base.cow ] && CHROOT="--existing-chroot /var/cache/pbuilder/base.cow"
	sudo /usr/sbin/piuparts \
		$CHROOT -t ${TMPDIR:-/tmp} \
		--warn-on-others --skip-logrotatefiles-test --minimize \
		--log-file="${CHANGES}_piuparts.log" \
		"$CHANGES"
fi
echo

PKGVER=$(echo ${PACKAGE}-${VERSION} | perl -pe 's;^(.+)(?:-.+)$;$1;')
AUTOPATCH=debian/patches/debian-changes-${VERSION}
DIFFGZ=$BUILDDIR/${PACKAGE}_${VERSION}.diff.gz
DEBGZ=$BUILDDIR/${PACKAGE}_${VERSION}.debian.tar.gz
DEBBZ2=$BUILDDIR/${PACKAGE}_${VERSION}.debian.tar.bz2
DEBXZ=$BUILDDIR/${PACKAGE}_${VERSION}.debian.tar.xz
[ -e "$DEBGZ" ] || DEBGZ="$DEBBZ2"
[ -e "$DEBGZ" ] || DEBGZ="$DEBXZ"

if [ "$FORMAT" = "1.0" -a -e "$DIFFGZ" ] && zgrep -E "^\+\+\+ " "$DIFFGZ" | grep -q -v $PKGVER/debian; then
	echo
	echo "changes in .diff.gz outside debian/!"
	read -n 1 -p "view .diff.gz? y/N " VIEWDIFFGZ
	if [ "$VIEWDIFFGZ" = "y" ]; then
		filterdiff -z -x "$PKGVER/debian/*" "$DIFFGZ" | colordiff | /usr/bin/less -R
	fi
	echo
	echo "maybe: mkdir -p debian/patches && diff2patches $DIFFGZ"
	echo
elif [ "$FORMAT" = "3.0" -a -e "$DEBGZ" ] && tar tvf "$DEBGZ" | grep -q "$AUTOPATCH"; then
	echo
	echo "automatically created patch in .debian.tar.gz/.bz2/.xz!"
	read -n 1 -p "view .debian.tar.gz/.bz2/.xz? y/N " VIEWDEBGZ
	if [ "$VIEWDEBGZ" = "y" ]; then
		tar xf "$DEBGZ" "$AUTOPATCH" --to-stdout | colordiff | /usr/bin/less -R
	fi
	echo
	echo "maybe: tar xf $DEBGZ $AUTOPATCH"
	echo
else
	read -n 1 -p "ready for upload? y/N " UPLOAD
	if [ "$UPLOAD" = "y" ]; then
		echo
		cat "$CHANGES"
		echo
		echo dput $(readlink -f "$CHANGES")
	fi
	echo
fi
