#!/bin/bash

# run & log functions
. /etc/rc.d/rc.runlog

queue(){
  atq | grep -Pom1 '^\d+'
}

JOB=$(queue)
if [[ -n $JOB ]]; then
  atrm $JOB 2>/dev/null
  log "remove queued job $JOB"
else
  log "no queued job present"
fi

# Validate delay parameter is numeric
if [[ -n $1 && ! $1 =~ ^[0-9]+$ ]]; then
  log "invalid delay parameter: $1, using default"
  DELAY=1
else
  DELAY=${1:-1}
fi

echo "sleep $DELAY; /usr/local/emhttp/webGui/scripts/reload_services" | at -M now 2>/dev/null
log "queue new job $(queue), wait for ${DELAY}s"
exit 0
