Bug 332714: Brand console application on Windows

The name of the console application must match the GUI application name
or it will try to load the wrong ini file.

Contributed by STMicroelectronics

Change-Id: I53e231eb6b4958e9da5b09fab3ab8b2fc0baafbb
Signed-off-by: Torbjörn Svensson <torbjorn.svensson@st.com>
diff --git a/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/internal/p2/publisher/eclipse/BrandingIron.java b/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/internal/p2/publisher/eclipse/BrandingIron.java
index b1114b4..a6c14f1 100644
--- a/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/internal/p2/publisher/eclipse/BrandingIron.java
+++ b/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/internal/p2/publisher/eclipse/BrandingIron.java
@@ -315,9 +315,11 @@
 		descriptor.addFile(targetLauncher);
 	}
 
-	private File findLauncher(File root) {
-		for (String launcherName : new String[] { "launcher", "eclipse" }) { //$NON-NLS-1$ //$NON-NLS-2$
-			File launcher = new File(root, launcherName);
+	private File findLauncher(File root, String... candidates) {
+		if (candidates.length == 0)
+			candidates = new String[] { "launcher", "eclipse" }; //$NON-NLS-1$ //$NON-NLS-2$
+		for (String candidate : candidates) {
+			File launcher = new File(root, candidate);
 			if (launcher.exists())
 				return launcher;
 		}
@@ -359,26 +361,38 @@
 
 	private void brandWindows(ExecutablesDescriptor descriptor) throws Exception {
 		File root = descriptor.getLocation();
-		File templateLauncher = new File(root, name + ".exe"); //$NON-NLS-1$
-		if (!templateLauncher.exists())
-			templateLauncher = new File(root, "launcher.exe"); //$NON-NLS-1$
-		if (!templateLauncher.exists())
-			templateLauncher = new File(root, "eclipse.exe"); //$NON-NLS-1$
-		if (brandIcons) {
-			if (templateLauncher.exists()) {
+		String binary = name + ".exe"; //$NON-NLS-1$
+		File templateLauncher = findLauncher(root, binary, "launcher.exe", "eclipse.exe"); //$NON-NLS-1$ //$NON-NLS-2$
+		if (templateLauncher != null) {
+			if (brandIcons) {
 				String[] args = new String[icons.length + 1];
 				args[0] = templateLauncher.getAbsolutePath();
 				System.arraycopy(icons, 0, args, 1, icons.length);
 				IconExe.main(args);
-			} else {
-				LogHelper.log(new Status(IStatus.ERROR, Activator.ID, "Could not find executable to brand", null)); //$NON-NLS-1$
 			}
+
+			if (!templateLauncher.getName().equals(binary)) {
+				File targetLauncher = new File(root, binary);
+				templateLauncher.renameTo(targetLauncher);
+				descriptor.replace(templateLauncher, targetLauncher);
+			}
+		} else {
+			LogHelper.log(new Status(IStatus.ERROR, Activator.ID, "Could not find executable to brand", null)); //$NON-NLS-1$
 		}
-		File targetLauncher = new File(root, name + ".exe"); //$NON-NLS-1$
-		if (templateLauncher.exists() && !templateLauncher.getName().equals(name + ".exe")) { //$NON-NLS-1$
-			templateLauncher.renameTo(targetLauncher);
-			descriptor.replace(templateLauncher, targetLauncher);
+
+		// Handle console application
+		binary = name + "c.exe"; //$NON-NLS-1$
+		templateLauncher = findLauncher(root, binary, "launcherc.exe", "eclipsec.exe"); //$NON-NLS-1$ //$NON-NLS-2$
+		if (templateLauncher != null) {
+			if (!templateLauncher.getName().equals(binary)) {
+				File targetLauncher = new File(root, binary);
+				templateLauncher.renameTo(targetLauncher);
+				descriptor.replace(templateLauncher, targetLauncher);
+			}
+		} else {
+			LogHelper.log(new Status(IStatus.ERROR, Activator.ID, "Could not find console executable to brand", null)); //$NON-NLS-1$
 		}
+
 		descriptor.setExecutableName(name, true);
 	}
 
diff --git a/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ExecutablesDescriptor.java b/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ExecutablesDescriptor.java
index 353aab1..5b1c6f8 100644
--- a/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ExecutablesDescriptor.java
+++ b/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ExecutablesDescriptor.java
@@ -245,10 +245,6 @@
 	private void updateExecutableName(String newName) {
 		if (newName.equalsIgnoreCase(executableName))
 			return;
-		String targetIni = executableName + ".ini"; //$NON-NLS-1$
-		String targetExecutable = executableName;
-		String executableExtension = Constants.OS_WIN32.equals(os) ? ".exe" : ""; //$NON-NLS-1$ //$NON-NLS-2$
-		targetExecutable = executableName + executableExtension;
 		Set<File> filesCopy = new HashSet<>(files);
 		for (File file : filesCopy) {
 			String base = file.getParent();
@@ -257,9 +253,13 @@
 			base = base == null ? "" : base + "/"; //$NON-NLS-1$ //$NON-NLS-2$
 			if (Constants.OS_MACOSX.equals(os) && base.startsWith(executableName + ".app")) //$NON-NLS-1$
 				base = newName + ".app" + base.substring(executableName.length() + 4); //$NON-NLS-1$
-			if (file.getName().equalsIgnoreCase(targetExecutable))
-				replace(file, new File(base + newName + executableExtension));
-			else if (file.getName().equalsIgnoreCase(targetIni))
+			if (file.getName().equalsIgnoreCase(executableName))
+				replace(file, new File(base + newName));
+			else if (file.getName().equalsIgnoreCase(executableName + ".exe")) //$NON-NLS-1$
+				replace(file, new File(base + newName + ".exe")); //$NON-NLS-1$
+			else if (file.getName().equalsIgnoreCase(executableName + "c.exe")) //$NON-NLS-1$
+				replace(file, new File(base + newName + "c.exe")); //$NON-NLS-1$
+			else if (file.getName().equalsIgnoreCase(executableName + ".ini")) //$NON-NLS-1$
 				replace(file, new File(base + newName + ".ini")); //$NON-NLS-1$
 			else if (Constants.OS_MACOSX.equals(os))
 				replace(file, new File(base + file.getName()));
diff --git a/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxExecutableAction.java b/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxExecutableAction.java
index 0e8fa0d..e3ec91b 100644
--- a/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxExecutableAction.java
+++ b/bundles/org.eclipse.equinox.p2.publisher.eclipse/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxExecutableAction.java
@@ -292,6 +292,10 @@
 			File newFile = new File(file.getParentFile(), "eclipse.exe"); //$NON-NLS-1$
 			file.renameTo(newFile);
 			descriptor.replace(file, newFile);
+		} else if (file.getName().equals("launcherc.exe")) { //$NON-NLS-1$
+			File newFile = new File(file.getParentFile(), "eclipsec.exe"); //$NON-NLS-1$
+			file.renameTo(newFile);
+			descriptor.replace(file, newFile);
 		}
 	}
 }