using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Lentia.Utils;

public static class BashUtils {
    public static string GetBashCommandOutput(string command) {
        using var proc = new Process {
            StartInfo = new ProcessStartInfo {
                FileName = "/bin/bash",
                Arguments = $"-c \"{command}\"",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            }
        };
        proc.Start();
        return proc.StandardOutput.ReadToEnd();
    }

    public static void OpenUrl(string url) {
        try {
            Process.Start(url);
        }
        catch {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
                url = url.Replace("&", "^&");
                Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
            } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
                Process.Start("xdg-open", url);
            } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
                Process.Start("open", url);
            } else {
                throw;
            }
        }
    }
}