# Maintainer: Xeonacid <h.dwwwwww@gmail.com>
# Maintainer: KokaKiwi <kokakiwi+aur@kokakiwi.net>
# Contributor: George Rawlinson <george@rawlinson.net.nz>
# Contributor: RocketDev <ma2014119@outlook.com>

_pkgname=ImHex
pkgname=${_pkgname,,}
pkgver=1.38.1
pkgrel=2
pkgdesc='A Hex Editor for Reverse Engineers, Programmers and people that value their eye sight when working at 3 AM'
url='https://imhex.werwolv.net'
license=('GPL-2.0-only')
arch=('x86_64')
depends=(
    'glfw'
    'hicolor-icon-theme' 'xdg-desktop-portal' # needed in runtime
    'dbus'
    'mbedtls' 'libmbedcrypto.so'
    'curl' 'libcurl.so'
    'freetype2' 'libfreetype.so'
    'fmt' 'libfmt.so'
    'yara' 'libyara.so'
    'file' 'libmagic.so'
    'libglvnd' # needed in runtime
    'glibc' 'gcc-libs' 'bash'
    'libxrandr' # needed in runtime
    'fontconfig'
    'capstone'
    'md4c'
    # required by imhex plugins
    'lz4'
    'zlib'
    'bzip2'
    'xz'
    'zstd'
    'libssh2'
)
makedepends=('git' 'cmake' 'ninja' 'llvm' 'nlohmann-json' 'librsvg' 'python' 'cli11' 'boost' 'dotnet-runtime')
optdepends=(
    'dotnet-runtime: dotnet script support'
)
replaces=('imhex-patterns-git')
source=(
    "$pkgname::git+https://github.com/WerWolv/ImHex.git#tag=v$pkgver"
    "nativefiledialog::git+https://github.com/btzy/nativefiledialog-extended.git"
    "xdgpp::git+https://git.sr.ht/~danyspin97/xdgpp"
    "libromfs::git+https://github.com/WerWolv/libromfs"
    "pattern_language::git+https://github.com/WerWolv/PatternLanguage"
    "libwolv::git+https://github.com/WerWolv/libwolv"
    "HashLibPlus::git+https://github.com/WerWolv/HashLibPlus"
    "lunasvg::git+https://github.com/sammycage/lunasvg"
    "edlib::git+https://github.com/Martinsos/edlib"
    "throwing_ptr::git+https://github.com/rockdreamer/throwing_ptr"
    "ImHex-Patterns::git+https://github.com/WerWolv/ImHex-Patterns.git"
    "disassembler::git+https://github.com/WerWolv/Disassembler.git"
)
b2sums=('5b984ee5c6e8c5e6ab4e9caa1493e90d5c18567887156312f8248ac62c4e7cf499372aee25108f35ae3db191a0b07ecca764872201cd1d20219b16fdc8700e19'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP')

prepare() {
    cd "$pkgname"

    git submodule init
    for name in libromfs pattern_language libwolv disassembler; do
        git submodule set-url lib/external/"$name" "$srcdir/$name"
        git -c protocol.file.allow=always submodule update lib/external/$name
    done
    for name in nativefiledialog xdgpp HashLibPlus edlib lunasvg; do
        git submodule set-url lib/third_party/"$name" "$srcdir/$name"
        git -c protocol.file.allow=always submodule update lib/third_party/"$name"
    done
    pushd lib/external/pattern_language
        git submodule init
        for name in libwolv throwing_ptr; do
            git submodule set-url external/"$name" "$srcdir/$name"
            git -c protocol.file.allow=always submodule update external/"$name"
        done
    popd
}

build() {
    cmake -B build -S "$pkgname" \
        -G Ninja \
        -Wno-dev \
        -D CMAKE_BUILD_TYPE=None \
        -D CMAKE_INSTALL_PREFIX=/usr \
        -D CMAKE_SKIP_RPATH=ON \
        -D IMHEX_OFFLINE_BUILD=ON \
        -D IMHEX_IGNORE_BAD_CLONE=ON \
        -D IMHEX_STRIP_RELEASE=OFF \
        -D IMHEX_USE_GTK_FILE_PICKER=OFF \
        -D IMHEX_STRICT_WARNINGS=OFF \
        -D IMHEX_BUNDLE_DOTNET=OFF \
        -D IMHEX_BUNDLE_PLUGIN_SDK=OFF \
        -D USE_SYSTEM_LLVM=ON \
        -D USE_SYSTEM_YARA=ON \
        -D USE_SYSTEM_FMT=ON \
        -D USE_SYSTEM_NLOHMANN_JSON=ON \
        -D USE_SYSTEM_CAPSTONE=ON \
        -D USE_SYSTEM_MD4C=ON \
        -D USE_SYSTEM_CLI11=ON \
        -D USE_SYSTEM_BOOST=ON \
        -D LIBPL_ENABLE_CLI=ON \
        -D LIBPL_BUILD_CLI_AS_EXECUTABLE=OFF \
        -D IMHEX_VERSION="$pkgver" \
        -D IMHEX_ENABLE_UNIT_TESTS=ON \
        -D IMHEX_ENABLE_PLUGIN_TESTS=ON \
        -D IMHEX_ENABLE_IMGUI_TEST_ENGINE=ON \
        -D CMAKE_POLICY_VERSION_MINIMUM=3.5

    cmake --build build --parallel

    cmake --build build --parallel --target unit_tests
}

package() {
    DESTDIR="$pkgdir" cmake --install build
    
    # Documentation
    install -Dm0644 -t "$pkgdir/usr/share/doc/$pkgname" \
        "$pkgname/README.md"
}

check() {
    export LD_LIBRARY_PATH="$PWD/build/plugins:$PWD/build/lib/libimhex"
    pushd "$pkgname/lib/external/pattern_language"
        python tests/integration/integration.py ../../../../build/imhex --pl
    popd

    ctest --test-dir build --output-on-failure
}
# vim: set ts=4 sw=4 et:
