#!/usr/bin/osascript

-- -------------------------------------------------------------------- --
--                                                                      --
--            (C) Kerio Technologies s.r.o. All rights reserved.        --
--                                                                      --
-- -------------------------------------------------------------------- --

-- Functions.

-- Logging function
to log {logString, logFile}
	try
		do shell script "echo \"" & logString & "\" >> " & logFile
	end try
end log

-- Display dialog
to displayDialog(textMsg)
	do shell script "osascript -e 'tell application \"System Events\"' -e \"activate\" -e \"display dialog \\\"" & textMsg & "\\\" buttons {\\\"OK\\\"} default button 1\" -e 'end tell'"
end displayDialog

-- Compare Mac OS X version with supplied "ver" version. 
to minmaxOSVersion(ver, min)
	set decNum to 0
	set numList to characters of (ver as string)
	repeat with num in numList
		set decNum to (16 * decNum) + num
	end repeat
	tell application "Finder" to set sysv to (system attribute "sysv")
	if min then
		if sysv < decNum then return false
	else
		if sysv > decNum then return false
	end if
	return true
end minmaxOSVersion

--
-- Main.
--

-- Check whether Finder is responding.
repeat
	try
		tell application "Finder"
			set testFinderResponsiveness to (container of (path to me)) as alias as string
			exit repeat
		end tell
	on error errStr
		set errMsg to "Finder is not responsive to AppleScript calls.
" & "Error: " & errStr
		displayDialog("Finder is not responding. Please close all Finder pop-up windows, finish all Finder actions, and click OK to continue.")
	end try
end repeat

-- Get path to AppleScript include file
set configToolCommonIncl to ""
try
	tell application "Finder"
		set pathToThisFile to path to me
		set scriptFolder to (container of pathToThisFile) as alias as string
		set configToolCommonIncl to scriptFolder & "common.scpt"
		
		set scriptFolderQuotedPOSIX to quoted form of (POSIX path of scriptFolder)
		do shell script "osacompile -o " & scriptFolderQuotedPOSIX & "common.scpt " & scriptFolderQuotedPOSIX & "common.applescript"
	end tell
on error errStr
	set errMsg to "Failed to get common file name.
" & "Error: " & errStr & "
Exiting."
	displayDialog(errMsg & " Restart your Mac and try again.")
	error
end try

-- Load common functions
global ConfigToolCommon
set ConfigToolCommon to load script file configToolCommonIncl

-- Start logging
set logFile to ConfigToolCommon's initLogger("iCal Config Tool", "kerio-ical-config-tool.log")

-- Check version.
try
	if not ((minmaxOSVersion(1050, true)) and (minmaxOSVersion(1070, false))) then
		displayDialog("iCal Config Tool is designed to run on Mac OS X 10.5 and 10.6 only.")
		log {"Incompatible Mac OS X version.", logFile}
		error
	end if
on error errStr
	set errMsg to "Version check failed.
" & "Error: " & errStr & "
Exiting."
	log {errMsg, logFile}
	error
end try

ConfigToolCommon's checkConfigFile("iCal CalDAV")
ConfigToolCommon's checkRunningApps({"iCal", "Directory Utility"})

set scriptResult to ConfigToolCommon's launchPython()

return scriptResult
