#!/bin/bash
#
# given the dsc, this script builds the package in a pbuilder and generate
# reports to appear on REVU and to make the review easier :)
#
# Copyright (C) 2006 Reinhard Tartler <siretart@tauware.de> & 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.
#
# Contributors: Fathi Boudra <fboudra@free.fr>

APPNAME="REVU-Build";
VERSION="0.6.1";
AUTHOR="Reinhard Tartler <siretart@tauware.de> & Raphaël Pinson <raphink@ubuntu.com>";

USAGE="$0 [--help || -h][<dscfile.dsc>]";


DSCFILE="$1";

source /var/lib/revu-tools/revu-tools-loadconfig


if [[ "$1" = "--help" || "$1" = "-h" ]]; then
        "$ECHO" "${APPNAME} v.${VERSION} by ${AUTHOR}";
        "$ECHO" "usage: ${USAGE}";
        exit 0;
fi

# try and detect dsc file

if [[ -z "$DSCFILE" ]]; then
        if [[ ! -a `"$LS" | "$GREP" .dsc` ]]; then
                "$ECHO" "No dsc file found in the current directory or several \
dsc files. Use the dsc file as argument if there are several ones:";
                "$ECHO" "usage: ${USAGE}";
                exit 1;
        else
                DSCFILE=`"$LS" | "$GREP" dsc`;
        fi
fi


BASENAME=`"$BASENAMECMD" "$DSCFILE" .dsc`;


if [ "${BASENAME}x" = "x" ]; then
        "$ECHO" "Basename is empty. Something terribly went wrong."
        exit 1;
fi


APPNAME=`"$ECHO" "$BASENAME" | "$SED" -e 's/-[0-9]*[a-z]*[0-9]*$//'`;
DIRNAME=`"$ECHO" "$APPNAME" | "$SED" -e 's/_/-/'`;


# clean previous runs

rm -f "FTBFS";


if [[ -z "$DSCFILE" ]]; then
        "$ECHO" "usage: ${USAGE}";
        exit 1;
fi

if [ ! -r "$DSCFILE" ]; then
        "$ECHO" "Sorry, could not read ${DSCFILE}. Bailing out.";
        exit 1;
fi


PBUILDER="${NICE} ${PBUILDERNAME}";
PBUILDEROPTS="--buildresult $(pwd) --logfile ${BASENAME}.buildlog --hookdir ${PBUILDERHOOKS}";

"$ECHO" "Now building ${DSCFILE} with ${PBUILDERNAME}."

${PBUILDER} build ${PBUILDEROPTS} ${DSCFILE};

if [[ -n `"$GREP" "Failed autobuilding of package" ${BASENAME}.buildlog` ]]; then
	"$TOUCH" "FTBFS";
	exit 1;
fi

# now process resulting debs
for deb in `"$LS" *.deb`; do
	BASENAME=`"$BASENAMECMD" $deb .deb`;
	"$DPKGDEB" -I $deb > ${BASENAME}.info;
	"$DPKGDEB" -c $deb > ${BASENAME}.filelist;
	for h in {pre,post}{inst,rm}; do
		"$DPKGDEB" -I $deb $h > ${BASENAME}.${h};
	done
	"$DPKGDEB" -I $deb shlibs > ${BASENAME}.shlibs;
	"$LINTIAN" $deb > ${BASENAME}.lintian;
#	"$LINDA" $deb > ${BASENAME}.lintian;
done

exit 0;
