package com.eclipsesource.v8;

import com.eclipsesource.v8.PlatformDetector.OS;
import com.tencent.matrix.trace.core.AppMethodBeat;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

public class PlatformDetector$Vendor {
    private static final String LINUX_ID_PREFIX = "ID=";
    private static final String[] LINUX_OS_RELEASE_FILES = new String[]{"/etc/os-release", "/usr/lib/os-release"};
    private static final String REDHAT_RELEASE_FILE = "/etc/redhat-release";

    public static String getName() {
        AppMethodBeat.i(75013);
        String str;
        if (OS.isWindows()) {
            str = "microsoft";
            AppMethodBeat.o(75013);
            return str;
        } else if (OS.isMac()) {
            str = "apple";
            AppMethodBeat.o(75013);
            return str;
        } else if (OS.isLinux()) {
            str = getLinuxOsReleaseId();
            AppMethodBeat.o(75013);
            return str;
        } else if (OS.isAndroid()) {
            str = "google";
            AppMethodBeat.o(75013);
            return str;
        } else {
            UnsatisfiedLinkError unsatisfiedLinkError = new UnsatisfiedLinkError("Unsupported vendor: " + getName());
            AppMethodBeat.o(75013);
            throw unsatisfiedLinkError;
        }
    }

    private static String getLinuxOsReleaseId() {
        String parseLinuxOsReleaseFile;
        AppMethodBeat.i(75014);
        for (String file : LINUX_OS_RELEASE_FILES) {
            File file2 = new File(file);
            if (file2.exists()) {
                parseLinuxOsReleaseFile = parseLinuxOsReleaseFile(file2);
                AppMethodBeat.o(75014);
                return parseLinuxOsReleaseFile;
            }
        }
        File file3 = new File(REDHAT_RELEASE_FILE);
        if (file3.exists()) {
            parseLinuxOsReleaseFile = parseLinuxRedhatReleaseFile(file3);
            AppMethodBeat.o(75014);
            return parseLinuxOsReleaseFile;
        }
        UnsatisfiedLinkError unsatisfiedLinkError = new UnsatisfiedLinkError("Unsupported linux vendor: " + getName());
        AppMethodBeat.o(75014);
        throw unsatisfiedLinkError;
    }

    private static String parseLinuxOsReleaseFile(File file) {
        Throwable th;
        Closeable closeable;
        String str = null;
        AppMethodBeat.i(75015);
        Closeable bufferedReader;
        try {
            String readLine;
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            do {
                try {
                    readLine = bufferedReader.readLine();
                    if (readLine == null) {
                        break;
                    }
                } catch (IOException e) {
                    closeQuietly(bufferedReader);
                    AppMethodBeat.o(75015);
                    return str;
                } catch (Throwable th2) {
                    th = th2;
                    closeable = bufferedReader;
                    closeQuietly(closeable);
                    AppMethodBeat.o(75015);
                    throw th;
                }
            } while (!readLine.startsWith(LINUX_ID_PREFIX));
            str = PlatformDetector.access$300(readLine.substring(3));
            closeQuietly(bufferedReader);
            AppMethodBeat.o(75015);
        } catch (IOException e2) {
            bufferedReader = null;
            closeQuietly(bufferedReader);
            AppMethodBeat.o(75015);
            return str;
        } catch (Throwable th3) {
            th = th3;
            closeable = null;
            closeQuietly(closeable);
            AppMethodBeat.o(75015);
            throw th;
        }
        return str;
    }

    private static String parseLinuxRedhatReleaseFile(File file) {
        Throwable th;
        Closeable closeable;
        String str = null;
        AppMethodBeat.i(75016);
        Closeable bufferedReader;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            try {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    readLine = readLine.toLowerCase(Locale.US);
                    if (readLine.contains("centos")) {
                        str = "centos";
                    } else if (readLine.contains("fedora")) {
                        str = "fedora";
                    } else if (readLine.contains("red hat enterprise linux")) {
                        str = "rhel";
                    } else {
                        closeQuietly(bufferedReader);
                        AppMethodBeat.o(75016);
                        return str;
                    }
                    closeQuietly(bufferedReader);
                    AppMethodBeat.o(75016);
                    return str;
                }
                closeQuietly(bufferedReader);
                AppMethodBeat.o(75016);
                return str;
            } catch (IOException e) {
                closeQuietly(bufferedReader);
                AppMethodBeat.o(75016);
                return str;
            } catch (Throwable th2) {
                th = th2;
                closeable = bufferedReader;
                closeQuietly(closeable);
                AppMethodBeat.o(75016);
                throw th;
            }
        } catch (IOException e2) {
            bufferedReader = null;
            closeQuietly(bufferedReader);
            AppMethodBeat.o(75016);
            return str;
        } catch (Throwable th3) {
            th = th3;
            closeable = null;
            closeQuietly(closeable);
            AppMethodBeat.o(75016);
            throw th;
        }
    }

    private static void closeQuietly(Closeable closeable) {
        AppMethodBeat.i(75017);
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                AppMethodBeat.o(75017);
                return;
            }
        }
        AppMethodBeat.o(75017);
    }
}
