-- Copyright (C)2009 Sun Microsystems, Inc.
-- Copyright (C)2011 D. R. Commander
--
-- This library is free software and may be redistributed and/or modified under
-- the terms of the wxWindows Library License, Version 3.1 or (at your option)
-- any later version.  The full license is in the LICENSE.txt file included
-- with this distribution.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- wxWindows Library License for more details.

set disk to (path to startup disk) as string
set tmpfile to path to temporary items from user domain
set tmpfile to POSIX path of tmpfile & "tvnc_uninstall.log"
set uninstaller to disk & "@TVNC_MACPREFIX@:bin:uninstall"
set uninstaller to "sh " & POSIX path of uninstaller & " 2>&1 >" & tmpfile
set success to 0

display dialog "You are about to uninstall TurboVNC.  Proceed?" buttons {"Yes", "No"} default button "No"

if button returned of result is "Yes" then
	try
		do shell script (uninstaller) with administrator privileges
		set success to 1
	on error errstr number errnum
		if errnum is -128 then
			display dialog "Uninstall aborted." buttons {"OK"}
		else if errnum is 255 then
			set errmsg to "The uninstall script could not remove some of the files or directories installed by the TurboVNC package.  Consult:" & return & return & tmpfile & return & return & "for more details."
			display dialog errmsg buttons {"OK"} default button "OK" with icon caution
		else if errnum is 127 then
			display dialog "Could not find the TurboVNC uninstall script.  The TurboVNC package may have already been uninstalled." buttons {"OK"} default button "OK" with icon stop
		else
			set errmsg to "ERROR " & errnum & ": " & errstr
			display dialog errmsg buttons {"OK"} default button "OK" with icon stop
		end if
	end try
	if success is 1 then
		display dialog "TurboVNC has been successfully uninstalled." buttons {"OK"}
	end if
else
	display dialog "Uninstall aborted." buttons {"OK"}
end if
