#!/bin/sh

RUN_WITH_PROXY() {
    BINARY=$1
    shift
    if [ -n "$BINARY" ]; then
        exec "$BINARY" --proxy-server="socks5://127.0.0.1:65000" "$@"
    fi
}

GET_VESKTOP() {
    if command -v vesktop >/dev/null 2>&1; then
        echo "$(command -v vesktop)"
    elif command -v vesktop-bin >/dev/null 2>&1; then
        echo "$(command -v vesktop-bin)"
    elif command -v flatpak >/dev/null 2>&1 && flatpak list --ids | grep -q "dev.vencord.Vesktop"; then
        echo "flatpak run dev.vencord.Vesktop"
    fi
}

GET_DISCORD() {
    if command -v linux-discord >/dev/null 2>&1; then
        echo "$(command -v linux-discord)"
    elif command -v flatpak >/dev/null 2>&1 && flatpak list --ids | grep -q "com.discordapp.Discord"; then
        echo "flatpak run com.discordapp.Discord"
    fi
}

OS=$(uname -s)

if [ "$OS" = "FreeBSD" ]; then
    CMD=$(GET_DISCORD)
    RUN_WITH_PROXY $CMD "$@"
elif [ "$OS" = "Linux" ]; then
    CMD=$(GET_VESKTOP)
    RUN_WITH_PROXY $CMD "$@"
else
    linux-discord --proxy-server="socks5://127.0.0.1:65000" "$@"
fi
