#!/bin/bash
#
#  x509watch 0.6.1
#
#  (c) 2009-2017 by Robert Scheck <x509watch@robert-scheck.de>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

if [ -f /etc/sysconfig/x509watch ]; then
	. /etc/sysconfig/x509watch
fi

SENDMAIL="${SENDMAIL:-/usr/sbin/sendmail}"
DNSNAME="${DNSNAME:-/bin/hostname}"
OUTPUT="$(/usr/bin/x509watch $OPTIONS)"
RECIPIENT="${RECIPIENT:-root}"

if [ -n "${OUTPUT}" ]; then
	if [ -x "$SENDMAIL" -a -x "$DNSNAME" ]; then
		echo -e "From: x509watch\nTo:$RECIPIENT\nSubject: x509watch for $($DNSNAME)\n\n${OUTPUT}" | $SENDMAIL -t
	else
		echo "${OUTPUT}"
	fi
fi
