#!/bin/bash
########################################################################
# install-nightly-builds
#
# Quick script to install all the nightly build collectors.
#
# Note that the host credential for the machine upon which this script
# runs must be listed in root's .k5login for each of the collector
# machines.
#
# For the situation at the time of writing, this means:
#  host/gr6x3.fnal.gov@FNAL.GOV
# is in root's .k5login on gr8x{2,3,4}.
#
# 2010/03/24 Chris Green
########################################################################
prog=${0##*/}

####################################
# Required function do_installs
function do_installs() {
  [[ -n "$1" ]] && VERSION_OPT="$1"
  [[ -n "$2" ]] && VERSION_ARG="$2"
  for c in nightly_{batch,psacct,osg_daily}; do
    printf "########################################################################\n"
    "${INSTALL_SCRIPT}" -t -p -a \
                        "$VERSION_OPT" "$VERSION_ARG" \
                         -C "${CONFIG_DAT}" \
                         -m "$MAILTO" \
                         -c "${c}"
  done
}
####################################

####################################
# Optional help functions
function install-nightly-builds-help-description() {
    cat 1>&2 <<EOF
Install specified version (default latest nightly build) on nightly
build collectors.
EOF
}

function install-nightly-builds-help-options() {
    cat 1>&2 <<EOF
    [VERSION_OPT]
    -B (build in ~gratia/gratia-builds)
    -L (local build)
    -R (release in ~gratia/gratia-releases)

    [VERSION_ARG]
    Release (VERSION_OPT -B or -R) or local build location (-L)
EOF
}
####################################

####################################
# Install-related variables.
VERSION_OPT="-B"
VERSION_ARG=latest
SCRIPT_TOPDIR=~gratia/gratia-builds/gratia-${VERSION_ARG}
INSTALL_SCRIPT=${SCRIPT_TOPDIR}/build-scripts/install-release
CONFIG_DAT=${SCRIPT_TOPDIR}/common/configuration/collector-nbuild.dat
MAILTO=gratia-builds@fnal.gov

####################################
# Handle build retries.
(( max_retries = 5 ))
(( retry = 0 ))

if [[ ! -r "${SCRIPT_TOPDIR}" ]]; then
  echo "ERROR: specified script top directory \"${SCRIPT_TOPDIR}\"" 1>&2
  exit 1
fi

####################################
# Make sure the build is actually ready to be used.
while [[ ! -r "${SCRIPT_TOPDIR}/target/gratia.tar" ]] && (( retry < max_retries )); do
  printf "WARNING: build not complete -- waiting 1 minute (retry %d of %d)\n"  \
    $(( ++retry )) $(( max_retries ))
  sleep 60
done

####################################
# Failure.
if (( retry > max_retries )); then # failed
  echo "ERROR: build was not completed in time to install: please check" 1>&2
  exit 1
fi

########################################################################
# Main program.
####################################

install_script=$(type -p install-group 2>/dev/null)
install_script=${install_script:-~gratia/gratia-builds/gratia-latest/build-scripts/install-group}
.  "${install_script}" "$@"
exit $?
