>From 7c037ac3c7480c370372e56654cec467f3c5409f Mon Sep 17 00:00:00 2001
From: Rich Megginson <rmeggins@redhat.com>
Date: Fri, 6 Nov 2009 21:11:11 -0700
Subject: [PATCH] Remove old package during upgrade
 Upgrading 389 on top of Fedora does not remove the old Fedora package.  The
 installer will now run the uninstall program to remove the old package.  In
 addition, package uninstall does not remove the old desktop shortcut nor
 the old program menu, so we have a script to remove those.

---
 win/Console.wxs |   72 ++++++++++++++++++++++++++++++++++++++++++++++++------
 win/Makefile    |    7 ++++-
 win/VERSION.mak |    8 +++++-
 3 files changed, 77 insertions(+), 10 deletions(-)

diff --git a/win/Console.wxs b/win/Console.wxs
index 2e3ce3d..a247770 100644
--- a/win/Console.wxs
+++ b/win/Console.wxs
@@ -56,11 +56,8 @@
       InstallerVersion='100' Languages='1033' Compressed='yes' SummaryCodepage='1252' />
 
     <!-- previous versions of this package were installed using the per-user context -->
-    <!-- we cannot upgrade a per-machine package on top of a per-user package, nor -->
-    <!-- is there an easy way to convert a per-user package to a per-machine package -->
-    <!-- it seems the default changes per platform, so explicitly set this package -->
-    <!-- to be per-user -->
-    <Property Id='ALLUSERS' Value="2" />
+    <!-- explicitly set this and future packages to be per-machine (All Users) -->
+    <Property Id='ALLUSERS' Value="1" />
 
     <Upgrade Id='$(var.UPGRADEGUID)'>
       <UpgradeVersion OnlyDetect='yes' Property='NEWERFOUND'
@@ -70,8 +67,66 @@
     <Media Id='1' Cabinet='Sample.cab' EmbedCab='yes' DiskPrompt="CD-ROM #1" />
     <Property Id='DiskPrompt' Value="Console Installation [1]" />
 
+<?ifdef OLDGUID ?>
+    <Property Id='OLDGUID' Value="{$(var.OLDGUID)}" />
+<?endif ?>
+<?ifdef OLDSHORTCUT ?>
+    <Property Id='OLDSHORTCUT' Value="$(var.OLDSHORTCUT)" />
+<?endif ?>
+<?ifdef OLDPROGRAMFOLDER ?>
+    <Property Id='OLDPROGRAMFOLDER' Value="$(var.OLDPROGRAMFOLDER)" />
+<?endif ?>
+
+    <Property Id='MSIEXEC' Value="msiexec.exe" />
+
     <CustomAction Id='AlreadyUpdated' Error='[ProductName] [ProductVersion] is already installed.' />
     <CustomAction Id='NoDowngrade' Error='A later version of [ProductName] is already installed.' />
+    <CustomAction Id='RemoveOldPkg' Property='MSIEXEC' ExeCommand='/x [OLDGUID] /qn' Return='asyncNoWait' />
+    <CustomAction Id='RemoveOldShortcuts' Script='vbscript'>
+      <![CDATA[
+      On error resume next
+      set rec = Session.installer.CreateRecord(1)
+      msgtype = &H04000000
+      rec.StringData(1) = "RemoveOldShortcuts: Begin"
+      Session.Message msgtype, rec
+      Dim fso
+      Set fso = CreateObject("Scripting.FileSystemObject")
+      oldsc = Session.Property("OLDSHORTCUT")
+      rec.StringData(1) = "RemoveOldShortcuts: old shortcut is " & oldsc
+      Session.Message msgtype, rec
+      oldfldr = Session.Property("OLDPROGRAMFOLDER")
+      rec.StringData(1) = "RemoveOldShortcuts: old program folder is " & oldfldr
+      Session.Message msgtype, rec
+      dskfldr = Session.Property("DesktopFolder")
+      rec.StringData(1) = "RemoveOldShortcuts: DesktopFolder is " & dskfldr
+      Session.Message msgtype, rec
+      For Each userfldr in fso.GetFolder(dskfldr).ParentFolder.ParentFolder.SubFolders
+        ' for each userfldr
+        ' look for Desktop\oldshortcut and remove
+        fullpath = userfldr.path & "\Desktop\" & oldsc
+        If fso.FileExists(fullpath) Then
+          rec.StringData(1) = "RemoveOldShortcuts: Removing old shortcut " & fullpath
+          Session.Message msgtype, rec
+          fso.DeleteFile(fullpath)
+        Else
+          rec.StringData(1) = "RemoveOldShortcuts: ignoring missing old shortcut " & fullpath
+          Session.Message msgtype, rec
+        End If
+        ' look for Start Menu\Programs\oldfldr and remove
+        fullpath = userfldr.path & "\Start Menu\Programs\" & oldfldr
+        If fso.FolderExists(fullpath) Then
+          rec.StringData(1) = "RemoveOldShortcuts: Removing old program folder " & fullpath
+          Session.Message msgtype, rec
+          fso.DeleteFolder(fullpath)
+        Else
+          rec.StringData(1) = "RemoveOldShortcuts: ignoring missing old program folder " & fullpath
+          Session.Message msgtype, rec
+        End If
+      Next
+      rec.StringData(1) = "RemoveOldShortcuts: End"
+      Session.Message msgtype, rec
+      ]]>
+    </CustomAction>
 
     <Directory Id='TARGETDIR' Name='SourceDir'>
       <!-- the VC runtime redist package -->
@@ -177,10 +232,10 @@
           <Component Id='ConsoleBat' Guid='00DEC033-5D77-49a2-9478-FA67E8D4EFEA'>
             <File Id='CONSOLEBAT' LongName='$(var.FILEPREF)-console.bat' Name='con.bat' DiskId='1' src='$(var.FILEPREF)-console.bat' Vital='yes' >
                 <Shortcut Id="startmenuConsole" Directory="ProgramMenuDir" Name="CONFLDR" 
-                  LongName="$(var.PRODUCTNAME)" WorkingDirectory='INSTALLDIR'
+                  LongName="$(var.SHORTCUT)" WorkingDirectory='INSTALLDIR'
                   Icon="Logo$(var.BRANDNOSPACE).ico" IconIndex="0" Advertise="yes" />
                 <Shortcut Id="desktopConsole" Directory="DesktopFolder" Name="CONFLDR"
-                  LongName="$(var.PRODUCTNAME)" WorkingDirectory='INSTALLDIR'
+                  LongName="$(var.SHORTCUT)" WorkingDirectory='INSTALLDIR'
                   Icon="Logo$(var.BRANDNOSPACE).ico" IconIndex="0" Advertise="yes" />
             </File>
           </Component>
@@ -241,13 +296,14 @@
     <Property Id="INSTALLLEVEL">3</Property>
 
     <!-- we don't really allow fancy dialogs and customization, so just use minimal -->
-    <!--    <UIRef Id="WixUI_Mondo" /> -->
     <UIRef Id="WixUI_Minimal" />
     <UIRef Id="WixUI_ErrorProgressText" />
 
     <InstallExecuteSequence>
       <Custom Action='NoDowngrade' After='FindRelatedProducts'>NEWERFOUND</Custom>
       <RemoveExistingProducts After='InstallFinalize' />
+      <Custom Action='RemoveOldPkg' After='InstallFinalize'>OLDGUID AND NOT Installed</Custom>
+      <Custom Action='RemoveOldShortcuts' After='RemoveOldPkg'>OLDSHORTCUT AND OLDPROGRAMFOLDER AND NOT Installed</Custom>
     </InstallExecuteSequence>
 
     <Icon Id="Logo$(var.BRANDNOSPACE).ico" SourceFile="Logo$(var.BRANDNOSPACE).ico" />
diff --git a/win/Makefile b/win/Makefile
index cdb9fd8..9982eb4 100644
--- a/win/Makefile
+++ b/win/Makefile
@@ -194,12 +194,17 @@ LAYOUT : $(PKGDIR) $(NSPRLIBDIR)\libnspr4.dll $(NSSLIBDIR)\nss3.dll $(JSSDIR)\js
 	copy /Y License.rtf $(PKGDIR)
 
 $(PKGDIR)\$(FULL_PKGNAME) : $(PKGDIR) LAYOUT $(PKGDIR)\$(CANDLE) $(PKGNAME).wxs
-	cd $(PKGDIR) & cd & $(CANDLE) -nologo "-dCRTMSM=$(CRTMSM)" "-dPOLICYCRTMSM=$(POLICYCRTMSM)" -dVERSION=$(VERSION) -dUSE64=$(USE64) "-dBRAND=$(BRAND)" -dBRANDNOSPACE=$(BRANDNOSPACE) "-dVENDOR=$(VENDOR)" -dPKGGUID=$(PKGGUID) "-dPRODUCTNAME=$(PRODUCTNAME)" -dFILEPREF=$(FILEPREF) -dUPGRADEGUID=$(UPGRADEGUID) -out $(PKGNAME).wixobj ..\$(PKGNAME).wxs
+	cd $(PKGDIR) & cd & $(CANDLE) -nologo "-dCRTMSM=$(CRTMSM)" "-dPOLICYCRTMSM=$(POLICYCRTMSM)" -dVERSION=$(VERSION) -dUSE64=$(USE64) "-dBRAND=$(BRAND)" -dBRANDNOSPACE=$(BRANDNOSPACE) "-dVENDOR=$(VENDOR)" -dPKGGUID=$(PKGGUID) "-dPRODUCTNAME=$(PRODUCTNAME)" -dFILEPREF=$(FILEPREF) -dUPGRADEGUID=$(UPGRADEGUID) -dOLDGUID=$(OLDGUID) "-dSHORTCUT=$(SHORTCUT)" "-dOLDSHORTCUT=$(OLDSHORTCUT)" "-dOLDPROGRAMFOLDER=$(OLDPROGRAMFOLDER)" -out $(PKGNAME).wixobj ..\$(PKGNAME).wxs
 	$(LIGHT) -nologo -out $@ -b $(PKGDIR) $(PKGDIR)\$(PKGNAME).wixobj $(WIXDIR)\wixui.wixlib -loc $(WIXDIR)\WixUI_en-us.wxl
 
 clean:
 	rmdir /S /Q $(PKGDIR)
 
+cleanpkg:
+	del $(PKGDIR)\$(FULL_PKGNAME)
+	del $(PKGDIR)\$(PKGNAME).wixobj
+	del $(PKGDIR)\$(FILEPREF)-console.bat
+
 help:
 	@echo You must define the following paths:
 	@echo BASEDIR - the directory containing all of the other components' directories
diff --git a/win/VERSION.mak b/win/VERSION.mak
index 578c764..9238b07 100644
--- a/win/VERSION.mak
+++ b/win/VERSION.mak
@@ -17,8 +17,14 @@ PRODUCTNAME=$(BRAND) Management Console
 PKGGUID=f6570f14-9613-4e40-9e14-450aad257f0e
 # the upgrade GUID should usually not be changed
 UPGRADEGUID=7EA828C0-C219-438d-9BB3-3418DC900D60
+# guid of old version to be removed
+OLDGUID=36C8F6DB-6C1B-416e-83E4-31A0688EBD3F
+OLDSHORTCUT=Fedora IDM Console.lnk
+OLDPROGRAMFOLDER=Fedora Identity Management Console
 # filename prefix for certain branded jar and script files
 FILEPREF=389
 # location of brand specific bitmaps
 BITMAPDIR=Bitmaps
-ICONDIR=.
\ No newline at end of file
+ICONDIR=.
+# name of desktop shortcut to create
+SHORTCUT=389 Console
-- 
1.5.6.1.1071.g76fb

