package org.apache.maven.jnlp;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

/**
 * Updates manifest for correcting java webstart bug
 *
 * @author <a href="mailto:evenisse@ifrance.com">Emmanuel Venisse</a>
 * @version $Id$
 */
public class UpdateManifest
{
    private File inputJar;
    private File manifest;
    private File outputDir;

    public void process() throws Exception
    {
        System.out.println("Update Manifest in " + inputJar.getName());
        JarFile jarFile = new JarFile(inputJar);
        Enumeration entries = jarFile.entries();
        File outputJar = new File(outputDir, inputJar.getName());
        FileOutputStream fos = new FileOutputStream(outputJar);
        JarOutputStream jos = new JarOutputStream(fos);
        while (entries.hasMoreElements()) {
            process(jarFile, jos, (JarEntry)entries.nextElement());
        }
        jos.close();
        jarFile.close();
        outputJar.setLastModified(inputJar.lastModified());
    }
    
    private void process(JarFile jarFile, JarOutputStream jos, JarEntry entry)
        throws Exception
    {
        InputStream is = jarFile.getInputStream(entry);
        JarEntry je = entry;
        if (entry.getName().equalsIgnoreCase("META-INF/MANIFEST.MF"))
        {
            je = new JarEntry("META-INF/MANIFEST.MF");
            is = new FileInputStream(manifest);
        }
        JarEntry destEntry = new JarEntry (je.getName()); 
        jos.putNextEntry(destEntry); 
        
        byte[] buffer = new byte[2048];
        int read = 0;
        while((read = is.read(buffer)) > 0)
        {
            jos.write(buffer, 0, read);
        }
        jos.closeEntry();
    }
    
    public void setInputJar(File input)
    {
        this.inputJar = input;
    }
    
    public File getInputJar()
    {
        return inputJar;
    }
    
    public void setManifest(File manifest)
    {
        this.manifest = manifest;
    }
    
    public File getManifest()
    {
        return manifest;
    }
    
    public void setOutputDir(File outputDir)
    {
        this.outputDir = outputDir;
    }
    
    public File getOutputDir()
    {
        return outputDir;
    }
}