#!/bin/bash

# this script fetches the yearly schedule from www.islamicfinder.org
# and convert it to the simpler table
#
#
# Instruction:
#   1. check the yearly schedule from islamicfinder:
#      - goto the site and find the schedule for your area
#      - click the "Monthly/Annual Schedule" link
#      - click in the "Printable Format" box the "Annual Schedule" button
#   2. copy the link location from the address bar
#   3. paste the link in CSLINK variable or put it as first parameter to this program
#      use quote '' to enclose the link address!
#
# Rosandi 2006
#

CSLINK='paste_the_link_here_to_islamic_finder_here'
YEAR=$(date +%Y)

# take parameters if given
[[ -z $1 ]] || CSLINK=$1
[[ -z $2 ]] || YEAR=$2

lynx -dump $CSLINK |
awk '
$1=="Date"{take=1;next}
/^$/{take=0;next}
/[|]/{take=0;next}
$1=="January"  {ofl="01.'$YEAR'";next}
$1=="February" {ofl="02.'$YEAR'";next}
$1=="March"    {ofl="03.'$YEAR'";next}
$1=="April"    {ofl="04.'$YEAR'";next}
$1=="May"      {ofl="05.'$YEAR'";next}
$1=="June"     {ofl="06.'$YEAR'";next}
$1=="July"     {ofl="07.'$YEAR'";next}
$1=="August"   {ofl="08.'$YEAR'";next}
$1=="September"{ofl="09.'$YEAR'";next}
$1=="October"  {ofl="10.'$YEAR'";next}
$1=="November" {ofl="11.'$YEAR'";next}
$1=="December" {ofl="12.'$YEAR'";next}
take==1 {
        split($0, tab)
        split(tab[2], subuh, /:/)
        split(tab[4], duhur, /:/)
        split(tab[5], ashar, /:/) 
        split(tab[6], magrib, /:/)
        split(tab[7], isya, /:/)
        print $1, 60*subuh[1]+subuh[2],
              d=(duhur[1]==12)?60*duhur[1]+duhur[2]:60*(duhur[1]+12)+duhur[2],
              a=60*(ashar[1]+12)+ashar[2],
              m=60*(magrib[1]+12)+magrib[2],
              i=60*(isya[1]+12)+isya[2] >> ofl
}'