#!/bin/sh
#% 
#%        * REMINDER * by Alex Kasman 1993
## (use accompanying program ADDREMINDER to generate the rc file)
## Type "reminder help" for instructions

echo ""
echo "(reminder by alex kasman (c) 1993)"

echo ""
## HELP?

if [ "$1" = "help" ]; then

grep "^#%" `which reminder`|sed 's/#%//'|more

else

## .reminderrc?

if [ -f .reminderrc ]; then

## IF no argument, set number of days to lookahead to 8
## else set to argument

if [ "$1" = "pause" ]; then
pause="100"
lookahead="8"
else
if [ -z "$1" ]; then
   lookahead="8"
else
   lookahead="$1"
fi
fi

if [ "30" -lt "$lookahead" ]; then
echo "The requested number of days is too large."
echo "Maximum *lookahead* is 30."
lookahead=30
fi

 
## get today's info
today=`date`


day=`echo $today|sed 's/[a-zA-Z ]*\([0-9]*\).*/\1/'`
dmonth=`echo $today|sed 's/^[a-zA-Z]* \([a-zA-Z]*\) .*/\1/'`
dday=`echo $today|sed 's/^\([A-Z][a-z][a-z]\).*$/\1/'`

case "$dmonth" in
Jan)
month=1;;
Feb)
month=2;;
Mar)
month=3;;
Apr)
month=4;;
May)
month=5;;
Jun)
month=6;;
Jul)
month=7;;
Aug)
month=8;;
Sep)
month=9;;
Oct)
month=10;;
Nov)
month=11;;
Dec)
month=12;;
esac


case $dday in
Sun)
wday="1";;
Mon)
wday="2";;
Tue)
wday="3";;
Wed)
wday="4";;
Thu)
wday="5";;
Fri)
wday="6";;
Sat)
wday="7";;
esac
 

## If the .remindertemp file is up to date
if [ -f ".remindertemp" ]; then
temptest=`grep "today is $month-$day -- \*Reminders\* for next $lookahead days:" .remindertemp`
else
temptest=""
fi


if [ -z "$temptest" ]; then
touch .remindertemp
# Print a calendar on the screen
cal

echo "  ------ today is $month-$day -- Collecting info for next $lookahead days:"


# find last day of this month
lastday=`cal|tail -1|sed 's/^.*\([0-9][0-9]\)$/\1/'`

if [ "$lastday" = "" ]; then
lastday=`cal|tail -2|head -1|sed 's/^.*\([0-9][0-9]\)$/\1/'`
fi

# Print the relevant information from .reminderrc
i=0
temp=" TODAY >"
mo=`echo "$month-1"|bc`
if [ "$mo" = "0" ]; then
mo="12"
fi

touch .remindertemp
rm .remindertemp
touch .remindertemp

echo "
  ------ today is $month-$day -- *Reminders* for next $lookahead days:
" >> .remindertemp

echo -n "  "

(while [ "$i" -lt "$lookahead" ]; do
if [ "$month$day" = "41" ]; then
echo "   4-1: Receive \$1,000,000.00 from LJM"
fi

grep "^[!0]*$month-[0]*$day:" .reminderrc | sed "s/^[!]*\(.*\)$/ $temp \1/"
grep "^*-[0]*$day:" .reminderrc | sed "s/^*/*$temp $month/"
grep "^w-$wday:" .reminderrc |sed "s/^w-$wday/w$temp $month-$day/"
temp=">"

day=`echo "$day+1"|bc`
if [ "$wday" = "7" ]; then
wday="1"
else
wday=`echo "$wday+1"|bc`
fi

i=`echo "$i+1"|bc`

if [ "$lastday" -lt "$day" ]; then
day="1"
month=`echo "$month+1"|bc`
if [ "$month" = "13" ]; then
month="1"
fi
echo "  ---"
fi
echo -n "." >&2
done ) >> .remindertemp
echo ""
fi
clear
more .remindertemp

echo ""

if [ ! "0" = `grep -c "^[0]*$mo-" .reminderrc` ]; then
echo -n "Shall I remove last month's *reminders*? (y/n) "
read ans
if [ "$ans" = "y" ]; then
echo "e .reminderrc
,g/^[0]*$mo-/d
w"|ed -s
fi
fi
if [ ! -z "$pause" ]; then
echo "TYPE RETURN TO REMOVE"
read ans
fi
else # if no file .reminderrc
echo "THERE IS NO FILE .reminderrc IN THIS DIRECTORY"
echo "FOR HELP, TYPE reminder help."
fi
fi
## Help message appears below
#%
#% Reminder is a program which is designed to remind you of upcoming
#% events.  The syntax is very simple...you just type:
#% "reminder [n] [help]"
#% where the square brackets mean that this is optional.  The optional
#% number "n" tells the program how many days in advance you want to see.
#% The default, if you don't give a number, is 8.  If today is Monday, for
#% example, this means you will see all of the events scheduled between today
#% and the following Monday.
#%
#% Basic use:
#% Simply use the program "addreminder" to create a list of events.  It will
#% prompt you for a month (a number between 1 and 12) and a day (a number
#% between 1 and 31).  Then it will prompt you for a message.  After that,
#% when you type "reminder" it will remind you of this event beginning a
#% week in advance!  It might be a good idea to put "reminder" in your
#% .login file.  Then, you will be "reminded" every time you log in.
#%
#% The program will automatically remove "reminders" from the previous 
#% month.  That is, if it is currently October, reminder will look to see
#% whether there are any messages from September still in your file.  If
#% there are, then reminder will ask you whether you want to have them removed.
#%
#% Special effects:
#% There are three "special effects" that you can get by altering the
#% month entry.  If you want a message to show up EVERY month for the
#% same day, you can simply use the character "*" instead of a number.
#% For example, if you type "*" for the month, "1" for the day and "PAY RENT"
#% for the message, you will be reminded to pay the rent on the first day of
#% every month.
#%
#% The second special effect is for messages that should show up on the
#% same day every year.  Let's say your mother's birthday is on 9-2.
#% You can add this to your reminder file, but you DON'T want it to get erased
#% once October arrives (so that it will be there next year as well).  
#% Simply put a "!" in front of the month (i.e., !9) to let reminder
#% know that this is a yearly event.
#%
#% The final special effect is for an event which is the same day every
#% week.  In this case, you type "w" for the month and a number to represent
#% the day of the week.  For example, if you want to be reminded to go
#% shopping every Sunday and to go to class on Wednesday, you would enter
#% "w-1: Go shopping" and "w-4: Go to class".
#%
#% The File:
#% The events you save will be stored in the file .reminderrc.  That file
#% must be in the directory that you are in when you call reminder!!  In
#% particular, you can keep a separate one in different directories if you
#% want to.  However, I suggest that you have only one .reminderrc file
#% and that you keep it in your home directory.
#%
#% You can edit the .reminderrc file with any standard editor, if you want
#% to.  The syntax in the file is NOT mysterious.  Each line simply looks
#% like:
#% month-day: message
#% where "month" is "number", "!number" or "*" and day is any number from
#% 1 to 31.  Since new events are added at the end of the file, it will
#% end up "self organizing" so that permanent events (* and !) will be
#% at the beginning of the file.  However, the order does NOT make a difference.
#%
#% Bugs:
#% I don't know of any bugs, yet.  The number of days to look ahead cannot be
#% more than 30 (if it is, it will just use 30.)  Reminder uses the program
#% "cal" and will not work without it!!
#%
#% alexk@math.bu.edu