#!/bin/sh

# Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons
# GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)

set -e

#
# generate a graph of a remote darcs repository
#
# requires darcs-graph and wget in the $PATH

#
# reads darcs repo urls line-by-line on stdin, generating graphs, and
# playing them in a web server dir (currently hard coded below)
#

# create a fake patches dir
d=`mktemp -d`
cd $d

# read darcs repo urls on stdin
while read url ; do

    b=`basename $url`
    p=$b/_darcs/patches

    mkdir $b
    cd $b

    # get index.html for patches dir
    echo "Fetching " $url
    if wget -q "$url/_darcs/patches" ; then

        mkdir -p $p

        # assumes servers return patch dirs in the same format
        cd $p
        cat ../../../index.html | tr A-Z a-z |\
            sed '/href/!d; s/.*href="//; s/">.*$//;/\.gz$/!d' | xargs touch
        cd ../../..

        /home/dons/bin/darcs-graph -y 25 $b && \
            mv /tmp/$b-commits.png /home/dons/www/images/commits/community

    fi &

    cd ..

done

wait

rm -rf $d
