#!/usr/bin/bash
#
# test_certexpires
#
# Ablaufdatum von SSL-Zertifikaten prüfen
# ruft test_certexpire auf
#
#################################
WDIR="`dirname $0`"
#
CONF=/etc/uls/certexpires.conf
#
unset S DT TMP_PATH days
#
while getopts c:d:DS? op; do
  case "$op" in
    c) CONF="$OPTARG";;
    d) TMP_PATH="-d $OPTARG";;
    D) days='-D';;
    S) S="-S";;
    *) echo "usage: $0 [-d storepath] [-D] [-c confpath] [-S] [<date> <time>]"
       exit 1;;
  esac
done
shift $(( $OPTIND - 1 ))
#
#
if [[ $# -lt 2 ]]; then
  DT="`date '+%F %T%:z'`"
else
  DT="$1 $2"
fi
#
if [[ -f "$CONF" ]]; then
  grep -E '^ *[^#]* |^ *[^#]*$' "$CONF" | while read file sec tst host dummy; do
    if [[ -n "$host" ]]; then
      hst="-h $host"
    else
      unset hst
    fi
    if [[ -f "$file" ]]; then
      $WDIR/test_certexpire $days -T "$DT" $TMP_PATH $S $hst "${sec:-Certificates}" "${tst:-Valid}" "$file"
    else
      if [[ -d $file ]]; then
        folder="$file/"
      else
        unset folder
      fi
      for f in $(ls $file); do
        if [[ -f "$folder$f" && $f != *.conf ]]; then
          $WDIR/test_certexpire $days -T "$DT" $TMP_PATH $S $hst "${sec:-Certificates}" "${tst:-Valid}" "$folder$f"
        fi
      done
    fi
  done
fi
