#!/bin/sh

if [ -z "$2" ]; then
  echo "Usage : $0 \$BRANCH \$TAG_TO_BACKPORT [\$TAG_BACKPORTED_LAST]"
  exit 1
fi

HEAD=`git show | grep "^commit " | head -1 | sed "s/commit //"`
REPO=`~/bin/repo-version.sh $1`

git checkout $2
git checkout -b backport/$REPO

[ -z "$3" ] || git merge -s ours $3

for i in `ls *.spec`; do
  srpmbackport --spec $i -$1 > $i.$1
  mv -f $i.$1 $i
  add_changelog -e "- backport to branch/$REPO" $i
done

gear-commit --no-edit -a
gear-create-tag

git checkout $HEAD
git branch -D backport/$REPO