#!/bin/sh

[ -d _darcs -a -d .git ] || exit 1

PREFIX=/usr/local
DARCS=$PREFIX/bin/darcs
XSLTPROC=$PREFIX/bin/xsltproc

TMPDIR=/tmp/darcs2git
[ -d $TMPDIR -a -w $TMPDIR ] || mkdir -p $TMPDIR

DARCS2GIT_XSLT=`dirname $0`/darcs2git.xslt
[ -r $DARCS2GIT_XSLT ] || exit 1

$DARCS fetch --all --summary --quiet > /dev/null &&
$DARCS pull --all --quiet --dry-run --xml-output > $TMPDIR/patches.xml
[ -s $TMPDIR/patches.xml ] || exit 2
if head -n 1 $TMPDIR/patches.xml | grep "No remote changes to pull in!"
then
	echo "No remote changes to pull in!" >&2
	exit 2
fi
$XSLTPROC --nonet --novalid --stringparam TMPDIR "$TMPDIR/" $DARCS2GIT_XSLT $TMPDIR/patches.xml > $TMPDIR/doit.sh && \
sh -x $TMPDIR/doit.sh

exit $?

# Für jeden patch (identifiziert durch seinen hash):
#  1. darcs fetch
#  2. darcs pull --dry-run --xml-output > patches.xml
#  3. xsltproc --nonet --novalid darcs2git.xslt patches.xml > doit.sh
#
#  2. darcs pull - -matches="hash $HASH" --post-hook="$DARCS_FILES und $DARCS_PATCHES_XML wegschreiben"
#  3. git add $DARCS_FILES
#  4. git commit -F "$name\n\n$comment" --author=$author --date=$date  $DARCS_FILES

##### Praeliminiarien: s. darcs2git.init.sh #####
#
### wikidata ###
# mkdir wikidata
# cd wikidata
### darcs ###
# darcs initialize
# echo "https://mesh-j-1.free.de/~ffdo/wiki/wikidata" > _darcs/prefs/defaultrepo
# patch _darcs/prefs/boring
9a10,12
> # wg. git
> \.git$
> 
^D
Hmm...  Looks like a normal diff to me...
Patching file _darcs/prefs/boring using Plan A...
Hunk #1 succeeded at 10.
done
### git ###
# git config --global user.name "FF-DO-Wiki `hostname`"
# git config --global user.email "$USER@`hostname`"
# git init
# echo "_darcs" > .gitignore
# git add .gitignore
# git commit -m "ignore _darcs" .gitignore