#!/bin/bash
set -e

SVN=${1:-/mit/xvm/svn}

mako-render rules.mako > rules

rm git -rf
mkdir git
cd git

sed -n 's/^create repository // p' ../rules | while read repo; do
    mkdir -p "$repo" && GIT_DIR="$repo" git init --bare
done

svn-all-fast-export --identity-map <(sed 's/= //' ../authors) ../rules "$SVN"

sed -n 's/^create repository // p' ../rules | while read repo; do
    ! [ -e "$repo/refs/heads/tags" ] || \
	cp -a "$repo/refs/heads/tags/." "$repo/refs/tags" && \
	rm -rf "$repo/refs/heads/tags"
done

pushd packages/invirt-remote.git
echo $(git rev-parse 0.2.0 invirt-remote-server invirt-remote-host) >> info/grafts
git filter-branch --tag-name-filter cat -- ^invirt-remote-server ^invirt-remote-host --all
rm -rf info/grafts refs/original refs/heads/invirt-remote-server refs/heads/invirt-remote-host
popd

pushd packages/invirt-console.git
echo $(git rev-parse 0.2.0 0.2.0^ invirt-console-host) >> info/grafts
git filter-branch --tag-name-filter cat -- ^0.2.0^ ^invirt-console-host --all
rm -rf info/grafts refs/original refs/heads/invirt-console-host
popd

pushd packages/invirt-dns.git
echo $(git rev-parse sipb-xen-dns/1~10 sipb-xen-dns/1~11 dns) >> info/grafts
git filter-branch --tag-name-filter cat -- ^sipb-xen-dns/1~11 ^dns --all
rm -rf info/grafts refs/original refs/heads/dns
popd

pushd packages/libyaml.git
git fetch git://andersk.mit.edu/libyaml.git refs/tags/upstream/0.1.1:refs/tags/upstream/0.1.1 refs/tags/debian/0.1.1-1:refs/tags/debian/0.1.1-1
git branch upstream upstream/0.1.1
echo $(git rev-parse 0.1.1-1_andersk1 debian/0.1.1-1^0) >> info/grafts
git filter-branch --tag-name-filter cat -- ^debian/0.1.1-1 --all
rm -rf info/grafts refs/original
popd

pushd packages/python-routefs.git
git fetch git://github.com/ebroder/python-routefs.git debian:refs/heads/debian
git tag -f 1.0.1-1 2fc1d90
git branch -f master 1.0.1-1
git branch -d debian
popd

echo
echo 'The Git migration was successful.'
