/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.util.ConfigFactory
 *  com.l7tech.util.IOUtils
 *  com.l7tech.util.ResourceUtils
 *  com.l7tech.util.ShutdownExceptionHandler
 *  com.l7tech.util.UptimeMetrics
 */
package com.l7tech.server.util;

import com.l7tech.server.ServerConfig;
import com.l7tech.server.util.s;
import com.l7tech.server.util.t;
import com.l7tech.util.ConfigFactory;
import com.l7tech.util.IOUtils;
import com.l7tech.util.ResourceUtils;
import com.l7tech.util.ShutdownExceptionHandler;
import com.l7tech.util.UptimeMetrics;
import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UptimeMonitor {
    private static final Logger a;
    private static final int b = 30000;
    private static final String[] c;
    private static String d;
    private Thread e = null;
    private UptimeMetrics f = null;
    private static final String[] g;

    private static UptimeMonitor a() {
        return t.a;
    }

    private UptimeMonitor() {
        this.b();
    }

    private void b() {
        try {
            this.f = UptimeMonitor.a(c);
            if (d != null) {
                this.e = new Thread((Runnable)new s(this), g[4]);
                this.e.setDaemon(true);
                this.e.start();
                this.e.setUncaughtExceptionHandler((Thread.UncaughtExceptionHandler)ShutdownExceptionHandler.getInstance());
            }
        }
        catch (IllegalStateException illegalStateException) {
            throw UptimeMonitor.a(illegalStateException);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     */
    private static UptimeMetrics a(String[] stringArray) {
        for (int i2 = 0; i2 < stringArray.length; ++i2) {
            String string = stringArray[i2];
            Process process = null;
            UptimeMetrics uptimeMetrics = UptimeMonitor.a(string);
            process = Runtime.getRuntime().exec(string);
            process.waitFor();
            d = string;
            String[] stringArray2 = g;
            a.info(stringArray2[8] + d);
            UptimeMetrics uptimeMetrics2 = uptimeMetrics;
            try {
                if (process != null) {
                    process.destroy();
                }
            }
            catch (IOException iOException) {
                throw UptimeMonitor.a(iOException);
            }
            return uptimeMetrics2;
            catch (IOException iOException) {
                try {
                    if (process == null) continue;
                    process.destroy();
                    continue;
                }
                catch (IOException iOException2) {
                    throw UptimeMonitor.a(iOException2);
                }
            }
            catch (InterruptedException interruptedException) {
                Thread.currentThread().interrupt();
                UptimeMetrics uptimeMetrics3 = null;
                {
                    catch (Throwable throwable) {
                        try {
                            if (process != null) {
                                process.destroy();
                            }
                        }
                        catch (IOException iOException) {
                            throw UptimeMonitor.a(iOException);
                        }
                        throw throwable;
                    }
                }
                try {
                    if (process != null) {
                        process.destroy();
                    }
                }
                catch (IOException iOException) {
                    throw UptimeMonitor.a(iOException);
                }
                return uptimeMetrics3;
            }
        }
        a.warning(g[0]);
        return null;
    }

    public static UptimeMetrics getLastUptime() throws FileNotFoundException, IllegalStateException {
        return UptimeMonitor.a().c();
    }

    public static boolean isUptimeMetricsAvailable() {
        boolean bl2;
        try {
            bl2 = UptimeMonitor.a().e != null;
        }
        catch (IllegalStateException illegalStateException) {
            throw UptimeMonitor.a(illegalStateException);
        }
        return bl2;
    }

    private synchronized UptimeMetrics c() throws FileNotFoundException, IllegalStateException {
        try {
            if (d == null) {
                throw new FileNotFoundException(g[3]);
            }
        }
        catch (FileNotFoundException fileNotFoundException) {
            throw UptimeMonitor.a(fileNotFoundException);
        }
        try {
            if (this.e == null) {
                throw new IllegalStateException(g[13]);
            }
        }
        catch (FileNotFoundException fileNotFoundException) {
            throw UptimeMonitor.a(fileNotFoundException);
        }
        return this.f;
    }

    public static void shutdownMonitorThread() {
        UptimeMonitor.a().d();
    }

    private synchronized void d() {
        try {
            if (this.e != null) {
                this.e.interrupt();
                this.e = null;
            }
        }
        catch (IllegalStateException illegalStateException) {
            throw UptimeMonitor.a(illegalStateException);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private static UptimeMetrics a(String string) throws IOException, InterruptedException {
        UptimeMetrics uptimeMetrics;
        Process process = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            process = Runtime.getRuntime().exec(string);
            bufferedInputStream = new BufferedInputStream(process.getInputStream());
            try {
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedException(g[9]);
                }
            }
            catch (IOException iOException) {
                throw UptimeMonitor.a(iOException);
            }
            byte[] byArray = IOUtils.slurpStream((InputStream)bufferedInputStream);
            String string2 = new String(byArray);
            UptimeMetrics uptimeMetrics2 = new UptimeMetrics(string2, ServerConfig.getInstance().getServerBootTime());
            process.waitFor();
            uptimeMetrics = uptimeMetrics2;
        }
        catch (Throwable throwable) {
            try {
                ResourceUtils.closeQuietly(bufferedInputStream);
                if (process != null) {
                    process.destroy();
                }
            }
            catch (IOException iOException) {
                throw UptimeMonitor.a(iOException);
            }
            throw throwable;
        }
        try {
            ResourceUtils.closeQuietly((Closeable)bufferedInputStream);
            if (process != null) {
                process.destroy();
            }
        }
        catch (IOException iOException) {
            throw UptimeMonitor.a(iOException);
        }
        return uptimeMetrics;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void e() {
        String[] stringArray = g;
        a.info(stringArray[7]);
        long l2 = 0L;
        while (true) {
            try {
                UptimeMetrics uptimeMetrics = UptimeMonitor.a(d);
                UptimeMonitor uptimeMonitor = this;
                synchronized (uptimeMonitor) {
                    this.f = uptimeMetrics;
                }
                l2 = 0L;
            }
            catch (IOException iOException) {
                try {
                    if (l2 < 5L) {
                        a.log(Level.WARNING, g[14], iOException);
                    }
                }
                catch (IOException iOException2) {
                    throw UptimeMonitor.a(iOException2);
                }
                ++l2;
            }
            catch (InterruptedException interruptedException) {
                stringArray = g;
                a.info(stringArray[1]);
                return;
            }
            try {
                Thread.sleep(30000L);
            }
            catch (InterruptedException interruptedException) {
                a.info(g[11]);
                return;
            }
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[17];
                var3_1 = 0;
                var2_2 = "<f\u007f\u000bh5FXirEbzG\b{rFczW\u0000jx^r;P\u0014j;DhzF\u0010fh\u000bu#A\fjv&-\u007foBk?\u0012\u0015`uBr5@X{sYc;VXfh\u000bu2G\f{rEazV\u0017xu\u000bWmrE)/B\ffvN2,g~\u000bs*F\u0011b~\u000bc\"W\u001bzoJd6WXxzX&4]\f/}Ds4VX`u\u000br2[\u000b/hRr?_V\u000e-\u007foBk?\u00125`uBr5@\u0003\u001afu\u0007\u000b||ci7W!-\u007foBk?\u0012\u0015`uBr5@X{sYc;VXfh\u000bu.S\n{rEa\u0019-|rEazG\b{rFczW\u0000jx^r;P\u0014j!\u000b\u00181aoNt(G\b{~O&>G\nfuL&?J\u001dl5\u0014\u001b54H\u007f=E\u0011a4Io4\u001d\r\u007foBk?&-\u007foBk?\u0012\u0015`uBr5@X{sYc;VXfh\u000bu2G\f{rEazV\u0017xu\u0006\r\u007foBk?%,g~\u000bS*F\u0011b~fi4[\f`i\u000bn;AXm~NhzA\u0010zo\u000bb5E\u0016!6-azIj?\u0012\f`;Lc.\u0012\u000bvh_c7\u0012\r\u007foBk?\u0012\u001e}tF&.Z\u001d/n[r3_\u001d/~Sc9G\fnyGc";
                var4_3 = "<f\u007f\u000bh5FXirEbzG\b{rFczW\u0000jx^r;P\u0014j;DhzF\u0010fh\u000bu#A\fjv&-\u007foBk?\u0012\u0015`uBr5@X{sYc;VXfh\u000bu2G\f{rEazV\u0017xu\u000bWmrE)/B\ffvN2,g~\u000bs*F\u0011b~\u000bc\"W\u001bzoJd6WXxzX&4]\f/}Ds4VX`u\u000br2[\u000b/hRr?_V\u000e-\u007foBk?\u00125`uBr5@\u0003\u001afu\u0007\u000b||ci7W!-\u007foBk?\u0012\u0015`uBr5@X{sYc;VXfh\u000bu.S\n{rEa\u0019-|rEazG\b{rFczW\u0000jx^r;P\u0014j!\u000b\u00181aoNt(G\b{~O&>G\nfuL&?J\u001dl5\u0014\u001b54H\u007f=E\u0011a4Io4\u001d\r\u007foBk?&-\u007foBk?\u0012\u0015`uBr5@X{sYc;VXfh\u000bu2G\f{rEazV\u0017xu\u0006\r\u007foBk?%,g~\u000bS*F\u0011b~fi4[\f`i\u000bn;AXm~NhzA\u0010zo\u000bb5E\u0016!6-azIj?\u0012\f`;Lc.\u0012\u000bvh_c7\u0012\r\u007foBk?\u0012\u001e}tF&.Z\u001d/n[r3_\u001d/~Sc9G\fnyGc".length();
                var1_4 = 45;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 18;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = UptimeMonitor.a(v0, UptimeMonitor.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "v[Ix\b\u0019z7\u0001OzS\u0012~<\u0018:\u0014\u0015eW\u000f<:W]}N\u0015<;GT%R\u000bg0C_";
                    var4_3 = "v[Ix\b\u0019z7\u0001OzS\u0012~<\u0018:\u0014\u0015eW\u000f<:W]}N\u0015<;GT%R\u000bg0C_".length();
                    var1_4 = 15;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 51;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = UptimeMonitor.a(v0, UptimeMonitor.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        UptimeMonitor.g = var5;
        UptimeMonitor.a = Logger.getLogger(UptimeMonitor.class.getName());
        v5 = new String[5];
        var0_6 = UptimeMonitor.g;
        v5[0] = var0_6[15];
        v5[1] = var0_6[2];
        v5[2] = var0_6[10];
        v5[3] = var0_6[16];
        v5[4] = ConfigFactory.getProperty((String)var0_6[6], null) + File.separator + var0_6[5] + File.separator + var0_6[12];
        UptimeMonitor.c = v5;
        UptimeMonitor.d = null;
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x20);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 106;
                    case 1 -> 29;
                    case 2 -> 9;
                    case 3 -> 57;
                    case 4 -> 20;
                    case 5 -> 72;
                    default -> 32;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

