#!/bin/bash
# setup_hw_decode.sh — Enable Rockchip hardware video decoding on ArkOS / R36S
#
# What this script does:
#   1. Adds a udev rule so the 'video' group can access /dev/vpu_service
#      (the VPU device used by Rockchip MPP on the kernel 4.4 BSP).
#   2. Installs librockchip-mpp and the gst-mpp GStreamer plugin, in order:
#      a) If a 'mpp-libs/' subdirectory is present next to this script,
#         copies the pre-built .so files directly (no network required).
#      b) Otherwise tries apt-get.
#      c) If apt fails, prints manual installation instructions.
#
# Requirements:
#   - Must be run as root or via sudo.
#   - Internet access (for apt-get).
#   - Target: RK3326 / PX30 device running ArkOS with Ubuntu eoan arm64.
#
# After this script succeeds, restart the DLNA browser app.  The GStreamer
# backend will detect mppvideodec and prefer it over software avdec_h264.

set -euo pipefail

info()  { echo "[setup-hw-decode] $*"; }
warn()  { echo "[setup-hw-decode] WARNING: $*" >&2; }
die()   { echo "[setup-hw-decode] ERROR: $*" >&2; exit 1; }

[[ $EUID -eq 0 ]] || die "This script must be run as root (use sudo)."

# ---------------------------------------------------------------------------
# 1. udev rule — /dev/vpu_service accessible to 'video' group
# ---------------------------------------------------------------------------
UDEV_RULE="/etc/udev/rules.d/50-vpu-service.rules"

info "Writing udev rule: $UDEV_RULE"
cat > "$UDEV_RULE" <<'EOF'
# Rockchip VPU service — allow the 'video' group to access the hardware encoder/decoder.
KERNEL=="vpu_service",  GROUP="video", MODE="0660"
KERNEL=="mpp_service",  GROUP="video", MODE="0660"
EOF

udevadm control --reload-rules
udevadm trigger --name-match=vpu_service 2>/dev/null || true
udevadm trigger --name-match=mpp_service 2>/dev/null || true

info "/dev/vpu_service access granted to 'video' group."
info "Verify with: ls -la /dev/vpu_service  (should show crw-rw---- root video)"

# ---------------------------------------------------------------------------
# 2. librockchip-mpp0 + gstreamer1.0-rockchip-mpp
# ---------------------------------------------------------------------------
#
# These packages are NOT in the standard Ubuntu eoan repos.
# Sources known to carry arm64 builds for RK3326/PX30:
#
#   A) ODROID community apt mirror (requires adding the source manually):
#      deb https://oph.mdrjr.net/meveric/ stretch main
#      apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A0A3B33F
#
#   B) Pre-built .deb files from the OGA / ArkOS forum / Discord.
#      Typical package names:
#        librockchip-mpp0_1.x.x_arm64.deb
#        gstreamer1.0-rockchip-mpp_1.x.x_arm64.deb
#
#   C) Build from source (advanced):
#        https://github.com/rockchip-linux/mpp
#        https://github.com/JeffyCN/gst-mpp
#
# This script tries apt-get first; if the package is not found it falls back
# to printing the manual instructions and exits with success (non-blocking).

MPP_PKGS="librockchip-mpp0 gstreamer1.0-rockchip-mpp"

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MPP_LIBS_DIR="$SCRIPT_DIR/mpp-libs"

if [[ -d "$MPP_LIBS_DIR" ]]; then
    info "Found pre-built libraries in $MPP_LIBS_DIR — copying..."
    # Core MPP runtime
    for f in "$MPP_LIBS_DIR"/librockchip_mpp.so* "$MPP_LIBS_DIR"/librockchip_vpu.so*; do
        [[ -e "$f" ]] && cp -v "$f" /usr/lib/aarch64-linux-gnu/
    done
    # GStreamer plugin
    if [[ -f "$MPP_LIBS_DIR/libgstrockchipmpp.so" ]]; then
        cp -v "$MPP_LIBS_DIR/libgstrockchipmpp.so" /usr/lib/aarch64-linux-gnu/gstreamer-1.0/
    fi
    ldconfig
    info "MPP libraries installed from $MPP_LIBS_DIR."
elif apt-cache show librockchip-mpp0 &>/dev/null; then
    info "Found librockchip-mpp0 in apt — installing..."
    apt-get install -y $MPP_PKGS
    info "MPP packages installed successfully."
else
    warn "librockchip-mpp0 is not available in the current apt sources."
    echo
    echo "  To enable hardware H.264/H.265/VP8/VP9 decoding, install these arm64 packages:"
    echo "    librockchip-mpp0        — Rockchip MPP runtime library"
    echo "    gstreamer1.0-rockchip-mpp — GStreamer plugin that exposes mppvideodec"
    echo
    echo "  Option A — Add a community apt source with RK3326 support, then re-run this script."
    echo "  Option B — Download pre-built .deb files and run:"
    echo "    sudo dpkg -i librockchip-mpp0_*.deb gstreamer1.0-rockchip-mpp_*.deb"
    echo
    echo "  The udev rule above has already been applied regardless."
    echo "  Once the packages are installed, restart the DLNA browser app."
fi

info "Done."
