#!/bin/sh -e
#
# Copyright (C) 2008  Robert Millan
#
# 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 3 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, see <http://www.gnu.org/licenses/>.


get_curr_rev ()
{
  LC_MESSAGES=C svn info | sed -ne "s/^Revision: //p"
}

usage ()
{
  cat << EOF >&2
Usage:
  $0 start
  $0 good [revision]
  $0 bad [revision]
EOF
}

case $1 in
  start)
    rm -rf .svn-bisect
    mkdir .svn-bisect
  ;;
  bad|good)
    what=$1
    shift
    if [ "$1" = "" ] ; then
      get_curr_rev > .svn-bisect/$what
    else
      echo $1 > .svn-bisect/$what
    fi
  ;;
  -h|--help)
    usage
    exit 0
  ;;
  "")
    usage
    exit 1
  ;;
  *)
    echo "Unknown parameter \`$1'" >&2
    usage
    exit 1
  ;;
esac

if ! test -e .svn-bisect/good || ! test -e .svn-bisect/bad ; then
  exit 0
fi

good=`cat .svn-bisect/good`
bad=`cat .svn-bisect/bad`
target=$(((${good}+${bad})/2))

if [ "$target" = "$good" ] ; then
  echo "Regression found!"
  echo "Last good revision: $good"
  echo "First bad revision: $bad"
  exit 0
fi

svn up -r $target
