#!/usr/bin/awk -f

function isleap(year)
{
	return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)
}

function mdays(mon, year)
{
	return (mon == 2) ? (28 + isleap(year)) : (30 + (mon + (mon > 7)) % 2)
}

# Split the time in seconds since epoch into a table, with fields
# named as with gmtime(3): tm["year"], tm["mon"], tm["mday"],
# tm["hour"], tm["min"], tm["sec"]
function gmtime(sec, tm,
	s)
{
	tm["year"] = 1970
	while (sec >= (s = 86400 * (365 + isleap(tm["year"])))) {
		tm["year"]++
		sec -= s
	}

	tm["mon"] = 1
	while (sec >= (s = 86400 * mdays(tm["mon"], tm["year"]))) {
		tm["mon"]++
		sec -= s
	}

	tm["mday"] = 1
	while (sec >= (s = 86400)) {
		tm["mday"]++
		sec -= s
	}

	tm["hour"] = 0
	while (sec >= 3600) {
		tm["hour"]++
		sec -= 3600
	}

	tm["min"] = 0
	while (sec >= 60) {
		tm["min"]++
		sec -= 60
	}

	tm["sec"] = sec
}

function print_fold(prefix, s, n)
{
	while (s != "") {
		line = substr(s, 1, n)
		if (length(s) > n) sub(" +[^ \t\r\n]*$", "", line)
		print prefix line
		s = substr(s, length(line) + 2)
	}
}

BEGIN {
	print "BEGIN:VCALENDAR"
	print "VERSION:2.0"
	print "CALSCALE:GREGORIAN"
	print "METHOD:PUBLISH"
}

{
	split($0, a, "\t")
	gmtime(a[1] + offset, beg)
	gmtime(a[2] + offset, end)
	cat = a[3]; loc = a[4]; sum = a[5]; des = a[6]

	print ""
	print "BEGIN:VEVENT"
	printf "DTSTART:%04d%02d%02dT%02d%02d00Z\n",
	  beg["year"], beg["mon"], beg["mday"], beg["hour"], beg["min"]
	printf "DTEND:%04d%02d%02dT%02d%02d00Z\n",
	  end["year"], end["mon"], end["mday"], end["hour"], end["min"]
	print "SUMMARY:"	sum
	print "DESCRIPTION:"	des
	print "CATEGORIES:"	cat
	print "LOCATION:"	loc
	print "END:VEVENT"
}

END {
	print ""
	print "END:VCALENDAR"
}
