src/docker/scripts/epss.sh
#!/bin/sh

set -e

echo "Mirroring EPSS..."

LOCKFILE=/tmp/epss.lock

if [ -f $LOCKFILE ]; then
  echo "Lockfile found - another mirror-sync process already running"
else
  touch $LOCKFILE
fi

function remove_lockfile() {
  rm -f $LOCKFILE
  exit 0
}
trap remove_lockfile SIGHUP SIGINT SIGQUIT SIGABRT SIGALRM SIGTERM SIGTSTP

CACERT_ARG=""
if [ -n "${CACERT}" ]; then
  echo "Using cacert file: $CACERT"
  if [ -f "$CACERT" ]; then
    echo "File $CACERT exists."
    CACERT_ARG="--cacert $CACERT"
  else
    echo "File $CACERT not found."
  fi
fi

curl -L -sS $CACERT_ARG -o /usr/local/apache2/htdocs/epss_scores-current.csv.gz https://epss.cyentia.com/epss_scores-current.csv.gz

if [ -f /usr/local/apache2/htdocs/epss_scores-current.csv.gz ]; then
  timestamp=$(stat -c "%Y" "/usr/local/apache2/htdocs/epss_scores-current.csv.gz" | xargs -I{} date -d @{} "+%Y-%m-%d %H:%M:%S")
  echo "<li><a href=\"epss_scores-current.csv.gz\">epss_scores-current.csv.gz</a>	(Last Modified: $timestamp)</li>" > /usr/local/apache2/htdocs/epss.shtml
fi

rm -f $LOCKFILE