#!/bin/bash
#
# Copyright © 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
# Copyright © 2003 Ryan Murray <rmurray@debian.org>
#
# 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/>.
#
#######################################################################

set -e

tmpfile=/tmp/abort-current-build.$$
set +e
ps xjww | egrep ' (/bin/sh /usr/bin/dpkg-buildpackage -us -uc |\(dpkg-buildpacka\))' >$tmpfile
set -e
n="`wc -l $tmpfile | awk '{print $1}'`"
if [ "$n" -eq 0 ]; then
    echo "No dpkg-buildpackage process found" 1>&2
    rm -f $tmpfile
    exit 1
fi
if [ "$n" -ge 2 ]; then
    echo "More than one dpkg-buildpackage processes found:"
    echo ' PPID   PID  PGID   SID TTY TPGID  STAT  UID   TIME COMMAND'
    cat $tmpfile
    rm -f $tmpfile
    exit 1
fi
pgid=`awk '{print $3}' <$tmpfile`
echo "Killing pgid $pgid"
kill -15 -$pgid
rm -f $tmpfile
exit 0
