#!/bin/bash -e
usage="rr-cntl cmd

Enable or disable updates on rr and associated servers.  This is done
by adding or removing the approriate crontabs.

cmd is start or stop
"

rrhosts="hgw1 hgw2 hgw3 hgw4 hgw5 hgw6 hgw7 hgw8 mgc"

if [ $# != 1 ] ; then
    echo "wrong # args: $usage" >&2
    exit 1
fi
cmd=$1

case "$cmd" in
    start);;
    stop);;
    *)
    echo "invalid argument: \"$cmd\", expected \"start\" or \"stop\"" >&2
    exit 1 ;;
esac

sshCronTab() {
    local request=$1
    shift
    local host=$1
    shift
    local ctargs=$*
    echo $request $host
    ssh -x $host crontab $ctargs < /dev/null
}

case "$cmd" in
    start)
        sshCronTab start hgnfs1 /genbank/etc/hgnfs1.crontab
        sshCronTab start hgdownload /genbank/etc/hgdownload.crontab

        for h in $rrhosts ; do
            sshCronTab start $h /genbank/etc/rr.crontab
        done
    ;;

    stop)
        sshCronTab stop hgnfs1 -r
        sshCronTab stop hgdownload -r

        for h in $rrhosts ; do
            sshCronTab stop $h -r
        done
    ;;
esac
