#!/bin/sh
TONGUE=$LANG
LANG=C
dspec="%Y/%m"
case $0 in
*yamaha_calendar) cat <<EOF
ʿǯ	(180)
2003	5	1	
2003	5	2	
2003	5	5	
2003	7	21	
2003	8	11	
2003	8	12	
2003	8	13	
2003	8	14	
2003	8	15	
2003	9	15	Ϸ
ʿǯ	(180)
2003	10	13	ΰ
2003	11	3	ʸ
2003	12	29	
2003	12	30	
2003	12	31	糢
2004	1	1	ö
2004	1	2	
2004	1	5	
2004	1	12	
2004	2	11	ǰ
ʿǯ	(181)
2004	5	3	(ˡǰ)
2004	5	4	(̱ε)
2004	5	5	(ɤ)
2004	7	19	()
2004	8	9	
2004	8	10	
2004	8	11	
2004	8	12	
2004	8	13	
2004	9	20	(Ϸ)
ʿǯ	(181)
2004	10	11	(ΰ)
2004	11	3	(ʸ)
2004	12	29	
2004	12	30	
2004	12	31	
2005	1	3	
2005	1	4	
2005	1	5	
2005	1	8	ж
2005	2	11	(ǰ)
2005	3	21	(ʬ)
EOF
exit 0 ;;
esac
case $1 in
1|2|3)	set $(date +$dspec -d"$1/1 next year") ;;
[0-9]*)	set $(date +$dspec -d"$1/1 this year") ;;
'')	set $(date +$dspec -d"this month") $(date +$dspec -d"next month") ;;
*)	echo Usage: $0 '[3 ... 12 1 2 3]' ;exit 1 ;;
esac
for ym
do
	Y=$(date +%Y -d $ym/1)
	m=$(date +%m -d $ym/1)
	LANG=$TONGUE cal $m $Y |awk 'BEGIN{
		Y='$Y'
		m='$m'
		list='${list:-0}'
		while (("yamaha_calendar" |getline) > 0)
		{
			if (NF >= 3)
			{
				x[$1,$2,$3]=$4
			}
		}
	}
	{
		if (NR <= 2 && ! list)
		{
			print
		}
		else
		{
			prefix = ""
			offset = 0
			if (NR == 3)
			{
				offset = 7 - NF
				for (i = 0 ;i < offset ;i++)
				{
					prefix = prefix"   "
				}
			}
			if (! list)
			{
				printf prefix
			}
			for (i = 1 ;i <= NF ;i++)
			{
				holiday = (i+offset==1) || (i+offset==7)
				if (holiday)
				{
					holiday = x[Y,m,$i] ? 0 : 1
					if (list && x[Y,m,$i])
					{
						printf"%02d/%02d/%02d %s\n",Y,m,$i,x[Y,m,$i]
					}
				}
				else
				{
					holiday = x[Y,m,$i] ? 1 : 0
					if (list && x[Y,m,$i])
					{
						printf"%02d/%02d/%02d %s\n",Y,m,$i,x[Y,m,$i]
					}
				}
				if (list)
				{
					continue
				}
				if (holiday)
				{
					printf "## "
				}
				else
				{
					printf "%2d ",$i
				}
			}
			if (! list)
			{
				printf "\n"
			}
		}
	}'
done
exit 0
