#!/bin/sh
# Builds the Ubuntu package of clipson in a container (x86_64 host, Docker):
#   docker run --rm -v "$PWD":/src -w /src ubuntu:24.04 sh tools/build-deb.sh
# Output: target/deb/clipson_<version>_amd64.deb. Built on 24.04 so it runs on 24.04 and later.
#
# It runs as root in a fresh Ubuntu 24.04 and installs the build packages with apt and rustup, so it
# is not meant for a desktop. Ubuntu ships an older FFmpeg than the 9.0 clipson needs, so a minimal
# FFmpeg with the VAAPI encoders is built from the release archive and packed into /usr/lib/clipson,
# where the rpath of clipson finds it. The archive is the one offered on the website (LGPL source offer).
set -eu

FFMPEG_VERSION=9.0
LIBDIR=/usr/lib/clipson

root=$(cd "$(dirname "$0")/.." && pwd)
work=$root/target/deb
stage=$work/stage
version=$(grep -m1 '^version' "$root/Cargo.toml" | cut -d'"' -f2)

export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y -qq --no-install-recommends ca-certificates curl xz-utils build-essential nasm \
    pkg-config libva-dev libdrm-dev libpipewire-0.3-dev clang libclang-dev >/dev/null
if ! command -v cargo >/dev/null; then
    curl -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal >/dev/null
fi
. "$HOME/.cargo/env"

# FFmpeg: shared libraries that find each other in LIBDIR.
mkdir -p "$work"
cd "$work"
if [ ! -d "ffmpeg-$FFMPEG_VERSION" ]; then
    curl -fsSLO "https://ffmpeg.org/releases/ffmpeg-$FFMPEG_VERSION.tar.xz"
    tar xf "ffmpeg-$FFMPEG_VERSION.tar.xz"
fi
rm -rf "$stage"
mkdir -p build
cd build
"../ffmpeg-$FFMPEG_VERSION/configure" \
    --prefix=/usr --libdir="$LIBDIR" --shlibdir="$LIBDIR" --incdir=/usr/include/clipson \
    --disable-everything --disable-autodetect --disable-programs --disable-doc --disable-network \
    --disable-avdevice --disable-swscale --disable-swresample \
    --enable-shared --disable-static --enable-pthreads \
    --enable-vaapi --enable-libdrm \
    --enable-encoder=aac,h264_vaapi,hevc_vaapi,av1_vaapi \
    --enable-filter=buffer,buffersink,hwmap,scale_vaapi \
    --enable-muxer=mp4 --enable-protocol=file \
    --extra-ldflags="-Wl,-rpath,$LIBDIR"
make -j"$(nproc)"
make install DESTDIR="$stage"

# The layout FFMPEG_DIR expects: include and lib.
rm -rf "$work/ffmpeg-dir"
mkdir -p "$work/ffmpeg-dir"
ln -s "$stage/usr/include/clipson" "$work/ffmpeg-dir/include"
ln -s "$stage$LIBDIR" "$work/ffmpeg-dir/lib"

cd "$root"
# DT_RPATH (old dtags) also applies to the libraries the FFmpeg libraries load.
export FFMPEG_DIR="$work/ffmpeg-dir" LIBCLANG_PATH=/usr/lib/llvm-18/lib
export RUSTFLAGS="-C link-arg=-Wl,-rpath,$LIBDIR -C link-arg=-Wl,--disable-new-dtags"
cargo build --release --locked --target-dir "$work/target"

# Package tree. Headers and pkg-config files are not shipped.
rm -rf "$stage/usr/include" "$stage$LIBDIR/pkgconfig" "$stage/usr/share"
install -Dm755 "$work/target/release/clipson" "$stage/usr/bin/clipson"
install -Dm644 installer/linux/clipson.desktop "$stage/usr/share/applications/clipson.desktop"
install -Dm644 web/icon.png "$stage/usr/share/icons/hicolor/256x256/apps/clipson.png"
install -Dm644 LICENSE "$stage/usr/share/doc/clipson/copyright"
install -Dm644 "$work/ffmpeg-$FFMPEG_VERSION/COPYING.LGPLv2.1" "$stage/usr/share/doc/clipson/LICENSE.ffmpeg"
cat >>"$stage/usr/share/doc/clipson/copyright" <<NOTICE

The libraries in $LIBDIR are FFmpeg $FFMPEG_VERSION under the LGPL 2.1 (LICENSE.ffmpeg).
Source: https://clipson.walleweb.se/ffmpeg-$FFMPEG_VERSION.tar.xz, built by tools/build-deb.sh.
NOTICE
strip --strip-unneeded "$stage/usr/bin/clipson" "$stage$LIBDIR"/*.so.*.*.*

mkdir -p "$stage/DEBIAN"
size=$(du -sk --exclude=DEBIAN "$stage" | cut -f1)
cat >"$stage/DEBIAN/control" <<CONTROL
Package: clipson
Version: $version
Section: video
Priority: optional
Architecture: amd64
Installed-Size: $size
Depends: libc6 (>= 2.39), libgcc-s1, libpipewire-0.3-0, libva2, libva-drm2, libdrm2, pipewire, xdg-desktop-portal
Recommends: xdg-desktop-portal-gnome | xdg-desktop-portal-kde | xdg-desktop-portal-hyprland | xdg-desktop-portal-wlr, xdg-user-dirs
Maintainer: William Forsdal <william.forsdal@gmail.com>
Homepage: https://clipson.walleweb.se/
Description: Instant replay recorder
 Keeps the last minutes of the screen in memory and saves them as an MP4
 on a key. AMD GPUs only (VAAPI), Wayland with a screen cast portal.
CONTROL
dpkg-deb --build --root-owner-group "$stage" "$work/clipson_${version}_amd64.deb"
ls -l "$work/clipson_${version}_amd64.deb"
