#!/bin/sh -e
# Umegaya's debian-devel-changes pinger
#
# Piped emails from http://lists.debian.org/debian-devel-changes to this program
# trigger a ping for the corresponding package in an Umegaya instance.

URL=$(awk '/^ping_url:/ {print $2 ; nextfile}' /etc/umegaya/umegaya.conf)
PACKAGE=$(grep ^Subject -m1 | awk '{print $3}')
MESSAGES=$(awk '/^messages:/ {print $2 ; nextfile}' /etc/umegaya/umegaya.conf)

if [ "$PACKAGE" != '' ] ; then
  curl --silent ${URL-http://localhost/umegaya}/$PACKAGE/ping > /dev/null
  if [ "$MESSAGES" = 'syslog' ] ; then
    logger -i umegaya-ddc-ping: pinged $PACKAGE at $URL
  fi
  exit 0
fi
if [ "$MESSAGES" = 'syslog' ] ; then
  logger -i umegaya-ddc-ping: failed to find package name in standard input
else
  exit 1
fi
exit 0
