# $Id$
# Maintainer: MediaArea.net SARL <info@mediaarea.net>

pkgname=('bwfmetaedit' 'bwfmetaedit-plugin-c2pa' 'bwfmetaedit-gui')
pkgver=26.01.20260815
pkgrel=1
pkgdesc="Supplies technical and tag information about a video or audio file"
arch=('x86_64')
url="https://mediaarea.net/BWFMetaEdit"
license=('GPL')

makedepends=('qt5-base' 'qt5-quickcontrols2' 'qt5-svg' 'qt5-tools' 'ca-certificates' 'rust')
source=(${pkgname}_${pkgver}.tar.xz bwfmetaedit-vendor_${pkgver}.tar.xz)
sha256sums=('SKIP' 'SKIP')

prepare() {
    cd "${srcdir}"/bwfmetaedit/Source/ThirdParty/c2pa-rs
    rm -rf vendor
    tar -xf "${srcdir}"/bwfmetaedit-vendor_${pkgver}.tar.xz
    mkdir .cargo
    printf '[source.crates-io]\nreplace-with = "vendored-sources"\n\n[source.vendored-sources]\ndirectory = "vendor"\n' > .cargo/config.toml

    cd "${srcdir}"/bwfmetaedit/Project/GNU/CLI
    sh ./autogen
    # libc2pa_c.so's has undefined references to internal ring_core_* symbols,
    # private implementation details never called from outside, shouldn't be re-validated here.
    LDFLAGS="$LDFLAGS -Wl,-rpath,/usr/lib/bwfmetaedit -Wl,--allow-shlib-undefined" ./configure --prefix=/usr --enable-c2pa

    cd "${srcdir}"/bwfmetaedit/Project/QtCreator
    sh ./prepare ENABLE_C2PA=yes QMAKE_RPATHDIR+=/usr/lib/bwfmetaedit QMAKE_LFLAGS+=-Wl,--allow-shlib-undefined BINDIR=/usr/bin
}

build() {
    cd "${srcdir}"/bwfmetaedit/Source/ThirdParty/c2pa-rs
    CARGO_HOME="${srcdir}"/cargo_home CARGO_NET_OFFLINE=true \
        cargo build --release --offline --locked -p c2pa-c-ffi --no-default-features --features "rust_native_crypto, http, file_io"
    strip target/release/libc2pa_c.so

    cd "${srcdir}"/bwfmetaedit/Project/GNU/CLI
    make

    cd "${srcdir}"/bwfmetaedit/Project/QtCreator
    make
}

package_bwfmetaedit() {
    pkgdesc="${pkgdesc} (CLI)"
    depends=('bwfmetaedit-plugin-c2pa')

    cd "${srcdir}"/bwfmetaedit/Project/GNU/CLI
    make DESTDIR="${pkgdir}" install

    install -D -m 0644 "${srcdir}"/bwfmetaedit/License.html "${pkgdir}"/usr/share/licenses/${pkgname}/License.html
    install -D -m 0644 "${srcdir}"/bwfmetaedit/History_CLI.txt "${pkgdir}"/usr/share/doc/${pkgname}/History.txt
    install -D -m 0644 "${srcdir}"/bwfmetaedit/Release/ReadMe_CLI_Linux.txt "${pkgdir}"/usr/share/doc/${pkgname}/ReadMe.txt
    install -D -m 0644 "${srcdir}"/bwfmetaedit/conformance_point_document.xsd "${pkgdir}"/usr/share/doc/${pkgname}/conformance_point_document.xsd
}

package_bwfmetaedit-plugin-c2pa() {
    pkgdesc="C2PA (Content Credentials) support library for BWF MetaEdit"

    install -D -m 0755 "${srcdir}"/bwfmetaedit/Source/ThirdParty/c2pa-rs/target/release/libc2pa_c.so "${pkgdir}/usr/lib/bwfmetaedit/libc2pa_c.so"
    install -D -m 0644 "${srcdir}"/bwfmetaedit/License.html "${pkgdir}"/usr/share/licenses/${pkgname}/License.html
}

package_bwfmetaedit-gui() {
    pkgdesc="${pkgdesc} (GUI)"
    depends=('bwfmetaedit-plugin-c2pa' 'qt5-base' 'qt5-quickcontrols2' 'qt5-svg')

    cd "${srcdir}"/bwfmetaedit/Project/QtCreator
    make INSTALL_ROOT="${pkgdir}" install

    install -D -m 0644 "${srcdir}"/bwfmetaedit/License.html "${pkgdir}"/usr/share/licenses/${pkgname}/License.html
    install -D -m 0644 "${srcdir}"/bwfmetaedit/History_GUI.txt "${pkgdir}"/usr/share/doc/${pkgname}/History.txt
    install -D -m 0644 "${srcdir}"/bwfmetaedit/Release/ReadMe_GUI_Linux.txt "${pkgdir}"/usr/share/doc/${pkgname}/ReadMe.txt
    install -D -m 0644 "${srcdir}"/bwfmetaedit/conformance_point_document.xsd "${pkgdir}"/usr/share/doc/${pkgname}/conformance_point_document.xsd

    install -D -m 0644 "${srcdir}"/bwfmetaedit/Source/Resource/Image/Logo/Logo128.png "${pkgdir}"/usr/share/icons/hicolor/128x128/apps/bwfmetaedit-gui.png
    install -D -m 0644 "${srcdir}"/bwfmetaedit/Source/Resource/Image/Logo/Logo128.png "${pkgdir}"/usr/share/pixmaps/bwfmetaedit-gui.png
    install -D -m 0644 "${srcdir}"/bwfmetaedit/Project/GNU/GUI/bwfmetaedit-gui.desktop "${pkgdir}"/usr/share/applications/bwfmetaedit-gui.desktop
    install -D -m 0644 "${srcdir}"/bwfmetaedit/Project/GNU/GUI/bwfmetaedit-gui.metainfo.xml "${pkgdir}"/usr/share/metainfo/bwfmetaedit-gui.metainfo.xml
    install -D -m 0644 "${srcdir}"/bwfmetaedit/Project/GNU/GUI/bwfmetaedit-gui.kde3.desktop "${pkgdir}"/usr/share/apps/konqueror/servicemenus/bwfmetaedit-gui.desktop
    install -D -m 0644 "${srcdir}"/bwfmetaedit/Project/GNU/GUI/bwfmetaedit-gui.kde4.desktop "${pkgdir}"/usr/share/kde4/services/ServiceMenus/bwfmetaedit-gui.desktop
    install -D -m 0644 "${srcdir}"/bwfmetaedit/Project/GNU/GUI/bwfmetaedit-gui.kde4.desktop "${pkgdir}"/usr/share/kservices5/ServiceMenus/bwfmetaedit-gui.desktop
}
