installer: Uninstall old version before installing new version

diff -r 814130b27945 installer/tortoisehg.iss
--- a/installer/tortoisehg.iss	Tue Jan 13 21:16:47 2009 -0600
+++ b/installer/tortoisehg.iss	Fri Jan 16 21:14:20 2009 +0100
@@ -59,3 +59,25 @@
 Filename: "{app}\add_path.exe"; Parameters: "/del {app}"
 Filename: "regsvr32.exe"; Parameters: "/s /u ""{app}\tortoisehg.dll"""
 
+[Code]
+{ Uninstall old TortoiseHg before proceeding with installation }
+function InitializeSetup(): Boolean; 
+var
+  Uninstaller: String;
+  ResultCode: Integer;
+begin
+  Result := True;
+  if not RegQueryStringValue(HKLM, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\TortoiseHg_is1', 'UninstallString', Uninstaller) then Exit;
+
+  Result := MsgBox('Old version must be uninstalled before installation.' #13
+    'Do you want me to uninstall it now?', mbConfirmation, MB_YESNO) = idYes;
+
+  if not Result then Exit; {quit and abort installation}
+
+  MsgBox('Warning: Old Version will be removed!', mbInformation, MB_OK); 
+  if Exec(RemoveQuotes(Uninstaller), ' /SILENT', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode)
+  then Exit;
+
+  Result := MsgBox('Unable to uninstall old version.'#13
+    'Do you really want to start setup?', mbConfirmation, MB_YESNO) = idYes;
+end;
