#!/bin/bash

trap 'kill %1;' SIGTERM

log() {
	echo "$(date +"%F %R"): $@"
}

run_certwatch() {
	log "Executing checks at $(date +"%F %R")..."
	/usr/local/bin/certwatch.sh
	case "$?" in
	  0) log "PASS: All checks passed" ;;
	  2) log "FAIL: One or more checks failed" ;;
	  *) log "ERROR: There was an error executing the checks" ;;
	esac
}

log "Hello and welcome to certwatch!"

(
	run_certwatch;

	while sleep 86400; do
  		run_certwatch;
	done;
) &

wait %1

log "Exiting..."

exit 0;
