/*
 * Copyright 2017 Volker Berlin (i-net software)
 *
 * 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.
 */
package de.inetsoftware.jwebassembly;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Download the JavaScript engine SpiderMonkey.
 * 
 * @author Volker Berlin
 */
public class SpiderMonkey {

    private String command;

    /**
     * Check if there is a new version of the script engine
     * 
     * @throws IOException
     *             if any error occur
     */
    private void download() throws IOException {
        boolean is32 = "32".equals( System.getProperty( "sun.arch.data.model" ) );
        String fileName;
        final String os = System.getProperty( "os.name", "" ).toLowerCase();
        if( os.contains( "windows" ) ) {
            fileName = is32 ? "win32" : "win64";
        } else if( os.contains( "mac" ) ) {
            fileName = is32 ? "mac" : "mac64";
        } else if( os.contains( "linux" ) ) {
            fileName = is32 ? "linux-i686" : "linux-x86_64";
        } else {
            throw new IllegalStateException( "Unknown OS: " + os );
        }
        File target = new File( System.getProperty( "java.io.tmpdir" ) + "/SpiderMonkey" );
        URL url = new URL( "https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/jsshell-" + fileName
                        + ".zip" );
        System.out.println( "\tDownload: " + url );
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        if( target.exists() ) {
            System.out.println( "\tUP-TP-DATE" );
            conn.setIfModifiedSince( target.lastModified() );
        }
        InputStream input = conn.getInputStream();
        command = target.getAbsolutePath() + "/js";
        if( conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED ) {
            return;
        }
        ZipInputStream zip = new ZipInputStream( input );
        long lastModfied = conn.getLastModified();
        do {
            ZipEntry entry = zip.getNextEntry();
            if( entry == null ) {
                break;
            }
            if( entry.isDirectory() ) {
                continue;
            }
            File file = new File( target, entry.getName() );
            file.getParentFile().mkdirs();

            Files.copy( zip, file.toPath(), StandardCopyOption.REPLACE_EXISTING );
            file.setLastModified( entry.getTime() );
            if( "js".equals( file.getName() ) ) {
                file.setExecutable( true );
            }
        } while( true );
        target.setLastModified( lastModfied );
    }

    public String getCommand() throws IOException {
        if( command == null ) {
            download();
        }
        return command;
    }

}

@@ -845,24 +845,50 @@
 CopyOption;%0A
+import java.time.Instant;%0A
 import java.
@@ -2263,58 +2263,8 @@
 ) %7B%0A
-            System.out.println( %22%5CtUP-TP-DATE%22 );%0A
     
@@ -2508,24 +2508,141 @@
 ODIFIED ) %7B%0A
+            System.out.println( %22%5CtUP-TP-DATE, use version from %22 + Instant.ofEpochMilli( target.lastModified() ) );%0A
             
@@ -3398,24 +3398,115 @@
 tModfied );%0A
+        System.out.println( %22%5CtUse Version from %22 + Instant.ofEpochMilli( lastModfied ) );%0A
     %7D%0A%0A    p

improve debug output