#!/bin/bash
#
# test_certexpire
#
# Ablaufdatum von Zertifikatsdateien an ULS
#
##################################################
#
unset SETULSHOSTNAME DT TMP_PATH S
while getopts d:D:h:S? op
 do
  case "$op" in
   d) TMP_PATH="-d $OPTARG";;
   D) DT="$OPTARG";;
   h) SETULSHOSTNAME="$OPTARG";;
   S) S="-S";;
   ?) echo "usage: $(basename $0) [-d storepath] [-S] [-D <datetime>] [-h <hostname>] <section> <teststep> <certpath> [...]"
      exit 0;;
  esac
done
shift $(( $OPTIND - 1 ))
#
if [[ $# -lt 3 ]]
 then
  echo "usage: $(basename $0) [-d storepath] [-S] [-D <datetime>] [-h <hostname>] <section> <teststep> <certpath> [...]"
  exit 1
fi
#
if [[ -f /etc/uls/uls.conf ]]
 then
  . /etc/uls/uls.conf
fi
if [[ -z "$DOMAINHOSTNAME" ]]
 then
  DOMAINHOSTNAME="DOMAIN:`hostname -s`"
fi
if [[ -z "$ULSHOSTNAME" ]]
 then
  ULSHOSTNAME=`hostname -s`
fi
#
if [[ -z "$DT" ]]
 then
  DT=`date '+%F %T%:z'`
fi
#
( echo "D;$DT;${SETULSHOSTNAME:-$ULSHOSTNAME};$1;$2;"
  shift 2
  while [[ -n "$1" ]]
   do
    EXP=$(date -d "`openssl x509 -enddate -noout -in "$1" | sed -n 's/notAfter=//p'`" '+%F %T')
    if [[ -n "$EXP" ]]
     then
      FN=$(basename "$1")
      echo "V;;;;;$FN;$EXP;{DT};"
    fi
    shift
  done
) | send_test_tab $TMP_PATH $S
