/*
    WinUninstallMenu.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. Move the WinUninstallMenu.class file into the JExpressInstaller 
	    subdirectory. Then add WinUninstallMenu to the "Customs" panel "After install". 
     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-2008 DeNova
    All rights reserved worldwide.
*/


import javax.swing.JLabel;
import java.io.File;
import java.awt.BorderLayout;
import com.denova.io.Log;
import com.denova.runtime.WindowsConstants;
import com.denova.runtime.WindowsMenus;
import com.denova.runtime.WindowsRegistry;
import com.denova.runtime.WindowsUtils;
import com.denova.ui.Fonts;
import com.denova.ui.WizardPanel;
import com.denova.util.PropertyList;
import com.denova.JExpress.Installer.CustomUninstaller;
import com.denova.JExpress.Installer.InstallPropertyNames;



public class WinUninstallMenu extends WizardPanel
                              implements InstallPropertyNames {

    public WinUninstallMenu ( PropertyList properties) {

        // initialize WizardPanel superclass
        super ( properties);

        // let them know what's happening
        setLayout ( new BorderLayout () );
        JLabel notice = new JLabel("Creating menu item for the uninstaller", JLabel.CENTER);
        notice.setFont(Fonts.Bold);
        add(notice);
    }

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

        try {

            // if we're on Windows, add the environment variable to autoexec.bat
            if ( isWindows() ) {
                createMenuItem ();
            }

        }
        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 createMenuItem ()
    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. getData ( PrimaryKey + " " + version, "UninstallString" );
            if ( uninstallCommand == null ) {
                uninstallCommand = WindowsRegistry. getData ( PrimaryKey, "UninstallString" );
            }

            if ( uninstallCommand != null &&
                 uninstallCommand. length () > 0 ) {

				String commandArgs = "";
				int index = -1;
				
				// if the command starts with "", find the ending quote
				if ( uninstallCommand. charAt ( 0 ) == '"' ) {
					
					String innerString = uninstallCommand. substring ( 1 );
					index = innerString. indexOf ( "\"" ) + 1;
				}
				
				else {
					index = uninstallCommand. indexOf ( " " );
				}
				
				if ( index > 0 ) {
					commandArgs = uninstallCommand. substring ( index );
					uninstallCommand = uninstallCommand. substring ( 0, index );
				}
				
				// split the command into the directory and the executable file
				File f = new File ( uninstallCommand );
				String execCommand = f. getName ();
				String workingDir = f. getParent ();
				
				String menuName = "Uninstall " + productName;
				String windowsStyle = "";
				String iconFilename = "";				
				
				debug ( "uninstallCommand " + uninstallCommand );
				debug ( "commandArgs " + commandArgs );
				debug ( "workingDir " + workingDir );
				debug ( "execCommand " + execCommand );
				
                WindowsMenus windowsMenus = new WindowsMenus();
                windowsMenus. addMenuItem ( submenu,
        	                         menuName,
        	                         workingDir,
                                     execCommand,
                                     commandArgs,
                                     windowsStyle,
                                     iconFilename );
				
                // 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 );
            }
        }
    }

    // don't let the user bypass this command before we're done
    public boolean isNextButtonEnabled () {
        return false;
    }

    // don't let the user back up before we're done
    public boolean isPreviousButtonEnabled () {
        return false;
    }

    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 ( "winuninstall" );
            uninstallLog. setLogging ( true );
        }
    }

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

    static Log uninstallLog;
    static final boolean debugging = false;

}  // WinUninstallMenu

