#!/usr/bin/env nix-shell
#!nix-shell -i ysh -p coreutils-full -p gnused -p oils-for-unix

# processes a tab-separated "deadlines" file and alerts for any upcoming events.
#
# deadlines.tsv file format:
# - <date>\t<reminder-interval>\t<event>
# - no header
# - one line per entry
# - <event> may contain any non-newline and non-tab characters
# - <notice-interval> is the number of days before the event to start alerting, followed by 'd', e.g. `14d`
# - <date> should be lexicographically orderable and machine-parsable, e.g. `2023-03-14`
#
# example `deadlines.tsv`
# 2023-03-14	1d	celebrate pi day!
# 2023-04-18	14d	taxes due
# 2023-04-01	7d	the other pie day :o

# configurables:
var deadlines = "$[ENV.HOME]/knowledge/planner/deadlines.tsv"

if ! test -f "$deadlines" {
  echo "WARNING: $deadlines sane-deadlines file not found"
  exit 1
}

var now=$(date +%s)
for line in ($(cat "$deadlines") => split(u'\n')) {
  if (line === "" or line => startsWith("#")) {
    # ignore comments
    continue
  }

  # parse normal line
  var deadline_field=$(echo "$line" | cut -f 1)
  var threshold_field=$(echo "$line" | cut -f 2)
  var description_field=$(echo "$line" | cut -f 3)

  # normalize dates into seconds since unix epoch
  var deadline=$(date -d "$deadline_field" +%s)
  var threshold=$(echo "$threshold_field" | sed 's/d/day /g')
  var birthtime=$(date -d "$deadline_field - $threshold" +%s)

  # show the event iff it's near
  # 86400 = 24*60*60 = # of seconds in a day
  # 10080 = 24*60*7 = # of seconds between UTC and UTC-7
  # 11520 = 24*60*8 = # of seconds between UTC and UTC-8
  if (now >= birthtime) {
    var days_until = int((deadline - now + 86399) / 86400)
    echo "in $days_until day(s): $description_field"
  }
}
