abinfo "Patching tg_owt ..."
cd "$SRCDIR"/../tg_owt
ab_apply_patches "$SRCDIR"/autobuild/patches/*.owt.deferred

abinfo "Building tg_owt ..."
cmake -B build \
      -G Ninja \
      -DBUILD_SHARED_LIBS=OFF \
      -DCMAKE_BUILD_TYPE=RelWithDebInfo \
      -DCMAKE_INSTALL_PREFIX=/usr \
      -DTG_OWT_DLOPEN_PIPEWIRE=OFF .
ninja -C build -v
ninja -C build install

abinfo "Patching tdlib ..."
cd "$SRCDIR"/../td
ab_apply_patches "$SRCDIR"/autobuild/patches/*.td.deferred

abinfo "Building tdlib ..."
cmake -B build \
      -G Ninja \
      -DTD_INSTALL_SHARED_LIBRARIES=OFF \
      -DTD_INSTALL_STATIC_LIBRARIES=ON \
      -DTD_E2E_ONLY=ON \
      -DCMAKE_BUILD_TYPE=RelWithDebInfo \
      -DCMAKE_INSTALL_PREFIX=/usr .
ninja -C build -v
ninja -C build install

cd "$SRCDIR"

if ab_match_arch riscv64; then
    abinfo "Injecting -pthread and -latomic for RISCV64..."
    export LDFLAGS="${LDFLAGS} -pthread -latomic"
fi

abinfo "Applying API ID from snapcraft.yml ..."
export CMAKE_AFTER=(${CMAKE_AFTER[@]} $(grep TDESKTOP_API_ "$SRCDIR"/snap/snapcraft.yaml | awk '{print $2}'))
