#!/bin/bash

ICS_URL="$1"

if [ -z "$ICS_URL" ]; then
  echo "Usage: $0 <ICS_URL>"
  exit 1
fi

ICS_DATA=$(curl -s "$ICS_URL")

TODAY=$(date +%Y%m%d)

echo "_model: htmlpage"
echo "---"
echo "title: Willkommen beim PC Stammertal"
echo "---"
echo "html:"
echo ""
echo "<h3>Unser nächster Anlass: </h3><br/>"
#original statement:
#awk 'BEGIN{FS=":"}/^DTSTART/{dtstart=$2}/^SUMMARY/{summary=$2}/^END:VEVENT/{print substr(dtstart,7,2)"/"substr(dtstart,5,2)"/"substr(dtstart,1,4),sprintf("%02d",substr(dtstart,10,2)+3)":"substr(dtstart,12,2),summary}' file.txt
#from here: https://stackoverflow.com/questions/74111401/parse-ics-and-create-output

#OK what the heck are we doing here? TBH I even do not know exactly myself
# First we iterate over the ICS items looking for DTSTART, SUMMARY, LOCATION and END:VEVENT
# as soon as we found an END:VEVENT we consider that an event and start processing it
# if we havent found DTSTART, SUMMARY OR LOCATION for that event (actually rather just since last itartion step, that's why at the end we empty those variables)
# we insert "19700101T000000" for dtstart and "&nbsp;" for the other variables.
# As whole day events have a starting time of exactly 00:00 we consider this starting time equivalent to a whole day event.
# Yes, that' means we can't have all those midnight parties as those turn into whole day events.
# Once we have this all collected we print that into the markdown representation of a list item with a sublist for the subsequent fields
# But! Beware: we are for one not printing the newlines (except for one at the end of each event) and for the other our lines start with a YYYYMMDD representation of the date
# So we have for each event a line starting with YYYYMMDD followed by the markdown representation of a list item containing a sublist with all fields terminated with a newline
# Thats the first awk command
# Second awk command filters all lines which are in the past (yes, most probably this could have been done in the first awk call as well)
# Now we sort those remaining lines as they come in absolute random order (that's the reason for having an event squeezed on one line, and starting the line with YYYYMMDD representation of te event date)
# Now we no longer need the leading timestamp so we cut it off
# As we only want the next event (which is the one on top of our now sorted list) we just grab the first line
echo "$ICS_DATA" | awk 'BEGIN{FS=":"}
/^DTSTART/{dtstart=$2}
/^SUMMARY/{summary=$2}
/^LOCATION/{location=$2}
/^END:VEVENT/{
  gsub(/\r/, "", summary)
  gsub(/\r/, "", location)
  if (dtstart == "") dtstart = "19700101T000000"
  if (summary == "") summary = "&nbsp;"
  if (location == "") location = "&nbsp;"

  date_str = substr(dtstart, 1, 4) "-" substr(dtstart, 5, 2) "-" substr(dtstart, 7, 2)
  cmd = "LC_ALL=de_DE.UTF-8 date -d \"" date_str "\" +%A"
  cmd | getline weekday
  close(cmd)

  cmd = "LC_ALL=de_DE.UTF-8 date -d \"" date_str "\" +%B"
  cmd | getline month
  close(cmd)

  orderstartdate=sprintf("%s", substr(dtstart,1,8))
  realstartdate=sprintf("%d. %s", substr(dtstart,7,2), month)
  starttime=sprintf("%02d:%02d", substr(dtstart,10,2), substr(dtstart,12,2))
  summarystring=sprintf("%s", summary)
  locationstring=sprintf("%s", location)
  wholeline=sprintf("%s <div class=\"nextevent\">%s <strong>%s, %s %s Uhr</strong> in %s</div>", orderstartdate, weekday, realstartdate, summarystring, starttime, location)
  gsub(/ 00:00 Uhr/, "", wholeline)
  print wholeline
  dtstart=""
  summary=""
  location=""
}' | awk -v today="$TODAY" 'substr($1,1,8) >= today' | sort | cut -d " " -f 2- | head -n 1

echo "<div class=\"threecolumn\">"
echo "  <div>"
echo "    <a href=\"termine/\"><img src=\" /images/termine_square.jpg\" alt=\"Terminkalender\"> All unsere Termine</a>"
echo "  </div>"
echo "  <div>"
echo "    <a href=\"about/\"><img src=\"/images/about_square.jpg\" alt=\"Buch\"> Alle Infos über uns</a>"
echo "  </div>"
echo "  <div>"
echo "    <a href=\"kontakt/\"><img src=\"/images/kontakt_square.jpg\" alt=\"Briefe\"> Kontaktiere uns</a>"
echo "  </div>"
echo "</div>"
echo "---"
echo "_template: page.html"
echo ""
