#!/bin/bash

# wminfo plugin: displays time in different time zones

# command: wminfo -p timezone.wmi -u 60s -b#000000 -f#ffff00

#
# CONFIGURATION SECTION BEGINS HERE
#

DateFormat="MM-DD"	# MM-DD for "month-day"
			# DD.MM for "day.month"
			# DD MM for "day month"
			# etc.

#
# CONFIGURATION SECTION ENDS HERE
#

if [ -e timezone.cfg ]
then
    timezonefile="timezone.cfg"
elif [ -e "$HOME/bin/timezone.cfg" ]
then
    timezonefile="$HOME/bin/timezone.cfg"
elif [ -e /usr/local/bin/timezone.cfg ]
then
    timezonefile="/usr/local/bin/timezone.cfg"
else
    echo "timezone.wmi: there is no timezone.cfg database."
    exit
fi

dateseparator=${DateFormat: -3:1}
DateFormat=${DateFormat: -5:2}${DateFormat:3}

the_day_before_yesterday=`date --date="-2 days" +"%m-%d"`
yesterday=`date --date="yesterday" +"%m-%d"`
today=`date +"%m-%d"`
tomorrow=`date --date="tomorrow" +"%m-%d"`
the_day_after_tomorrow=`date --date="+2 days" +"%m-%d"`

function compactformat {
    if [ "$date" == "$the_day_before_yesterday" ]
    then
	time=$time=
    elif [ "$date" == "$yesterday" ]
    then
	time=$time-
    elif [ "$date" == "$today" ]
    then
	time="$time "
    elif [ "$date" == "$tomorrow" ]
    then
	time=$time+
    elif [ "$date" == "$the_day_after_tomorrow" ]
    then
	time=$time#
    fi
}

function dateformat {
    if [ "$DateFormat" == "DDMM" ]
    then
	date=${date:3}${dateseparator}${date: -5:2}
    fi
}

for tz in `cat $timezonefile | awk '!/^#/{print $1}'`
do
    if [ "`echo $tz | grep '^GMT'`" != "" ]
    then
	timestring=`TZ="$tz" date +"%H:%M %m-%d"`
	time=${timestring: -11:5}
	date=${timestring: -5:5}
	compactformat
	dateformat
	zone=`grep $tz $timezonefile | awk '{print $2}'`
	if [ "$zone" == "" ]
	then
	    zone=`echo $tz | sed 's/GMT//'`
	fi
    else
	timestring=`TZ="$tz" date +"%Z %H:%M %m-%d"`
	time=${timestring: -11:5}
	date=${timestring: -5:5}
	compactformat
	dateformat
	zone=`grep $tz $timezonefile | awk '{print $2}'`
	if [ "$zone" == "" ]
	then
	    zone=${timestring:0:3}
	fi
    fi
    echo "$zone $time $date"
done

echo
