#!/bin/sh
# Backport the last change in HEAD, to a branch.
# Usage: svnbackport [-b branch] <file> [<file> ...]
#
# This is a port of the "cvsbackport" script:
# Initial author: Dirk Mueller
# Support for multiple command-line arguments, handling of patch rejects: David Faure
# Ported to SVN: Till Gerken
# Options and quote-safety: Matthew Woehlke
#
# This isn't the most sophisticated script ever, and might break. It needs to be
# used from within the repository so that it can guess the remote URL correctly.
#

#REPOSITORY=https://svn.kde.org/home/kde

usage() {
  echo "Usage: $0 [-b <branch>] <file> [<file> ...]"
  echo "  default branch is $DEF_BRANCH"
  exit
}

DEF_BRANCH=4.3
BRANCH=$DEF_BRANCH

while getopts 'b:-:h' opt ; do
  case $opt in
    b) BRANCH="$OPTARG";;
    *) usage;; # -h, --help will also trip this
  esac
done
unset opt
shift `expr ${OPTIND} - 1`

[ -z "$*" ] && usage

export LC_ALL="C"

SRC_REMOTE="`svn info | sed -n '/URL:/s/^URL: //p'`"
TARGET_REMOTE="`echo $SRC_REMOTE | sed "s|trunk/KDE|branches/KDE/$BRANCH|"`"

echo "Backporting to $BRANCH"
TMPFILE=`mktemp svnbackport.XXXXXX` || exit 1

patchok=1

for file in "$@" ; do

  echo "Looking for last change to $file..."
  svnlastchange -ws "$file" > $TMPFILE || exit 1
  echo "Browsing last change to $file..."
  less "$TMPFILE"

  FILE_PATH="$file"
  FROM_URL="$SRC_REMOTE/$file"
  TO_URL="$TARGET_REMOTE/$file"

  echo "Switching to branch..."
  svn switch "$TO_URL" "$FILE_PATH" || exit 1
  if patch "$FILE_PATH" "$TMPFILE"; then
    rm -f "$TMPFILE"
    echo "Showing diff for $file..."
    svn diff "$FILE_PATH" | less
  else
    rm -f "$TMPFILE"
    patchok=0
    break
  fi
done

if [ $patchok -eq 1 ]; then
  echo "Do you want to commit all changes? (y/n) [y]"
  read confirm
  if [ -z "$confirm" ] || [ "`echo $confirm | cut -c 1 | tr Y y`" = "y" ] ; then
    svn ci "$@"
  else
    echo "Aborted!" >&2
  fi
else
  echo -n "Patch failed! Press enter to revert changes and switch back to trunk: "
  read confirm
  for file in "$@" ; do
    svn revert "$file"
  done
fi

echo "Switching back to trunk..."
for file in "$@" ; do
  svn switch "$SRC_REMOTE/$file" "$file"
done
