#!/system/bin/tcsh -f
#command line tool to list/insert/delete one-time calendar events


#database file in android 4.2.2
set db=/data/data/com.android.providers.calendar/databases/calendar.db
#your timezone
set TZNAME=Europe/Prague

set now=`date +%s`"000"



#list all 
if ( ${#} == 0 ) then
setenv TZ $TZNAME
sqlite3 $db <<! |\
sort -n -t '|' -k 13|\
gawk '-F|' '{printf("%3d. %-12s %-15s %s\n",$1,$6,$7,strftime("%B  %d %H:%M %Y %Z",$13/1000));}'
select * from Events;
!
exit 0
endif

if ( x"$1" == "xclean" || x"$1" == "xclear"  ) then
sqlite3 $db <<! |\
awk '-F|' '$13 < '$now' {printf("delete from Events where _id=%d;\ndelete from Reminders where event_id=%d;\ndelete from Instances where event_id=%d;\ndelete from EventsRawTimes where event_id=%d;\ndelete from CalendarAlerts where event_id=%d;\n",$1,$1,$1,$1,$1);}' >/data/tmp/cal$$.tmp
select * from Events;
!
#cat /data/tmp/cal$$.tmp

sqlite3 $db <  /data/tmp/cal$$.tmp
rm -f  /data/tmp/cal$$.tmp
exit 0
endif

set duration=3600
if ( x"$1" == "x-d"  ) then
	shift
	set duration=`expr $1 '*' 60`
	shift
endif

if ( x"$1" == "xadd"  ) then
shift
set label=$1
set loc=$2
shift
shift
set TZSHIFT=0
#NOTE! /usr/share/zoneinfo/ must have been copied from a linux computer for gdate to work correctly
#android on reboot removes /usr, so redo it if needed
if ( ! -e /usr/share/zoneinfo ) then
echo check /storage/emulated/legacy/zoneinfo/
echo Possibly scp -r -P 2222  -oKexAlgorithms=+diffie-hellman-group1-sha1  /usr/share/zoneinfo/ root@vpnsamsung:/storage/emulated/legacy/zoneinfo
mount -o remount,rw /
mkdir /usr
chmod 755 /usr
mkdir /usr/share
chmod 755 /usr/share
ln -s /storage/emulated/legacy/zoneinfo/ /usr/share/zoneinfo
mount -o remount,ro /
endif
set start0=`gdate --date='TZ="'$TZNAME'" '"$* " +%s`
unsetenv TZ
set start=`expr $start0 - $TZSHIFT`
set end0=`expr $start0 + $duration`
set end=`expr $start + $duration`
set startraw=`gdate -d @$start0 +%Y%m%dT%H%M%S`
set endraw=`gdate -d @$end0 +%Y%m%dT%H%M%S`
set year=`gdate -d @$start0 +%Y`
set month=`gdate -d @$start0 +%m`
set day=`gdate -d @$start0 +%d`
set julianday=`exps -a y=$year -a m=$month -a d=$day -o "\
	if (m <= 2) {\
                        y -= 1;\
                        m += 12;\
                }\
 	a=floor(y/100);\
	b= 2-a+floor(a/4);\
	j= floor(365.25*(y + 4716)) + floor(30.6001*(m+1)) + d + b - 1524.5;\
	ceil(j)\
"`
#echo Julian day = $julianday
set startmin=`exps -o "mod($start0/60,24*60)"`
set endmin=`exps -o "min(2,$startmin+60,1439)"`

cat <<! >/data/tmp/cal$$.tmp
INSERT INTO Events (hasAlarm,latitude,longitude,sticker_type,hasAttendeeData,lastDate,organizer,eventStatus,dirty,_sync_id,calendar_id,title,eventLocation,dtstart,dtend,eventTimezone) VALUES(1,0,0,0,1,${end}000,'My calendar',0,1,'My calendar${now}',1,'$label','$loc',${start}000,${end}000,'$TZNAME');
select _id from Events where title=='$label';
!
set my_event_id=`sqlite3 $db </data/tmp/cal$$.tmp|tail -1`
rm -f /data/tmp/cal$$.tmp
echo Created event $my_event_id

sqlite3 $db <<!
INSERT INTO Instances (event_id,begin,end,startDay,endDay,startMinute,endMinute) VALUES($my_event_id,${start}000,${end}000,$julianday,$julianday,$startmin,$endmin);
INSERT INTO EventsRawTimes (event_id,dtstart2445,dtend2445,lastDate2445) VALUES($my_event_id,'$startraw','$endraw','$endraw');
INSERT INTO Reminders (event_id,minutes,method) VALUES($my_event_id,15,1);
!

exit 0
endif

if ( x"$1" == "xdel"  ) then
shift
cat /dev/null >  /data/tmp/cal$$.tmp
foreach i ($*)
	echo delete from Events where _id=$i';' >>/data/tmp/cal$$.tmp
	echo delete from Instances where event_id=$i';' >>/data/tmp/cal$$.tmp
	echo delete from Reminders where event_id=$i';' >>/data/tmp/cal$$.tmp
	echo delete from EventsRawTimes where event_id=$i';' >>/data/tmp/cal$$.tmp
	echo delete from CalendarAlerts where event_id=$i';' >>/data/tmp/cal$$.tmp
end
sqlite3 $db < /data/tmp/cal$$.tmp
rm -f  /data/tmp/cal$$.tmp
exit 0
endif

echo Unknown command $1
exit 1
