#!/bin/sh -e

# Daily maintenance script for mini-buildd
#
# Essentially, this call mbd-update-rep as user mini-buildd,
# which also eventually triggers mbd-update-bld on all builder
# hosts.

# If package is removed, but not purged, exit silently
[ -x "/usr/share/mini-buildd/mbd-update-rep" ] || exit 0

# Read configuration (we need it for the configured mail address only)
CONF="/home/mini-buildd/.mini-buildd.conf"
if [ -e "${CONF}" ]; then
	. ${CONF}
else
	echo "WARNING: ${0}: No file ${CONF}. mini-buildd not installed/configured?" >&2
	mbd_mail="root"
fi

# Run mbd-update-rep as mini-buildd; send a warning email when
# the script fails, or there is an error log "E: " in the
# output.
MBD_TMP_LOG=$(mktemp /tmp/mini-buildd-rep.cron.daily.XXXXXX)
trap "rm -f ${MBD_TMP_LOG}" EXIT

if ! su - mini-buildd -c "/usr/share/mini-buildd/mbd-update-rep" >${MBD_TMP_LOG} 2>&1 ||
	grep --quiet "\]: E:" ${MBD_TMP_LOG}; then
	(
		echo "=> ERRORS:"
		grep "\]: E:" ${MBD_TMP_LOG}
		echo
		echo "=> COMPLETE LOG:"
		cat ${MBD_TMP_LOG}
	) | mail -s "ERRORS in: ${0}" "${mbd_mail}"
fi
