// 
// Decompiled by Procyon v0.6.0
// 

package io.sentry.internal;

import java.util.jar.Attributes;
import java.util.Enumeration;
import java.io.IOException;
import io.sentry.SentryIntegrationPackageStorage;
import java.util.jar.Manifest;
import java.net.URL;
import io.sentry.ISentryLifecycleToken;
import org.jetbrains.annotations.NotNull;
import io.sentry.util.AutoClosableReentrantLock;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class ManifestVersionReader
{
    @Nullable
    private static volatile ManifestVersionReader INSTANCE;
    @NotNull
    private static final AutoClosableReentrantLock staticLock;
    private volatile boolean hasManifestBeenRead;
    @NotNull
    private final VersionInfoHolder versionInfo;
    @NotNull
    private AutoClosableReentrantLock lock;
    
    @NotNull
    public static ManifestVersionReader getInstance() {
        if (ManifestVersionReader.INSTANCE == null) {
            try (final ISentryLifecycleToken ignored = ManifestVersionReader.staticLock.acquire()) {
                if (ManifestVersionReader.INSTANCE == null) {
                    ManifestVersionReader.INSTANCE = new ManifestVersionReader();
                }
            }
        }
        return ManifestVersionReader.INSTANCE;
    }
    
    private ManifestVersionReader() {
        this.hasManifestBeenRead = false;
        this.versionInfo = new VersionInfoHolder();
        this.lock = new AutoClosableReentrantLock();
    }
    
    @Nullable
    public VersionInfoHolder readOpenTelemetryVersion() {
        this.readManifestFiles();
        if (this.versionInfo.sdkVersion == null) {
            return null;
        }
        return this.versionInfo;
    }
    
    public void readManifestFiles() {
        if (this.hasManifestBeenRead) {
            return;
        }
        try (final ISentryLifecycleToken ignored = this.lock.acquire()) {
            if (this.hasManifestBeenRead) {
                if (ignored != null) {
                    ignored.close();
                }
                return;
            }
            final Enumeration<URL> resources = ClassLoader.getSystemClassLoader().getResources("META-INF/MANIFEST.MF");
            while (resources.hasMoreElements()) {
                try {
                    final Manifest manifest = new Manifest(resources.nextElement().openStream());
                    final Attributes mainAttributes = manifest.getMainAttributes();
                    if (mainAttributes == null) {
                        continue;
                    }
                    final String name = mainAttributes.getValue("Sentry-Opentelemetry-SDK-Name");
                    final String version = mainAttributes.getValue("Implementation-Version");
                    final String sdkName = mainAttributes.getValue("Sentry-SDK-Name");
                    final String packageName = mainAttributes.getValue("Sentry-SDK-Package-Name");
                    if (name != null && version != null) {
                        this.versionInfo.sdkName = name;
                        this.versionInfo.sdkVersion = version;
                        final String otelVersion = mainAttributes.getValue("Sentry-Opentelemetry-Version-Name");
                        if (otelVersion != null) {
                            SentryIntegrationPackageStorage.getInstance().addPackage("maven:io.opentelemetry:opentelemetry-sdk", otelVersion);
                            SentryIntegrationPackageStorage.getInstance().addIntegration("OpenTelemetry");
                        }
                        final String otelJavaagentVersion = mainAttributes.getValue("Sentry-Opentelemetry-Javaagent-Version-Name");
                        if (otelJavaagentVersion != null) {
                            SentryIntegrationPackageStorage.getInstance().addPackage("maven:io.opentelemetry.javaagent:opentelemetry-javaagent", otelJavaagentVersion);
                            SentryIntegrationPackageStorage.getInstance().addIntegration("OpenTelemetry-Agent");
                        }
                        if (name.equals("sentry.java.opentelemetry.agentless")) {
                            SentryIntegrationPackageStorage.getInstance().addIntegration("OpenTelemetry-Agentless");
                        }
                        if (name.equals("sentry.java.opentelemetry.agentless-spring")) {
                            SentryIntegrationPackageStorage.getInstance().addIntegration("OpenTelemetry-Agentless-Spring");
                        }
                    }
                    if (sdkName == null || version == null || packageName == null || !sdkName.startsWith("sentry.java")) {
                        continue;
                    }
                    SentryIntegrationPackageStorage.getInstance().addPackage(packageName, version);
                }
                catch (final Exception ex) {}
            }
        }
        catch (final IOException ex2) {}
        finally {
            this.hasManifestBeenRead = true;
        }
    }
    
    static {
        staticLock = new AutoClosableReentrantLock();
    }
    
    public static final class VersionInfoHolder
    {
        @Nullable
        private volatile String sdkName;
        @Nullable
        private volatile String sdkVersion;
        
        @Nullable
        public String getSdkName() {
            return this.sdkName;
        }
        
        @Nullable
        public String getSdkVersion() {
            return this.sdkVersion;
        }
    }
}
