@echo off
::
:: onIncomingCall $calldate $calltime $callnumber $callname
::
::  handler for onIncomingCall event
::

:: blame: shardy@@differentchairs.com

:: uses 3rd party growl app (http://growlforwindows.com) for
:: displaying transient notification messages in local UI

setlocal
set cidLogfile=c:\callerId.log
set cdate=%1
set ctime=%2
if not defined MAXLOGFILESIZE set MAXLOGFILESIZE=5000000
if not %3.==. set cnumber=%~3
if not %4.==. (
	for /f "tokens=3*" %%a in ("%*") do set cname=%%~b
	)

set notification=%cdate%-%ctime%

if defined cnumber set notify2=%cnumber%
if defined cname   set notify2=%notify2% %cname%

echo growlnotify /t:CallerId %notification% %notify2%
growlnotify  /i:%~dp0\cid.ico  /t:CallerId "%notification% \n%notify2%"

call :limitAt %MAXLOGFILESIZE% %cidLogfile% old

echo %notification%  %notify2% >>%cidLogfile%

::
:: here's where I could send out custom notifications (sms, etc). The
:: action can be a function of the target number
::

endlocal
goto :eof

:: if filesize exceeds given size, copy to {file}.{bakExt} and delete original
:: usage - limitAt {sizeLimit} {file} {bakExt}
:limitAt
	if %3.==. goto :eof
	if not %4.==. goto :eof
	if not exist %2 goto :eof

	set maxSize=%1
	set theFile=%2
	set bakExt=%3

	set fname=%~nx2
	:: this line probably not highly backward compatible (works on xp 5.1.26)
	set currentSize=%~z2

	if  %currentSize% GEQ %maxSize% (
	  echo limiting: move %theFile% to %theFile%.%bakExt%
	  move /y %theFile% %theFile%.%bakExt%
	  )
	goto :eof