#!/bin/sh
# Version: 1.3
# Description: Update DNS blocklist and ensure script runs in the background

LOCKFILE="/var/run/dnsbl-ipset.lock"
LOGFILE="/var/log/dnsbl-ipset.log"

# Ensure the script runs in the background
if [ -z "$BACKGROUND" ]; then
  BACKGROUND=true nohup "$0" "$@" >> $LOGFILE 2>&1 &
  exit 0
fi

{
  echo "[$(date)] Starting dnsbl-update script"

  if [ ! -f $LOCKFILE ]; then
    echo "[$(date)] Lock file not found, running dnsbl-ipset.sh in background"
    nohup /etc/firehol/dnsbl-ipset.sh >> $LOGFILE 2>&1 &
    echo "[$(date)] dnsbl-ipset.sh started in background with PID $!"
  else
    echo "[$(date)] Lock file found, exiting"
  fi

  echo "[$(date)] Finished dnsbl-update script"
} >> $LOGFILE 2>&1