#!/bin/sh

# Calculate and display the date in the Discordian calendar. Something
# similar used to be part of util-linux, but isn't anymore. Oh well. That's
# what shell scripts are for.

gyear=$(date +%Y)
dayofyear=$(date +%j)

# strip leading zeroes. "+%-j" doesn't work on netbsd and leaving them in
# makes stuff be interpreted as octal.

gyear=$(echo "$gyear" | sed 's/^0*//')
dayofyear=$(echo "$dayofyear" | sed 's/^0*//')


yold=$((gyear+1166))

# leapyear handling

if [ $((gyear%4)) -eq 0 ] && [ $((gyear%100)) -ne 0 ] || [ $((gyear%400)) -eq 0 ]
then
	if [ "$dayofyear" -eq 60 ] #is it St Tib's Day?
	then
		echo "St. Tib's Day, $yold YOLD"
		exit 0
	fi

	if [ "$dayofyear" -gt 60 ] # Is it *after* St Tib's Day?
	then
		dayofyear=$((dayofyear-1)) # Remove intercallary day for later calculations
	fi
fi

season=$((dayofyear/73+1))
dayofseason=$((dayofyear%73))
if [ "$dayofseason" -eq 0 ]
then
	dayofseason=73
fi

seasonstr="IMPOSSIBLE SEASON"
if [ "$season" -eq 1 ]
then
	seasonstr="Chaos"
fi
if [ "$season" -eq 2 ]
then
	seasonstr="Discord"
fi
if [ "$season" -eq 3 ]
then
	seasonstr="Confusion"
fi
if [ "$season" -eq 4 ]
then
	seasonstr="Bureaucracy"
fi
if [ "$season" -eq 5 ]
then
	seasonstr="Aftermath"
fi

echo "$seasonstr $dayofseason, $yold YOLD"