#!/bin/bash

# wminfo plugin: clock and calendar for American users

# command: wminfo -p date-us.wmi -u 1 -b#000000 -f#ffff00

#
# CONFIGURATION SECTION BEGINS HERE
#

AM_PM="off"

Compact_Format="off"

#
# CONFIGURATION SECTION ENDS HERE
#

tempfile="$HOME/.wminfo/.date-us.tmp"

if [ "$AM_PM" == "on" ]
then
    date=$(date "+%I%M%S%p%m%d%Y")
    I=${date:0:2}
    M=${date:2:2}
    S=${date:4:2}
    p=${date:6:2}
    m=${date:8:2}
    d=${date:10:2}
    Y=${date:12:4}
    echo "$I:$M:$S$p"
else
    date=$(date "+%H%M%S%m%d%Y")
    H=${date:0:2}
    M=${date:2:2}
    S=${date:4:2}
    m=${date:6:2}
    d=${date:8:2}
    Y=${date:10:4}
    echo "$H:$M:$S "
fi

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

current_date="$m $d"
current_year="$Y"

if [ ! -e $tempfile ] || [ "`grep \"$current_year $current_date\" $tempfile`" == "" ] || [ $datefile -nt $tempfile ]
then
    echo "$current_year $current_date" > $tempfile
    if [ "$Compact_Format" == "off" ]
    then
	date +"%A%n%b %d" >> $tempfile
    else
	date +"%a %b %d" | sed -E 's/^(..)./\1/' >> $tempfile
    fi
    grep "^$current_year $current_date" $datefile | sed -E 's/[0-9]+ [0-9]+ [0-9]+ //' >> $tempfile
    grep "^$current_date" $datefile | sed -E 's/[0-9]+ [0-9]+ //' >> $tempfile
    filelength=`wc -l $tempfile | awk '{print $1}'`
    let "filelength=$filelength-1"
    sed -i 's/ /_/g' $tempfile
    tail -n $filelength $tempfile | \
    while read line junk
    do
	length=`echo $line | wc -m`
        if [ $length -lt 10 ]
        then
	    let "offset=10-$length"
	    for step in `seq 1 $offset`
	    do
		sed -i "s/^$line/$line /" $tempfile
	    done
        fi
    done
    sed -i 's/_/ /g' $tempfile
fi

tail -n +2 $tempfile

echo -e "         \n         "


