#!/bin/sh
#
# run-at-int - runon series
#
# ---
# source: https://github.com/BurntSushi/dotfiles

# require at least two arguments
if [ "$#" -lt 3 ]; then
  printf >&2 '%s: NUMBER[SUFFIX] UNTIL command ...' "$(basename "$0")"
  exit 2
fi

sleepTime="$1"
shift

until=0
[ "$1" != "0" ] && until=$(date -d "$1" +%s)
shift

while true; do
  d_time="$(date +%s)"
  if [ "${until}" != "0" ] && [ "${d_time}" -ge "${until}" ]; then
    exit
  fi
  "$@"
  sleep "${sleepTime}"
done
