#!/bin/bash
########################################################################
# install-group
#
# Quick script to install a group of collectors. Note this should be
# *sourced*, not executed, by a script which provides a function
# do_installs and an optional default value for MAILTO.
#
# 2010/03/31 Chris Green
########################################################################
prog=${prog:-${0##*/}}

# Default in case it's not set by calling script.
MAILTO=${MAILTO:-gratia-builds@fnal.gov}

########################################################################
# Functions
########################################################################

####################################
# usage
function usage() {
    cat 1>&2 <<EOF
usage: $prog --help|-h|\?
       $prog [<install-group-options>] [-- <do_installs-options>]

EOF
    if type ${prog}-help-description >/dev/null 2>&1; then
${prog}-help-description
printf "\n" 1>&2
fi
    cat 1>&2 <<EOF
$prog uses the install-group script machinery to facilitate installation
of multiple similar collectors as one set. If functions
${prog}-help-description and/or ${prog}-help-options are found, its
output will be included herein.

$prog will source the install-group script, which requires the
do_installs function to be defined.

install-group options:

   -h
   -\?
  --help
    This help.

   -m <mailto>
  --mail <mailto>
    Change destination for mailed reports.

   -d
  --debug
    Output is copied to screen in addition to being mailed.

   -v
  --verbose

    Under normal circumstances, the report for each install will be sent
    under separate cover; any standard output from this script is
    suppressed and only standard error will be sent to the specified
    recipient. If --verbose is specified, then standard output from this
    script will not be suppressed.

Any remaining non-option arguments or any arguments following the first
bare "--" will be passed direct to the do_installs() callout function. 

EOF
if type ${prog}-help-options >/dev/null 2>&1; then
  printf "$prog-specific options:\n\n" 1>&2
  ${prog}-help-options
fi
}
####################################

####################################
# cleanup
#
# Clean up and exit, making sure we preserve and send output.
####################################
function cleanup() {
  [[ -n "$TMP" ]] || return # Nothing to do
  # Restore output and close file
  if [[ -f "$TMP.log" ]]; then
    # Make sure we have some output otherwise the mail might get bounced
    # from a list.
    exec 4>&2 1>&3-
    # If we have non-zero output, send it.
    if [[ -s "$TMP.log" ]]; then
      subject="REPORT from ${prog} on `hostname -s`"
      mutt -F "$TMP.muttrc" -s "$subject" "$MAILTO" <"$TMP.log" >/dev/null 2>&1
    fi
    (( ${debug:-0} )) && cat "$TMP.log"
  fi
  # Clean up.
  rm "$TMP*" 2>/dev/null
}
####################################

########################################################################
# Main program.
####################################
TMP=`mktemp ${TMPDIR:-/tmp}/${prog}.XXXXXXXXXX`
trap "cleanup" EXIT

# Send mail with output from this script.
cat > $TMP.muttrc <<EOF
set realname = "Gratia builds"
set from = "gratia-builds@fnal.gov"
EOF

####################################
# Parse options, including long options.
TEMP=`getopt -o :dh\?m:v -n "$prog" --long debug --long verbose --long mail: --long help -- "$@"`

if [ $? != 0 ]; then
  echo "terminating due to option parse errors" 1>&2
  return 1
fi

eval set -- "$TEMP"

while true; do
    case $1 in
      -d|--debug)
        debug=1
        shift
        ;;
      --help|-h|-\?)
        usage
        return 1
        ;;
      --mail|-m)
        MAILTO=$2
        shift 2
        ;;
      -v|--verbose)
        verbose=1
        shift
        ;;
      --)
        shift
        break
        ;;
      *)
    esac
done
####################################

# Redirect output to file and trap exit.
exec 3>&1 4>&2 >"$TMP.log" 2>&1
trap "cleanup" EXIT # Need to re-do trap after exec.

# If we're running this script as root, get the host credentials.
if (( $(id -u) == 0 )); then
  # Save existing KRB5CCNAME just in case the host credentials won't get us where we need to be.
  export KRB5CCNAME_OLD=$KRB5CCNAME
  export KRB5CCNAME=$TMP.krb5
  /usr/krb5/bin/kinit -k
fi

# Install all required collectors.
(( ${verbose:-0} )) || redirect=">/dev/null"

if type do_installs >/dev/null 2>&1; then
  eval do_installs "$@" "$redirect"
else
  echo "ERROR: install-group should be sourced by a script that has already defined the \"do_installs\" function!" 1>&2
fi
