/*
    MoveFiles.java
    Move files from the installation directory to another directory
      -- currently configured to move all DLL files from the installation dir 
         to the Windows\System or WinNT\System dir

    Notes: 
     1. Currently, this class file moves all DLL files from the installation/application
        directory to the Windows's System subdirectory if the installer is being
        operated on a Windows OS.
     2. You must include the *.DLL files in the Files panel; the files must be
        in the root of the installation directory, not in a subdirectory
     3. You must include this class file "After uninstaller created" in the Customs panel
     4. If you want to adapt this class to move other files to a different 
        destination, then change the destinationDir and the files that you want moved
     5. You are welcome to adapt this to meet your needs as long as you 
        only use it with the JExpress product

    Copyright 1998-2010 DeNova
    
*/
import java.awt.BorderLayout;


import java.io.File;
import com.denova.runtime.WindowsDirs;
import com.denova.util.PropertyList;
import com.denova.JExpress.Installer.CustomInstaller;
import com.denova.JExpress.Installer.CustomUninstaller;
import com.denova.JExpress.Installer.InstallPropertyNames;
import com.denova.JExpress.Installer.StatusPanel;

public class MoveFiles extends StatusPanel
                       implements InstallPropertyNames {

    private String progressLabel = "Copying files";
    
    
    public MoveFiles ( PropertyList properties) {

        super (properties);
        
        updateProgressBarLabel(progressLabel);
    }

    /** 
     * Enter the panel. 
     *
     * Do not call this method; only the wizard should call it.
     */
     public synchronized void enter()
     {
        if (isWindows())
        {
            updateProgressBarLabel(progressLabel);
        
	        String winDir = WindowsDirs. getWindowsDirectory ();
            File appDir = new File ( getPropertyList (). getProperty ( ApplicationDirectory, "" ) );
            File destinationDir = new File ( winDir, "System" );
        
            // setup to add commands to the uninstaller to remove the files we move
            CustomUninstaller customUninstaller = new CustomUninstaller ();
            
            // look at all the files in the installation/application directory
            String [] filenames = appDir. list ();
            for ( int fileIndex = 0;
                  fileIndex < filenames. length;
                  ++ fileIndex ) {

                String filename = filenames [ fileIndex ];

                // only move the files that end with .DLL to the destination directory
                if ( filename. endsWith ( ".DLL" ) ||
                     filename. endsWith ( ".dll" ) ||
                     filename. endsWith ( ".Dll" ) ) {
            
                    File fromFile = new File ( appDir, filename );
                    File toFile = new File ( destinationDir, filename );
                                    
                    try {
                        com.denova.io.FileSystem. copyFile ( fromFile, toFile );

                        // remove the file when we delete the app
                        customUninstaller. deleteFile ( toFile. getPath () );
                    }
            
                    catch ( Exception e ) {
                        CustomInstaller. logException ( "Unable to copy files",
                                                        e );
                    }
                
                    // delete the file from the application directory
                    fromFile. delete ();
                }

                Thread. yield ();
            }
            
            customUninstaller. append ( appDir. getPath () );
        }

        showNextPanel ();
    }

}