/*-
 * ~~~~~~licensing~~~~~~
 * base-support
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.base.support.osgi;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.osgi.framework.Constants;

public class BundleUtils {

    public static File fixBundleWithUpdateLocation(InputStream is, String uri) throws IOException {
        File file = Files.createTempFile("update-", ".jar").toFile();
        try (ZipInputStream zis = new ZipInputStream(is);
             ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file))) {

            byte[] buf = new byte[8192];
            zos.setLevel(0);
            while (true) {
                ZipEntry entry = zis.getNextEntry();
                if (entry == null) {
                    break;
                }
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int n;
                while (-1 != (n = zis.read(buf))) {
                    baos.write(buf, 0, n);
                }
                if (entry.getName().equals(JarFile.MANIFEST_NAME)) {
                    Manifest man = new Manifest(new ByteArrayInputStream(baos.toByteArray()));
                    if (man.getMainAttributes().getValue(Constants.BUNDLE_UPDATELOCATION) == null) {
                        man.getMainAttributes().putValue(Constants.BUNDLE_UPDATELOCATION, uri);
                    }
                    baos.reset();
                    man.write(baos);
                }
                byte[] data = baos.toByteArray();
                CRC32 crc = new CRC32();
                crc.update(data);
                entry = new ZipEntry(entry.getName());
                entry.setSize(data.length);
                entry.setCrc(crc.getValue());
                zos.putNextEntry(entry);
                zos.write(data);
                zis.closeEntry();
                zos.closeEntry();
            }
        }
        return file;
    }

}
