#!/bin/bash

#
# (c) 2013 Heinlein Support GmbH
#          Robert Sander <r.sander@heinlein-support.de>
#

# This 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 in version 2.  This file is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# ails.  You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

OPENSSL=`which openssl`

if [ ! -x "$OPENSSL" ]; then
  exit
fi

CERT_DIRS="/etc/ssl/certs"

CONFIG_FILE="$MK_CONFDIR/sslcertificates"

if [ -r $CONFIG_FILE ]; then

  . $CONFIG_FILE

fi

echo '<<<sslcertificates>>>'

for dir in $CERT_DIRS; do
	for certfile in $(grep -l -- '-----BEGIN CERTIFICATE-----' $dir/* 2> /dev/null); do

		cert_enddate="$($OPENSSL x509 -noout -enddate -in $certfile | cut -d "=" -f 2 )"
		cert_enddate_epoch=$(date --date "$cert_enddate" '+%s')
		cert_subject="$($OPENSSL x509 -noout -subject -in $certfile | cut -d "=" -f 2-)"

		echo $certfile $cert_enddate_epoch $cert_subject
	done

done
