/*
    UninstallMenu.java
    Create a menu item for the uninstaller

    Notes: 
     1. You can probably use this class as is, but be sure to review the
	    code to insure it's doing what you expect.
     2. Add UninstallMenu to the "Customs" | "Installer" tab 
       "After uninstaller created". Move the UninstallerMenu class
       to whatever directory you define for custom installer classes.
     3. You are welcome to adapt this to meet your needs
        as long as you only use it with the JExpress product
 
    Copyright (C) 1998-2012 DeNova.com
*/


import com.denova.io.Log;
import com.denova.runtime.Exec;
import com.denova.runtime.UnixMenu;
import com.denova.runtime.WindowsConstants;
import com.denova.runtime.WindowsMenus;
import com.denova.runtime.WindowsRegistry;
import com.denova.runtime.WindowsUtils;
import com.denova.util.PropertyList;
import com.denova.JExpress.Installer.CustomUninstaller;
import com.denova.JExpress.Installer.InstallPropertyNames;
import com.denova.JExpress.Installer.StatusPanel;
import java.io.File;
import java.util.List;



public class UninstallMenu extends StatusPanel
                           implements InstallPropertyNames {

    public UninstallMenu(PropertyList properties)
    {
        super(properties);
    }

    /** 
     * Enter the panel. 
     *
     * Do not call this method; only the wizard should call it.
     */
     public synchronized void enter()
     {
        // we don't actually want to interact with the user

        try {
            updateProgressBarLabel("Creating menu item for the uninstaller");

            if (isWindows()) {
                createWindowsMenu();
            }
            
            else if (!isMacOsX())
            {
                createUnixMenu();
            }

        }
        catch(Exception e) {

            // report that something went wrong
            debug("exception while creating uninstaller menu");
            log(e);
        }

        // we don't want any user interaction
        // so move them to the next panel automatically
        showNextPanel ();
    }

    
    private void createUnixMenu()
    throws Exception
    {
        if (UnixMenu.kdeMenusInstalled() ||
            UnixMenu.gnomeMenusInstalled())
        {
            String submenu = getPropertyList().getProperty(Submenu, "");
            String installDir = getPropertyList().getProperty(ApplicationDirectory, "");
            String productName = getPropertyList().getProperty(PackageName, "");
                    
            UnixMenu unixMenu = new UnixMenu(uninstallLog);
            unixMenu.setSubmenu(submenu);
            unixMenu.setInstallDir(installDir);
            unixMenu.setWorkingDir(installDir);
            unixMenu.setLaunchDirName(installDir);
            unixMenu.setLongName("Uninstall " + productName);
            unixMenu.setShortName("uninstall");
            
            if (UnixMenu.kdeMenusInstalled())
            {
                unixMenu.createKdeMenu("uninstall", false);
            }
            
            if (UnixMenu.gnomeMenusInstalled())
            {
                unixMenu.createGnomeMenu("uninstall", false);
            }
        }
    }
    
    
    private void createWindowsMenu()
    throws Exception {

        // clear any errors
        WindowsUtils.clearError();

        String productName = getPropertyList().getProperty(PackageName, "");
        String submenu = getPropertyList().getProperty(Submenu, "");
        String version = getPropertyList().getProperty(PackageVersion, "");

        if (productName. length() > 0)
        {
            boolean ok;

            final String PrimaryKey = 
			 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + productName;

            String uninstallCommand = WindowsRegistry.getStringData(PrimaryKey + " " + version, "UninstallString");
            if (uninstallCommand == null) {
                uninstallCommand = WindowsRegistry.getStringData(PrimaryKey, "UninstallString");
            }

            if (uninstallCommand == null ||
                uninstallCommand. length () <= 0)
            {
                log("unable to get uninstall command from " + PrimaryKey);
            }
            else
            {
            	String exec = "";
		String commandArgs = "";
		
		// if the command starts with "", find the ending quote
		List commands = Exec.getCommandArgs(uninstallCommand);
		int index = 0;
		if (index < commands.size())
		{
		    exec = (String)commands.get(index++);
		    while (index < commands.size())
		    {
		    	String command = Exec.quoteArg((String)commands.get(index++));
		        commandArgs += command;
		        commandArgs += " ";
		    }
		    commandArgs = commandArgs.trim();
		}
		
		// split the command into the directory and the executable file
		File f = new File(exec);
		String execCommand = f.getPath();
		String workingDir = f.getParent();
		
		String menuName = "Uninstall " + productName;
		String windowsStyle = "";
		String iconFilename = WindowsRegistry.getStringData(PrimaryKey, "DisplayIcon");
		if (iconFilename == null)
		{
		    iconFilename = "";
		}
		
		debug("menuName " + menuName);
		debug("exec " + exec);
		debug("commandArgs " + commandArgs);
		debug("workingDir " + workingDir);
		debug("execCommand " + execCommand);
		debug("iconFilename " + iconFilename);
				
                WindowsMenus windowsMenus = new WindowsMenus();
                ok = windowsMenus.addMenuItem(submenu,
        	                         menuName,
        	                         workingDir,
                                     execCommand,
                                     commandArgs,
                                     windowsStyle,
                                     iconFilename);
                debug("added uninstall menu: " + ok);
				
                // remove this menu item during the uninstall
                String applicationDirectory = getPropertyList (). getProperty(ApplicationDirectory, "");
                CustomUninstaller customUninstaller = new CustomUninstaller ();
                customUninstaller.deleteWindowsMenuItem(submenu,
                                                        menuName,
                                                        WindowsConstants. CurrentUser);

                customUninstaller.append(applicationDirectory);

            }
        }
    }

    void debug(String s) {
        if (debugging) {
            log (s);
        }
    }

    static private void log(String s) {
        startLog ();
        uninstallLog. write(s);
    }

    static private void log(Exception e) {
        startLog ();
        uninstallLog. setLogging(true);
        uninstallLog. write(e);
    }

    static private void startLog () {
        if (uninstallLog == null) {
            uninstallLog = new Log("uninstallmenu");
        }
    }

    static private void stopLogging () {
        if (uninstallLog != null) {
            uninstallLog. stopLogging ();
        }
    }

    static Log uninstallLog;
    static final boolean debugging = true;

}  // UninstallMenu