#!/bin/bash
#
# given the original tarball, this script gets it and generates a few
# reports comparing it with the orig.tar.gz file. These reports appear on REVU
# 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-Orig";
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


# clean previous runs

"$RM" -rf "$MD5FILE" "$DIFFFILE" "$TARDIR" "$ORIGREPORT";


# 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


if [ ! -r "$DSCFILE" ]; then
        "$ECHO" "Sorry, could not read ${DSCFILE}. Bailing out.";
        exit 1;
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/_/-/'`;


# check for the extracted source in dir and dpkg-source if necessary
if [[ ! -a "$DIRNAME" ]]; then
	"$ECHO" "No extracted source found in the directory. Extracting it from dsc.";
	"$DPKGSRC" -x "$DSCFILE";
fi


# create $ORIGREPORT
"$ECHO" -e "REVU-orig report for package ${BASENAME}:\n\n" > "$ORIGREPORT";


# try and detect upstream tarball

if [[ -z "$TARBALL" ]]; then
	"$ECHO" "No tarball argument given. Trying to detect tarball automagically.";
	if [[ -a "$DIRNAME"/debian/watch ]]; then
		"$ECHO" "debian/watch found. Running uscan.";
		"$ECHO" -e "I: Using debian/watch to retrieve the upstream tarball.\n\n" >> "$ORIGREPORT";
		cd "$DIRNAME";
		if [[ -z `"$USCAN" --report` ]]; then
			# there is no new version. We want to try and get the current upstream
			"$ECHO" "Getting upstream url for the current version.";
			URL=`"$USCAN" --dehs | "$SED" -n '/^<upstream-url>/ s///p' | "$SED" -e 's/<\/upstream-url>//'`;
			cd ..;
			"$ECHO" -e "Upstream tarball url: ${URL}\n\n" >> "$ORIGREPORT";
			"$WGET" -nc "$URL";
		elif [[ -n `"$USCAN" --report | "$GREP" "Newest version"` ]]; then
			# there is a new version available.
			"$USCAN" --report | "$GREP" "Newest version" | "$SED" -e 's/^/W: New upstream version available./' -e 's/$/.\n\n/' >> ../"$ORIGREPORT";
			cd ..;
			exit 1;
		else
			cd ..;
			"$ECHO" -e "E: Error while running uscan. debian/watch maybe be incorrect. \ 
Couldn't retrieve upstream tarball.\n\n" >> "$ORIGREPORT";
			exit 1;
		fi
	else
		"$ECHO" -e "W: There is no debian/watch in this package. \
Although this is not compulsory, it helps maintaining \
the package properly.\n\n" >> "$ORIGREPORT";
	fi;
	# try and detect in the current directory
	if [[ -a `"$LS" | "$GREP" ".tar.gz" | "$GREP" -v ".orig.tar.gz"` ]]; then
	        # case of a tar.gz tarball, but not orig
		TARBALL=`"$LS" | "$GREP" ".tar.gz" | "$GREP" -v ".orig.tar.gz"`;
	elif [[ -a `"$LS" | "$GREP" ".tgz"` ]]; then
		TARBALL=`"$LS" | "$GREP" ".tgz"`;
	elif [[ -a `"$LS" | "$GREP" ".tar.bz2"` ]]; then
		TARBALL=`"$LS" | "$GREP" ".tar.bz2"`;
	elif [[ -a `"$LS" | "$GREP" ".tbz"` ]]; then
	        TARBALL=`"$LS" | "$GREP" ".tbz"`;
	else
                "$ECHO" -e "No upstream tarball found in the current directory \
or several possible files. Use the upstream tarball as argument if there are \
several ones:\nusage $0 [<upstream_tarball.{gz,tgz,bz2}> [<dscfile.dsc> [<report file>]]]";
        	exit 1;
	fi
fi


"$ECHO" -e "Upstream tarball name: ${TARBALL}\n\n" >> "$ORIGREPORT";


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

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


# try and detect orig tarball

if [[ -z `"$GREP" "$APPNAME.orig.tar.gz" "$DSCFILE"` ]]; then
	"$ECHO" -e "W: Debian-native package. revu-orig will fail on this package. \
Most packages should not be Debian-native, but use a .orig.tar.gz \
and a .diff.gz files instead.\n\n" >> "$ORIGREPORT";
	exit 1;
else
        ORIGBALL="$APPNAME"".orig.tar.gz";
fi



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

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


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


"$ECHO" "Generating md5 report in ${MD5FILE};";
"$MD5SUM" "$TARBALL" > "$MD5FILE";
"$MD5SUM" "$ORIGBALL" >> "$MD5FILE";


"$MKDIR" "$TARDIR";
cd "$TARDIR";


if [[ -n `"$FILE" ../"$TARBALL" | "$GREP" "bzip2"` ]]; then
         "$ECHO" "Extracting tar.bz2 upstream archive "$TARBALL" to extracted_tarballs/ ...";
         UPSTREAM_OUTPUT=`"$TAR" xvjf ../"$TARBALL"`;
         UPSTREAM=`"$ECHO" "$UPSTREAM_OUTPUT" | "$HEAD" -n1 | "$SED" -e 's/\/.*//'`;
elif [[ -n `"$FILE" ../"$TARBALL" | "$GREP" "gzip"` ]]; then
         "$ECHO" "Extracting tar.gz upstream archive "$TARBALL" to extracted_tarballs/ ...";
         UPSTREAM_OUTPUT=`"$TAR" xvzf ../"$TARBALL"`;
         UPSTREAM=`"$ECHO" "$UPSTREAM_OUTPUT" | "$HEAD" -n1 | "$SED" -e 's/\/.*//'`;
else
         "$ECHO" "Unrecognized upstream archive type. Aborting.";
         exit 1;
fi

"$MV" "$UPSTREAM" "$UPSTREAM"-upstream;

"$ECHO" "Extracting tar.gz orig archive to extracted_tarballs/ ...";
ORIG_OUTPUT=`"$TAR" -xvzf ../"$ORIGBALL"`;
ORIG=`"$ECHO" "$ORIG_OUTPUT" | "$HEAD" -n1 | "$SED" -e 's/\/.*//'`;
"$MV" "$ORIG" "$ORIG"-orig;

"$ECHO" "Generating diff report in upstream_orig.diff";
"$DIFF" -ru "$UPSTREAM"-upstream "$ORIG"-orig > ../upstream_orig.diff;

cd ../

exit 0;

