#!/bin/sh
#
# cldiary.sh
#
# Command Line Diary
# A script for printing diary entries to stdout using gcal
#
# Copyright (C) 2007 Rob Newcater <rob@basicallytech.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# resource files
RF_REGULAR=${HOME}/.gcal/regular
RF_DIARY=${HOME}/.gcal/diary

# gcal highlight settings
GHL='[1;33m:[0m:[0m:[0m'

# gcal date format
GDF='%1%<3#K %>02*D/%>02*M/%>04*Y%2'

# today in my gcal output format
GTODAY=`date '+%a %d/%m/%Y'`

# tomorrow in my gcal output format
GTOMORROW=`date --date='next day' '+%a %d/%m/%Y'`

ord_suff()
{
   # the ordinal suffix for the date of the month
   case `date '+%-d'` in
      1|21|31) echo -n "st" ; exit 0 ;;
      2|22) echo -n "nd" ; exit 0 ;;
      3|23) echo -n "rd" ; exit 0 ;;
      *) echo -n "th" ; exit 0 ;;
   esac

}

show_date()
{
   # print the date in a nice blue colour
   ORD_SUFF=`ord_suff`
   date "+%n[1;36m%A, %-d${ORD_SUFF} %B %Y[0m"
}

exit_status()
{
   # capture the exit status of another part of a function?
   # http://www.networkcomputing.com/unixworld/answers/003.html
   "$@"
   #echo "Exit status of command ("$@") was " $? >&2
   GCAL_EXIT="$?"

   exit_message $GCAL_EXIT

}


show_diary()
{
   # show diary entries, but colour "today" bold green
   # and "tomorrow" bold yellow,
   # all other dates as bold, and straighten out the lines
   exit_status gcal -cdeoxl@t${1} -f ${RF_REGULAR}+${RF_DIARY} -qGB_EN -u --date-format="$GDF" -g | sed -e "s@^<${GTODAY}>:@    [1;32mToday[0m       @" -e "s@^ ${GTOMORROW} :@   [1;33mTomorrow[0m     @" -e '/^ [A-Z]/s/^ /[1m/' -e 's/ :/[0m  /' -e '/^:/s/::/[0m  /' -e 's/^:/[1m/' -e 's/^                  /                 /'

}

show_diary_year()
{
   # show diary entries, but colour "today" bold green,
   # and "tomorrow" bold yellow,
   # all other dates as bold, and straighten out the lines
   exit_status gcal -cdeoxy -f ${RF_REGULAR}+${RF_DIARY} -qGB_EN -u --date-format="$GDF" -g | sed -e "s@^<${GTODAY}>:@    [1;32mToday[0m       @" -e "s@^ ${GTOMORROW} :@   [1;33mTomorrow[0m     @" -e '/^ [A-Z]/s/^ /[1m/' -e 's/ :/[0m  /' -e '/^:/s/::/[0m  /' -e 's/^:/[1m/' -e 's/^                  /                 /'
}

show_diary_year_other()
{
   # show diary entries, but colour "today" bold green,
   # and "tomorrow" bold yellow,
   # all dates as bold, and straighten out the lines
   exit_status gcal -cdeoxy -f ${RF_REGULAR}+${RF_DIARY} -qGB_EN -u --date-format="$GDF" -g %${1} | sed -e "s@^<${GTODAY}>:@    [1;32mToday[0m       @" -e "s@^ ${GTOMORROW} :@   [1;33mTomorrow[0m     @" -e '/^ [A-Z]/s/^ /[1m/' -e 's/ :/[0m  /' -e '/^:/s/::/[0m  /' -e 's/^:/[1m/' -e 's/^                  /                 /'
}

exit_message()
{
   # print something even if there are no diary entries, just to reassure
   #case "$?" in
   case "$1" in

      0) # normal program termination
      exit 0
      ;;

      1) # no output recorded
#      echo -e "\nThere are no entries in your diary for the requested period"
      echo -e "\nThere are no entries in your diary $TIME_PERIOD"
      exit 0
      ;;

      *) # oops
      echo -e "\nAn error occurred"
      exit 2
      ;;

   esac
}

show_usage()
{
   echo ""
   echo "$0 <parameter>"
   echo ""
   echo " parameter              action"
   echo " ---------              ------"
   echo " -d|--days <num>        print out all diary entries for the next <num> days"
   echo "                        (no more than 99 days)"
   echo ""
   echo " -y|--year [yyyy]       print out all diary entries for the current year,"
   echo "                        or, optionally, for year yyyy"
   echo ""
   echo " -h|--help              print this message"
   echo ""
   exit 1
}

case "$1" in

   -d|--days)
   # if there is no $2 parameter, exit gracefully
   if [ -z "$2" ]
   then
      echo -e "\nPlease supply the number of days you wish to query your diary for"
      show_usage
   elif [ $2 -gt 83 ]
   then
      echo -e "\ngcal does not accept values greater than 83 days for this query"
      show_usage
   fi
   if [ "$2" -eq 1 ]
   then
      TIME_PERIOD="for the next day"
   else
      TIME_PERIOD="for the next $2 days"
   fi
   show_date
   show_diary $2
   # leave a new line
   echo ""
   ;;

   -y|--year)
   # if there is no $2 parameter, print entries for the current year
   if [ -z "$2" ]
   then
      TIME_PERIOD="this year"
      show_date
      show_diary_year
      exit 0
   fi
   TIME_PERIOD="for the year $2"
   show_date
   show_diary_year_other $2
   ;;

   -h|--help|*)
   show_usage
   ;;

esac

