#!/bin/bash
#
# This source file is Copyright (c) 2019, FERMI NATIONAL ACCELERATOR
#    LABORATORY.  All rights reserved.
# For details of the Fermitools (BSD) license see COPYING.

if [ ! -f /etc/cvmfs-user-pub.conf ]; then
    exit
fi

GCSTARTHOUR="`sed -n 's/^gcstarthour\s*//p' /etc/cvmfs-user-pub.conf`"
if [ -z "$GCSTARTHOUR" ]; then
    GCSTARTHOUR=3
fi
MYHOSTLONG="`hostname`"
MYHOSTSHORT="`hostname -s`"
REPONUM=0
THISHOUR="`date +%H`"
# remove leading zero if there is one, otherwise bash treats as octal
#  and complains about hour 08 and 09
THISHOUR="${THISHOUR/#0/}"
for HOSTREPO in `sed -n 's/^hostrepo\s*//p' /etc/cvmfs-user-pub.conf`; do
    let REPONUM+=1
    if (( $THISHOUR != ($GCSTARTHOUR - 1 + $REPONUM) % 24 )); then
        continue
    fi
    HOST="`echo $HOSTREPO|sed 's/:.*//'`"
    REPO="`echo $HOSTREPO|sed 's/.*://'`"
    if [ "$HOST" = "$MYHOSTLONG" ] || [ "$HOST" = "$MYHOSTSHORT" ]; then
        continue
    fi
    echo "Starting gc of $REPO at `date`"
    cvmfs_server gc -f $REPO
    echo "Ended gc of $REPO at `date`"
done
