// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.core;

import com.yahoo.io.IOUtils;
import com.yahoo.yolean.Exceptions;
import org.apache.felix.framework.Felix;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.jar.JarInputStream;
import java.util.logging.Logger;

/**
 * @author Simon Thoresen Hult
 * @author gjoranv
 */
public class ExportPackages {
    private static final Logger log = Logger.getLogger(ExportPackages.class.getName());

    public static final String PROPERTIES_FILE = "/exportPackages.properties";
    public static final String EXPORT_PACKAGES = "exportPackages";
    private static final String REPLACE_VERSION_PREFIX = "__REPLACE_VERSION__";

    public static void main(String[] args) throws IOException {
        String fileName = args[0];
        if (!fileName.endsWith(PROPERTIES_FILE)) {
            throw new IllegalArgumentException("Expected '" + PROPERTIES_FILE + "', got '" + fileName + "'.");
        }
        String exportPackages = getExportPackages(args);
        Properties props = new Properties();
        props.setProperty(EXPORT_PACKAGES, exportPackages);

        try (FileWriter writer = new FileWriter(new File(fileName))) {
            props.store(writer, "generated by " + ExportPackages.class.getName());
        }
    }

    private static String getExportPackages(String[] jars) throws IOException {
        StringBuilder out = new StringBuilder();
        out.append(getSystemPackages()).append(", ")
           .append("com.yahoo.jdisc, ")
           .append("com.yahoo.jdisc.application, ")
           .append("com.yahoo.jdisc.handler, ")
           .append("com.yahoo.jdisc.service, ")
           .append("com.yahoo.jdisc.statistics, ")
           .append("javax.inject;version=1.0.0, ")  // Included in guice, but not exported. Needed by container-jersey.
           .append("org.aopalliance.intercept, ")
           .append("org.aopalliance.aop");

        for (int i = 1; i < jars.length; ++i) {
            out.append(", ").append(getExportedPackages(jars[i]));
        }

        //TODO: temporary additions for backwards compatibility with Vespa 6. Remove when all apps have been built with 7
        out.append(", ")
                .append("javax.annotation, ")
                .append("javax.activation, ")
                .append("javax.xml.bind.annotation.adapters, ")
                .append("javax.xml.bind.annotation, ")
                .append("javax.xml.bind.attachment, ")
                .append("javax.xml.bind.helpers, ")
                .append("javax.xml.bind.util, ")
                .append("javax.xml.bind");

        return out.toString();
    }

    public static String readExportProperty() {
        Properties props = new Properties();
        try {
            props.load(ExportPackages.class.getResourceAsStream(PROPERTIES_FILE));
        } catch (IOException e) {
            throw new IllegalStateException("Failed to read resource '" + PROPERTIES_FILE + "'.");
        }
        return props.getProperty(EXPORT_PACKAGES);
    }

    public static String getSystemPackages() {
        File cache;
        try {
            cache = Files.createTempDirectory("felix-cache").toAbsolutePath().toFile();
        } catch (IOException e) {
            throw new RuntimeException("Could not create temp bundle-cache.", e);
        }
        Framework framework = new Felix(felixCacheParams(cache.getAbsolutePath()));
        try {
            framework.init();
            framework.start();
            return framework.getHeaders().get(Constants.EXPORT_PACKAGE);
        } catch (BundleException e) {
            throw new RuntimeException("Failed retrieving exported system packages. ", e);
        } finally {
            try {
                framework.stop();
                framework.waitForStop(10000);
            } catch (BundleException | InterruptedException e) {
                log.warning("Failed to stop Felix framework:\n" + Exceptions.toMessageString(e));
            }
            if (! IOUtils.recursiveDeleteDir(cache)) {
                log.warning("Failed to delete temp dir, must be deleted manually: " + cache.getAbsolutePath());
            }
        }
    }

    private static Map<String, Object> felixCacheParams(String cache) {
        Map<String, Object> params = new HashMap<>();
        params.put("felix.cache.profiledir", cache);
        params.put("felix.cache.dir", cache);
        params.put(Constants.FRAMEWORK_STORAGE, cache);
        return params;
    }

    private static String getExportedPackages(String argument) throws IOException {
        if (argument.startsWith(REPLACE_VERSION_PREFIX)) {
            String jarFile = argument.substring(REPLACE_VERSION_PREFIX.length());
            return readExportHeader(jarFile);
        } else {
            return readExportHeader(argument);
        }
    }

    private static String readExportHeader(String jarFile) throws IOException {
        try (JarInputStream jar = new JarInputStream(new FileInputStream(jarFile))) {
            return jar.getManifest().getMainAttributes().getValue(Constants.EXPORT_PACKAGE);
        }
    }

}

@@ -2431,567 +2431,8 @@
   %7D%0A
-%0A        //TODO: temporary additions for backwards compatibility with Vespa 6. Remove when all apps have been built with 7%0A        out.append(%22, %22)%0A                .append(%22javax.annotation, %22)%0A                .append(%22javax.activation, %22)%0A                .append(%22javax.xml.bind.annotation.adapters, %22)%0A                .append(%22javax.xml.bind.annotation, %22)%0A                .append(%22javax.xml.bind.attachment, %22)%0A                .append(%22javax.xml.bind.helpers, %22)%0A                .append(%22javax.xml.bind.util, %22)%0A                .append(%22javax.xml.bind%22);%0A%0A
     

Remove Export-Package's added to support Vespa 6 bundles on 7.