#!/usr/bin/env bash
# Helper for Linux shared hosting (cPanel SSH) without root access:
# Installs yt-dlp and downloads a static Linux x86_64 ffmpeg + ffprobe into ./bin/
set -euo pipefail

APP_DIR="$(cd "$(dirname "$0")" && pwd)"
mkdir -p "$APP_DIR/bin"

echo "Installing Python dependencies..."
pip install --no-cache-dir -r "$APP_DIR/requirements.txt"

if ! command -v ffmpeg >/dev/null 2>&1; then
  echo "Downloading static Linux ffmpeg/ffprobe into $APP_DIR/bin..."
  TMP_DIR="$(mktemp -d)"
  curl -fsSL "https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz" -o "$TMP_DIR/ffmpeg.tar.xz"
  tar -xf "$TMP_DIR/ffmpeg.tar.xz" -C "$TMP_DIR"
  cp "$TMP_DIR"/ffmpeg-*-amd64-static/ffmpeg "$APP_DIR/bin/ffmpeg"
  cp "$TMP_DIR"/ffmpeg-*-amd64-static/ffprobe "$APP_DIR/bin/ffprobe"
  chmod +x "$APP_DIR/bin/ffmpeg" "$APP_DIR/bin/ffprobe"
  rm -rf "$TMP_DIR"
fi

if ! command -v yt-dlp >/dev/null 2>&1; then
  echo "Installing standalone yt-dlp into $APP_DIR/bin..."
  curl -fsSL "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_linux" -o "$APP_DIR/bin/yt-dlp"
  chmod +x "$APP_DIR/bin/yt-dlp"
fi

echo "Setup complete! Binaries ready in $APP_DIR/bin"
