#!/bin/bash
#
# This is a pbuilder hook used to generate a report on 
# files created when running `debuild && debuild -S  -sa" 
# on a package. It is used on REVU (http://revu.tauware.de)
# to review packages to be included in Ubuntu.
#
# Copyright (C) 2006 Raphaël Pinson <raphink@ubuntu.com>
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


ECHO="/bin/echo";
APTGET="/usr/bin/apt-get";
MV="/bin/mv";
SED="/bin/sed";
BASENAMECMD="/usr/bin/basename";
DEBUILD="/usr/bin/debuild";
GZIP="/bin/gzip";
GREP="/bin/grep";

REPORT="debuild_test";
DIFFEXT="_${REPORT}.diff";

"$ECHO" "Installing devscripts to have debuild";
"$APTGET" install devscripts;

cd /tmp/buildd/;

"$ECHO" "Saving diff.gz files";

for difffile in *.diff.gz; do 
    "$MV" "$difffile" "$difffile"~;
done;

"$ECHO" "Saving dsc files";


# get dirname to cd in
DIRNAME=`"$ECHO" *.dsc | "$SED" -e 's/-[0-9]*[a-z]*[0-9]*.dsc$//' -e 's/_/-/'`;


for dscfile in *.dsc; do
    "$MV" "$dscfile" "$dscfile"~;
done;


"$ECHO" "Running debuild -S to finish debuild test";
cd "$DIRNAME";

"$DEBUILD" -S -sa;
cd ..;

"$ECHO" "Extracting debuild generated diff.gz files";
"$GZIP" -d *.diff.gz;

"$ECHO" "Generating debuild test report";
"$GREP" "^---.*orig*/" *.diff > "$REPORT";

"$ECHO" "Saving debuild diff files";
for difffile in *.diff; do
    basename=`"$BASENAMECMD" "$difffile" ".diff"`
    newname="${basename}${DIFFEXT}";
    "$MV" "$difffile" "$newname";
done;

"$ECHO" "Restoring saved diff.gz files";
for difffile in *diff.gz~; do
    newname=`"$ECHO" "$difffile" | "$SED" -e 's/diff.gz~/diff.gz/'`;
    "$MV" "$difffile" "$newname";
done;

"$ECHO" "Restoring saved dsc files";
for dscfile in *dsc~; do
    newname=`"$ECHO" "$dscfile" | "$SED" -e 's/.dsc~/.dsc/'`;
    "$MV" "$dscfile" "$newname";
done;

exit 0;
