#!/bin/bash
set -e

echo "[install-tools] Installing ffmpeg + yt-dlp..."

apt-get update && apt-get install -y --no-install-recommends curl ca-certificates xz-utils \
  && rm -rf /var/lib/apt/lists/*

# FFmpeg (static build from yt-dlp project)
curl -L https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz \
  -o /tmp/ffmpeg.tar.xz
mkdir -p /tmp/ffmpeg
tar -xJf /tmp/ffmpeg.tar.xz -C /tmp/ffmpeg --strip-components=2 --wildcards "*/bin/ffmpeg"
mv /tmp/ffmpeg/ffmpeg /usr/local/bin/ffmpeg
chmod +x /usr/local/bin/ffmpeg
rm -rf /tmp/ffmpeg /tmp/ffmpeg.tar.xz

# yt-dlp (standalone linux binary, no Python needed)
curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_linux \
  -o /usr/local/bin/yt-dlp
chmod +x /usr/local/bin/yt-dlp

# Cleanup curl/xz (keep ca-certificates for HTTPS)
apt-get purge -y curl xz-utils && apt-get autoremove -y && rm -rf /var/lib/apt/lists/*

echo "[install-tools] Done. ffmpeg=$(ffmpeg -version 2>&1 | head -1), yt-dlp=$(yt-dlp --version 2>/dev/null || echo 'ok')"
