#!/bin/bash
# update all oasis VOs that have changed

LOCKPFX=/stage/locks/
case "`hostname -s`" in
    *-itb) LOCKPFX=${LOCKPFX}itb_;;
    *)  echo "Run this only on oasis-itb" >&2
        exit 2;;
esac

for URL in `print_osg_repos -u`; do
    REPO="`basename $URL`"
    if [ ! -d /srv/cvmfs/$REPO ]; then
	echo "Adding repository $REPO at `date`"
	add_osg_repository $URL
    else
        # check to see if fingerprint has changed
        NEWFPRINT="`wget -qO- --timeout=10 --tries=2 $URL/.cvmfswhitelist|cat -v|grep ^-- -B 1|head -1`"
        OLDFPRINT="`cat -v /srv/cvmfs/$REPO/.cvmfswhitelist|grep ^-- -B 1|head -1`"
        if [ -n "$NEWFPRINT" ] && [ -n "$OLDFPRINT" ] && [ "$NEWFPRINT" != "$OLDFPRINT" ]; then
            echo "$REPO fingerprint changed from"
            echo "  $OLDFPRINT" 
            echo "to"
            echo "  $NEWFPRINT"
            echo "Re-adding repository $REPO at `date`"
            add_osg_repository $URL
        fi
    fi
done

for VO in `/usr/share/oasis/print_oasis_vonames`; do
    if [ ! -f /stage/oasis/$VO/update.details ]; then
	# skip because it hasn't been published yet
	continue
    fi
    if cmp /stage/oasis/$VO/update.details /cvmfs/oasis.opensciencegrid.org/$VO/update.details >/dev/null 2>&1; then
	# already up to date, nothing to do
	continue
    fi
    if [ -f ${LOCKPFX}oasis_update_requested_vo ]; then
	echo "Skipping update for $VO because `cat ${LOCKPFX}oasis_update_requested_vo` update in progress"
	exit 1
    fi
    echo "Requesting update for $VO at `date`"
    # this waits for the request to complete
    /usr/share/oasis/request_oasis_update $VO
done

