#!/bin/tcsh

# the above line is ignored the users default shell is used: im my case tcsh
# make sure u have xemacs, sed, grep, tr and touch as they should be *LINUX*

set myName="rabin shahav"
set myEmail="rabin.shahav@freescale.com"

set tmpMsg=/tmp/msg$$
set tmpMsgIn=/tmp/msgIn$$
set tmpEmacsDo=/tmp/emacsDo$$
set debugInfo=/tmp/owaDebug_$USER
onintr myInterrupt

cat > $tmpMsgIn

set whatStr=`grep -m 1 "^Subject: " $tmpMsgIn| sed "s/Subject://" | tr / - `
set whenStr=`grep -m 1 "^When: " $tmpMsgIn | sed "s/When://"|tr -s ',' ' ' `
set whereStr=`grep -m 1 "^Where: " $tmpMsgIn | sed "s/Where://" | tr / - `
set fromStr=`grep -m 1 "^From: " $tmpMsgIn | sed "s/From://" | tr / - `

# add more hebrew days =E9=E5=ED = yom =E7=EE=E9=F9=E9 = hamishi ...
foreach day ( Sunday Monday Tuesday Wednesday Thursday Friday Saturday =E9=E5=ED =E7=EE=E9=F9=E9 )
    set whenStr=`echo $whenStr | sed "s/$day//"`
end
set whenStr=`echo $whenStr | sed "s/ AM//g" | sed "s/ PM/PM/g"`

# $debugInfo is for debug.
touch $debugInfo
echo ==================== >> $debugInfo
echo whatStr $whatStr >> $debugInfo
echo whenStr $whenStr >> $debugInfo
echo fromStr $fromStr >> $debugInfo
echo whereStr $whereStr >> $debugInfo

set yearStr=`echo $whenStr | cut -d' ' -f3`
set monthStr=`echo $whenStr | cut -d' ' -f2`
set dayStr=`echo $whenStr | cut -d' ' -f1`
set hmStartStr=`echo $whenStr | cut -d' ' -f4| cut -f1 -d'-'`
set hmEndStr=`echo $whenStr | cut -d' ' -f4| cut -f2 -d'-'`

# parse start time
set hourStartStr=`echo $hmStartStr | cut -d: -f1`
set minutsStartStr=`echo $hmStartStr | cut -d: -f2`
if ( `echo $minutsStartStr | grep PM ` != "" ) then 
  set hourStartStr=`echo $hourStartStr + 12| bc`
endif
if ( $hourStartStr < 10 ) then
  set hourStartStr=0`echo $hourStartStr|bc`
endif
set minutsStartStr=`echo $minutsStartStr| sed "s/PM//"`

# parse end time
set hourEndStr=`echo $hmEndStr | cut -d: -f1`
set minutsEndStr=`echo $hmEndStr | cut -d: -f2`
if ( `echo $minutsEndStr | grep PM ` != "" ) then 
  set hourEndStr=`echo $hourEndStr + 12| bc`
endif
if ( $hourEndStr < 10 ) then
  set hourEndStr=0`echo $hourEndStr|bc`
endif
set minutsEndStr=`echo $minutsEndStr| sed "s/PM//"`

set monthStrNonDigit=`echo $monthStr | tr -d "0123456789"`
if ( $monthStrNonDigit == "" ) then
    set tmpDayStr=$dayStr
    set dayStr=$monthStr
    set monthStr=$tmpDayStr
endif

echo yearStr $yearStr >> $debugInfo
echo monthStr $monthStr >> $debugInfo
echo dayStr $dayStr >> $debugInfo
echo hmStartStr $hmStartStr >> $debugInfo
echo hmEndStr $hmEndStr >> $debugInfo
echo hourStartStr $hourStartStr >> $debugInfo
echo minutsStartStr $minutsStartStr >> $debugInfo
echo hourEndStr $hourEndStr >> $debugInfo
echo minutsEndStr $minutsEndStr >> $debugInfo

set monthNum=00
switch ($monthStr)
    case "January"
	set monthNum=01
	breaksw
    case "February"
	set monthNum=02
	breaksw
    case "March"
    case "=EE=F8=F5"
	set monthNum=03
	breaksw
    case "April"
	set monthNum=04
	breaksw
    case "May"
	set monthNum=05
	breaksw
    case "June"
	set monthNum=06
	breaksw
    case "July"
	set monthNum=07
	breaksw
    case "August"
	set monthNum=08
	breaksw
    case "September"
	set monthNum=09
	breaksw
    case "October"
	set monthNum=10
	breaksw
    case "November"
	set monthNum=11
	breaksw
    case "December"
	set monthNum=12
	breaksw
endsw


touch $tmpMsg
echo 'To: '"$myEmail" >> $tmpMsg
echo 'Subject:' "$whatStr" >> $tmpMsg
echo 'From:' "$fromStr" >> $tmpMsg
echo 'Content-Type: text/calendar; name=calendar.ics; charset=utf-8; METHOD=PUBLISH' >> $tmpMsg
echo 'Organization: Freescale' >> $tmpMsg
echo '--text follows this line--' >> $tmpMsg
echo 'Content-Transfer-Encoding: 8bit' >> $tmpMsg
echo '' >> $tmpMsg
echo 'BEGIN:VCALENDAR' >> $tmpMsg
echo 'CALSCALE:GREGORIAN' >> $tmpMsg
echo 'PRODID:-//Ximian//NONSGML Evolution Calendar//EN' >> $tmpMsg
echo 'VERSION:2.0' >> $tmpMsg
echo 'METHOD:REQUEST' >> $tmpMsg
echo 'BEGIN:VEVENT' >> $tmpMsg
echo 'DTSTART;TZID=/softwarestudio.org/Olson_20011030_5/Asia/Jerusalem:' >> $tmpMsg
echo " "${yearStr}${monthNum}${dayStr}T${hourStartStr}${minutsStartStr}00  >> $tmpMsg
echo 'DTEND;TZID=/softwarestudio.org/Olson_20011030_5/Asia/Jerusalem:' >> $tmpMsg
echo " "${yearStr}${monthNum}${dayStr}T${hourEndStr}${minutsEndStr}00 >> $tmpMsg
echo 'TRANSP:OPAQUE' >> $tmpMsg
echo 'SEQUENCE:3' >> $tmpMsg
echo 'SUMMARY:' "$whatStr" >> $tmpMsg
echo 'LOCATION:' "$whereStr" >> $tmpMsg
echo 'CLASS:PUBLIC' >> $tmpMsg
echo 'ORGANIZER;CN='"$fromStr"':MAILTO:'"$fromStr" >> $tmpMsg
# echo 'ORGANIZER;' "$fromStr" >> $tmpMsg
# echo 'X-MICROSOFT-CDO-REPLYTIME:20050307T065901Z' >> $tmpMsg
echo 'ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=CHAIR;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;' >> $tmpMsg
echo ' CN='"$myName"';LANGUAGE=en:MAILTO:'"$myEmail" >> $tmpMsg
echo 'END:VEVENT' >> $tmpMsg
echo 'END:VCALENDAR' >> $tmpMsg

# use xemacs instead of sendmail for now.
echo "(defalias 'mowa (read-kbd-macro" > $tmpEmacsDo
echo '"M-x mail RET C-a 5*<C-k> C-x i '${tmpMsg}' RET C-c C-c"))' >>$tmpEmacsDo
echo "(execute-kbd-macro 'mowa )" >> $tmpEmacsDo
xemacs -batch -q -l $tmpEmacsDo 

# cleanup
rm -f $tmpMsg $tmpMsgIn $tmpEmacsDo
exit 0
myInterrupt:
 rm -f $tmpMsg $tmpMsgIn $tmpEmacsDo
 exit -1





