package io.xpipe.app.core.check;

import io.xpipe.app.core.AppInstallation;
import io.xpipe.app.core.AppNames;
import io.xpipe.app.process.LocalShell;
import io.xpipe.app.process.ProcessOutputException;
import io.xpipe.app.update.AppDistributionType;
import io.xpipe.app.util.OsType;

public class AppTestCommandCheck {

    public static void check() throws Exception {
        if (OsType.ofLocal() == OsType.WINDOWS) {
            return;
        }

        if (AppDistributionType.get() == AppDistributionType.DEVELOPMENT) {
            return;
        }

        try (var sc = LocalShell.getShell().start()) {
            try {
                sc.getShellDialect()
                        .directoryExists(
                                sc,
                                AppInstallation.ofCurrent()
                                        .getBaseInstallationPath()
                                        .toString())
                        .execute();
            } catch (ProcessOutputException ex) {
                throw ProcessOutputException.withPrefix(
                        "Installation self test failed. Is your \"test\" shell command working as expected and is the "
                                + AppNames.ofCurrent().getName() + " installation directory " + "accessible?",
                        ex);
            }
        }
    }
}
