#!/bin/bash

set -e

###############################################################################
#
# Add the `gnats' system into cron
#

CRON_ALLOW='/etc/cron.allow'
AT_ALLOW='/etc/at.allow'

if [ "$1" = configure ]; then

  #
  # Make sure gnats has cron & at access
  #

  if [ -f $CRON_ALLOW ] && \
     [ $(protect fgrep -c gnats $CRON_ALLOW) -eq 0 ]; then
    echo 'gnats' >>$CRON_ALLOW
#    echo "- \`gnats' has been added to the cron.allow file"
  fi

  if [ -f $AT_ALLOW ] && \
     [ $(protect fgrep -c gnats $AT_ALLOW) -eq 0 ]; then
    echo 'gnats' >>$AT_ALLOW
#    echo "- \`gnats' has been added to the at.allow file"
  fi

  #
  # Add the `gnats' crontab file
  #

  crontab -u gnats - <<EOF
5,15,25,35,45,55 * * * * /usr/lib/gnats/queue-pr --run
10 3 * * * /usr/lib/gnats/check-db
EOF

fi
