#!/bin/bash
#
# given the dsc and the original tarball, this script gets it and generates a
# report to appear on REVU and to make the review easier :)
#
# 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.
# 
# Contributors: Fathi Boudra <fboudra@free.fr>

APPNAME="REVU-Report";
VERSION="0.6.1";
AUTHOR="Raphaël Pinson <raphink@ubuntu.com>";

USAGE="$0 [--help || -h][<upstream_tarball.{gz,tgz,bz2}> [<dscfile.dsc>]]";

TARBALL="$1";
DSCFILE="$2";

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 "$REPORT";

# Begin to build report
"$ECHO" -e "REVU report for package $BASENAME :\n\n " > "$REPORT";


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



"$ECHO" "Running revu-orig on the upstream tarball $TARBALL";
"$REVUORIG" "$TARBALL" "$DSCFILE";


# get TARBALL from detected archive in $ORIGREPORT
if [[ -n `"$GREP" "Upstream tarball name:" "$ORIGREPORT"` ]]; then
	TARBALL=`"$SED" -n '/Upstream tarball name: / s///p' < "$ORIGREPORT"`;
fi


if [[ -n `"$GREP" "W: Debian-native package" "$ORIGREPORT"` ]]; then
	"$ECHO" -e `${GREP} "W: Debian-native package" "$ORIGREPORT" | ${SED} -e 's/$/\n\n/'` >> "$REPORT";
fi


if [[ -n `"$GREP" "debian/watch" "$ORIGREPORT"` ]]; then
        "$GREP" "debian/watch" "$ORIGREPORT" | "$SED" -e 's/$/\n\n/' >> "$REPORT";
fi


"$ECHO" "Running revu-build on the dsc file $DSCFILE";
"$REVUBUILD" "$DSCFILE";

if [[ -a "FTBFS" ]]; then
	"$ECHO" -e "E: This package failed to build from source in REVU! \
Try to update your pbuilder and run revu-report again.\n\n" >> "$REPORT";
fi

"$ECHO" -e "* Upstream tarball: \n" >> "$REPORT";

if [[ -n `"$GREP" "W: New upstream version available." "$ORIGREPORT"` ]]; then
	"$GREP" "W: New upstream version available." "$ORIGREPORT" | "$SED" -e 's/^/\t/' -e 's/$/\n/' >> "$REPORT";
fi

if [[ -a "$MD5FILE" ]]; then
	if [[ `"$GREP" "$TARBALL" "$MD5FILE" | "$AWK" {'print $1'}` != `"$GREP" "orig.tar.gz" "$MD5FILE" | "$AWK" {'print $1'}` ]]; then
		if [[ -n `"$FILE" "$TARBALL" | "$GREP" "gzip"` ]]; then
	        	"$ECHO" -e "\tW: The upstream tarball and the orig have \
different md5 sums. It should not be so, since the tarball is a tar.gz one. \
Unless there is a good reason to modify the tarball (which will appear next line), \
this package should use the upstream tarball as orig.tar.gz.\n" >> "$REPORT";
		else
			"$ECHO" -e "\tThe upstream tarball and the orig have \
different md5 sums. This seems normal since the upstream tarball is not a \
tar.gz one. Check next line to get sure nothing was changed in the tarball when \
it was repackaged though.\n" >> "$REPORT";
		fi
	else
        	"$ECHO" -e "\tThe upstream tarball and the orig have identical \
md5 sums.\n" >> "$REPORT";
	fi
else
	"$ECHO" -e "\tW:There is no md5 sums report available. This is probably \
because this package is a Debian native one.\n" >> "$REPORT";
fi


if [[ -a "$TARBALLDIFF" ]]; then
	if [[ -s "$TARBALLDIFF" ]]; then
		"$ECHO" -e "\tW: The upstream tarball was modified! See \
${TARBALLDIFF} for the diff.\n\n" >> "$REPORT";
	else
		"$ECHO" -e "\tThe upstream tarball is identical to the \
orig.\n\n" >> "$REPORT";
	fi
else
	"$ECHO" -e "\tW:There is no tarball diff report available. This is \
probably because this package is a Debian native one.\n\n" >> "$REPORT";
fi


"$ECHO" -e "* Debuild test: \n" >> "$REPORT";

if [[ -a "$DEBUILDTEST" ]]; then
	if [[ -n `"$GREP" -v "orig/debian/" "$DEBUILDTEST"` ]]; then
		"$ECHO" -e "\tW: Building this package modifies or generates \
files not in debian/. See $DEBUILDTEST and consider \
adding clean rules to debian/rules.\n\n" >> "$REPORT";
	else
		"$ECHO" -e "\tThe debuild test (debuild && debuild -S) didn't \
generate files outside of debian/.\n\n" >> "$REPORT";
	fi
else
	"$ECHO" -e "\tW:No build test report was found in the directory. It \
may be because the package FTBFS.\n\n" >> "$REPORT";
fi

"$ECHO" -e "* Lintian and Linda: \n" >> "$REPORT";

if [[ -s "$BASENAME.lintian" ]]; then
	"$ECHO" -e "\tW: Lintian is not happy with this package. \
See "$BASENAME.lintian" for more informations.\n" >> "$REPORT";
else
	"$ECHO" -e "\tLintian is happy with this package.\n" >> "$REPORT";
fi

if [[ -s "$LINDA" ]]; then
	"$ECHO" -e "\tW: Linda is not happy with this package. \
See "$LINDA" for more informations.\n\n" >> "$REPORT";
else
	"$ECHO" -e "\tLinda is happy with this package.\n\n" >> "$REPORT";
fi

exit 0;
