#!/bin/bash

# this only runs from cron so a very simple lock file in RAM is sufficient
mkdir -p /run/cvmfs
LOCKFILE=/run/cvmfs/generating_whitelists
if [ -f $LOCKFILE ]; then
    exit
fi
echo $$ >$LOCKFILE

OLDSOURCE=""
(
echo "addcmd add_osg_repository @url@/cvmfs/@fqrn@"
echo "remcmd remove_osg_repository -f @fqrn@"
echo "keysource cvmfs-contrib/config-repo/master/etc/cvmfs/keys"
print_osg_repos -u|sed 's,\([^/]*/[^/]*/[^/]*\)/cvmfs/\(.*\),\1 \2,'| \
        while read SOURCE REPO; do
    if [ "$SOURCE" != "$OLDSOURCE" ]; then
        echo source $SOURCE
        OLDSOURCE=$SOURCE
    fi
    echo repos $REPO
done
# Don't use manage-replicas -c to continue failed adds, because it is 
# normal for there to be a period of time between a new repository getting
# added to the stratum 0 and it being ready to replicate (after it is has
# copied in the new whitelist).  Meanwhile with a -c it will show up in
# monitoring as "initial snapshot not in progress".
) | manage-replicas -f /dev/stdin

rm -f $LOCKFILE
