#!/bin/sh

# Copyright (C) 2003, 2008 Free Software Foundation

# This script is Free Software, and it can be copied, distributed and
# modified as defined in the GNU General Public License.  A copy of
# its license can be downloaded from http://www.gnu.org/copyleft/gpl.html

# Written by Tom Tromey <tromey@redhat.com>

# mkdiff version 2.0

# Usage: mkdiff [OPTION]... [FILE]...
# Make a patch suitable for submission to the GCC patch tester.
# The diff is made relative to the top of the tree, but the FILE
# options are relative the working directory, so this can be invoked
# exactly as one would invoke "svn diff" or "git diff", depending on
# the working tree.

#   --email=ADDR      email address to use (default currentuser@gcc.gnu.org)
#   --branch=BRANCH   branch to use (default from working directory)
#   --revision=REV    revision to use (default from working directory)
#   --config=OPTSTR   set configure options (default none)
#   --make=OPTSTR     set make options (default none)
#   --check=OPTSTR    set check options (default none)

#   --help            print this help, then exit
#   --version         print version number, then exit

# Unrecognized options are assumed to be options to "svn diff"
# or "git diff".

if test -d .svn; then
    vc=svn
elif git status -a > /dev/null 2>&1; then
    vc=git
else
    echo "$name: not in SVN or git-controlled directory" 1>&2
    exit 1
fi

name=mkdiff

diffopts=
email=`id -un`@gcc.gnu.org
branch=
revision=
files=
configopts=
makeopts=
checkopts=

# Parsing loop taken from Alexandre Oliva's cvs scripts.
while test $# -gt 0; do
    case "$1" in
    -v|--version)
	sed '/^# '$name' version / { s/^# //; p; }; d' < $0
	exit 0
	;;
    -\?|-h)
	sed '/^# usage:/ { s/^# //; p; }; d' < $0 &&
	echo
	echo "run \`$name --help | more' for full usage"
	exit 0
	;;
    --help)
	sed '/^# '$name' version /,/^[^#]/ { /^[^#]/ d; s/^# //; p; }; d' < $0
	exit 0
	;;
    --email=*)
        email=`echo "$1" | sed -e 's,--email=,,'`
	;;
    --branch=*)
        branch=`echo "$1" | sed -e 's,--branch=,,'`
	;;
    --revision=*)
        revision=`echo "$1" | sed -e 's,--revision=,,'`
	;;
    --check=*)
        checkopts=`echo "$1" | sed -e 's,--check=,,'`
	;;
    --make=*)
        makeopts=`echo "$1" | sed -e 's,--make=,,'`
	;;
    --config=*)
        configopts=`echo "$1" | sed -e 's,--config=,,'`
	;;
    -*)
	diffopts="$diffopts $1"
	;;
    *)
        files="$files $1"
	;;
    esac
    shift
done

if test $vc = svn; then
    if test -z "$branch"; then
	repo=`svn info | grep '^URL:' | sed -e 's,^URL: ,,'`
	case $repo in
	    */gcc.gnu.org/svn/gcc/trunk*)
		branch=trunk
		;;
	    */gcc.gnu.org/svn/gcc/branches/*)
		branch=`echo $repo | sed 's,.*/branches/\([^/]*\).*$,\1,'`
		;;
	    *)
		echo "$name: unrecognized repository, use --branch: $repo" 1>&2
		exit 1
		;;
	esac
    fi

    if test -z "$revision"; then
	revision=`svn info | grep '^Revision:' | sed -e 's,^Revision: ,,'`
    fi
else
    if test -z "$revision"; then
	revision=`git log | sed -e 's,^commit *,,' -e 1q`
    fi
fi


here=`pwd`
orig=$here
dirname=

cdie() {
    cd $1 || {
	echo "$name: couldn't cd to $1 from `pwd`" 1>&2
	exit 1
    }
}

if test $vc = svn; then
    while test -e ../.svn; do
	cdie ..
	next=`basename "$here"`
	here=`dirname "$here"`
	if test -z "$dirname"; then
	    dirname="$next/"
	else
	    dirname="$next/$dirname"
	fi
    done
    projecttestdir=.
else
    # We want to find the project type, but we don't have to bother
    # with computing the directory properly -- git diff handles this
    # itself.
    save=`pwd`
    while ! test -e .git; do
	cdie ..
    done
    projecttestdir=`pwd`
    cd $save
fi

if test -d $projecttestdir/gcc; then
    projecttype=gcc
elif test -d $projecttestdir/gdb; then
    projecttype=gdb
else
    echo "$name: couldn't determine project type in $projecttestdir" 1>&2
    exit 1
fi

if test -z "$files"; then
    files=.
fi

# We don't use filterdiff -addprefix since that does not seem to
# handle Index: lines.
newfiles=
for file in $files; do
    newfiles="$newfiles $dirname$file"
done

echo "projecttype:$projecttype"
echo "email:$email"
if test $vc = svn; then
    echo "branch:$branch"
fi
echo "revision:$revision"
echo "configure:$configopts"
echo "make:$makeopts"
echo "check:$checkopts"
echo
$vc diff $diffopts $newfiles
