#! /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>

# submit-patch version 2.0

# Usage: submit-patch [OPTION]... PATCH-FILE [NAME]
# Regression test a GCC patch.

#   --host=HOST       host to use
#   --dir=DIR         directory to use on host

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

# If NAME is not given, the basename of the patch file is used as
# the patch name.
name=submit-patch
repeat="test $# -gt 0"

host=gcc13.fsffrance.org
# This is used to construct the patch dir if --dir is not given.
patchbase=/home/tromey/auto-test-

# Parsing loop taken from Alexandre Oliva's cvs scripts.
while $repeat; 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
	;;
    --host=*)
        host=`echo "$1" | sed -e 's,--host=,,'`
	shift
	;;
    --dir=*)
        patchdir=`echo "$1" | sed -e 's,--dir=,,'`
	shift
	;;
    --)
	shift
	repeat=false
	;;
    -*)
	echo "$name: unrecognized option '$1'" 1>&2
	echo "$name: Try '$name --help' for more information" 1>&2
	exit 1
	;;
    *)
	repeat=false
	;;
    esac
done

file=$1
shift
if test -z "$file"; then
   (sed '/^# usage:/ { s/^# //; p; }; d' < $0 &&
      echo
      echo "run \`$name --help | more' for full usage") 1>&2
    exit 1
fi

patch=$1
shift
if test -z "$patch"; then
   if test "x$file" = "x-"; then
      echo "$name: NAME argument required if patch is from stdin" 1>&2
      exit 1
   else
      patch=`basename $file`
   fi
fi

if test "x$file" != "x-"; then
    if ! grep -s "^email:" $file > /dev/null; then
	echo "$name: no email: line in $file; did you use mkdiff?" 1>&2
	exit 1
    fi
fi

if test -z "$patchdir"; then
    if test "x$file" = "x-"; then
	echo "$name: --dir required if patch is from stdin" 1>&2
	exit 1
    fi
    project=`sed -n -e 's/^projecttype://p' -e '/^$/q' $file`
    patchdir=$patchbase$project/patches
fi

# Only run filterdiff on a known non-git patch.
filter=cat
if test "x$file" != "x-"; then
    if ! grep -s "diff --git" $file > /dev/null; then
	filter="filterdiff -x '*ChangeLog*'"
    fi
fi

# FIXME: may be nice to auto-start the patch tester.
# Use 'cat' here so we can accept "-".
cat $file | $filter | ssh $host cat '>' $patchdir/`id -un`-$patch
