#!/usr/bin/env bash
set -euo pipefail

log() { printf "\n==> %s\n" "$*"; }
have() { command -v "$1" >/dev/null 2>&1; }

REPO_URL="https://github.com/badaix/snapcast.git"
WORKDIR="${WORKDIR:-$HOME/tmp/snapcast}"
PREFIX="${PREFIX:-$HOME/.local}"

mkdir -p "$PREFIX/bin"
mkdir -p "$(dirname "$WORKDIR")"

# --- Detect distro ---
if [[ -r /etc/os-release ]]; then
  # shellcheck disable=SC1091
  . /etc/os-release
else
  echo "Cannot read /etc/os-release; unsupported system." >&2
  exit 1
fi
#!/usr/bin/env bash
set -euo pipefail

log() { printf "\n==> %s\n" "$*"; }

REPO_URL="https://github.com/badaix/snapcast.git"
WORKDIR="${WORKDIR:-$HOME/tmp/snapcast}"
PREFIX="${PREFIX:-$HOME/.local}"

mkdir -p "$PREFIX/bin"
mkdir -p "$(dirname "$WORKDIR")"

# --- Detect distro ---
if [[ -r /etc/os-release ]]; then
  # shellcheck disable=SC1091
  . /etc/os-release
else
  echo "Cannot read /etc/os-release; unsupported system." >&2
  exit 1
fi

IS_ARCH=0
case "${ID:-}" in
  arch|endeavouros) IS_ARCH=1 ;;
esac
if [[ $IS_ARCH -eq 0 && -n "${ID_LIKE:-}" ]] && echo "$ID_LIKE" | grep -qi arch; then
  IS_ARCH=1
fi

IS_FEDORA=0
case "${ID:-}" in
  fedora) IS_FEDORA=1 ;;
esac
if [[ $IS_FEDORA -eq 0 && -n "${ID_LIKE:-}" ]] && echo "$ID_LIKE" | grep -qiE 'fedora|rhel'; then
  IS_FEDORA=1
fi

if [[ $IS_ARCH -eq 0 && $IS_FEDORA -eq 0 ]]; then
  echo "Unsupported distro (ID=${ID:-unknown}, ID_LIKE=${ID_LIKE:-unknown})." >&2
  echo "This script supports Fedora and Arch/EndeavourOS." >&2
  exit 1
fi

# --- Install dependencies ---
if [[ $IS_ARCH -eq 1 ]]; then
  log "Detected Arch/EndeavourOS. Installing build deps with pacman..."
  sudo pacman -Sy --needed --noconfirm \
    base-devel git cmake pkgconf \
    boost flac libogg libvorbis opus alsa-lib libsoxr avahi \
    openssl expat
else
  log "Detected Fedora. Installing build deps with dnf..."
  sudo dnf install -y \
    git cmake make gcc gcc-c++ \
    pkgconf-pkg-config \
    boost-devel \
    alsa-lib-devel \
    avahi-devel \
    flac-devel \
    libogg-devel \
    libvorbis-devel \
    opus-devel \
    soxr-devel \
    expat-devel \
    openssl-devel
fi

# --- Prefer system pkg-config when available (avoid linuxbrew/pkg-config issues) ---
PKG_CONFIG_BIN="pkg-config"
if [[ -x /usr/bin/pkg-config ]]; then
  PKG_CONFIG_BIN="/usr/bin/pkg-config"
fi

# Clear env that commonly breaks pkg-config discovery (esp. with linuxbrew)
unset PKG_CONFIG_PATH PKG_CONFIG_LIBDIR CMAKE_PREFIX_PATH CPATH LIBRARY_PATH LD_LIBRARY_PATH

# Force known-good pkg-config search dirs on Linux (Fedora/Arch)
if [[ "$(uname -s)" == "Linux" ]]; then
  # Arch uses /usr/lib/pkgconfig; Fedora uses /usr/lib64/pkgconfig
  export PKG_CONFIG_LIBDIR="/usr/lib/pkgconfig:/usr/share/pkgconfig:/usr/lib64/pkgconfig"
fi

log "Using pkg-config at: $PKG_CONFIG_BIN"
"$PKG_CONFIG_BIN" --version || true

# --- Show which pkg-config modules are available ---
log "Checking required pkg-config modules..."
REQ_MODULES=(alsa avahi-client flac ogg vorbis vorbisenc opus soxr)
missing=()
for m in "${REQ_MODULES[@]}"; do
  if "$PKG_CONFIG_BIN" --exists "$m"; then
    printf "  [OK] %s\n" "$m"
  else
    printf "  [MISSING] %s\n" "$m"
    missing+=("$m")
  fi
done

if [[ ${#missing[@]} -ne 0 ]]; then
  echo
  echo "Missing pkg-config modules: ${missing[*]}" >&2
  echo "If you're on Fedora, double-check the devel packages installed:" >&2
  echo "  sudo dnf install -y alsa-lib-devel avahi-devel flac-devel libogg-devel libvorbis-devel opus-devel soxr-devel pkgconf-pkg-config" >&2
  echo "Also ensure you're not using linuxbrew's pkg-config (which pkg-config should be /usr/bin/pkg-config)." >&2
  exit 1
fi

# --- Fetch source ---
if [[ -d "$WORKDIR/.git" ]]; then
  log "Repo exists. Updating..."
  git -C "$WORKDIR" pull --rebase
else
  log "Cloning Snapcast..."
  git clone "$REPO_URL" "$WORKDIR"
fi

# --- Build snapclient only ---
cd "$WORKDIR"
mkdir -p build
cd build

log "Configuring (install prefix: $PREFIX)"
cmake .. \
  -DCMAKE_INSTALL_PREFIX="$PREFIX" \
  -DBUILD_SERVER=OFF \
  -DBUILD_CLIENT=ON

log "Building..."
make -j"$(nproc)"

log "Installing to $PREFIX/bin (no sudo)..."
make install

log "Done."
echo "Try: $PREFIX/bin/snapclient --version"
