#!/usr/bin/env bash
#
# pop-up calendar for dzen
#
# (c) 2007, by Robert Manea
# (c) 2009, slight improvments by Jerome Flesch
#

TODAY=$(expr `date +'%d'` + 0)
MONTH=`date +'%m'`
YEAR=`date +'%Y'`

while true
do
    echo -n "^tw()"
    date +'%A, %d.%m.%Y %H:%M:%S'

    s_month=`expr ${MONTH} + 0`

    for i in `seq -1 1`
    do
	c_month=$(( ${s_month} + ${i} ))
	c_year=${YEAR}

	if [ ${c_month} -lt 0 ]
	then
	    c_month=$((12 - ${c_month}))
	    c_year=$((${c_year} - 1))
	fi
	if [ ${c_month} -gt 12 ]
	then
	    c_month=$((${c_month} % 12))
	    c_year=$((${c_year} + 1))
	fi

	if [ ${i} -eq 0 ]
	then
	    # highlight
	    cal | sed -r -e "1,2 s/.*/^fg(white)&^fg()/" \
             -e "s/(^| )($TODAY)($| )/\1^bg(white)^fg(#111)\2^fg()^bg()\3/"
	else
	    cal $c_month $c_year \
		| sed -e "1,2 s/.*/^fg(white)&^fg()/"
	fi
    done
    sleep 1
done