# Note that this script can accept some limited command-line arguments, run
# `julia build_tarballs.jl --help` to see a usage message.
using BinaryBuilder, Pkg

function build_harfbuzz(ARGS, name::String)

    icu = name == "HarfBuzz_ICU"

    # Harfbuzz's non-breaking major releases have started incurring
    # some compatibility troubles for me as I try to use HarfBuzz_jll,
    # since a compat like "2.8.1" doesn't include the compatible v8 or
    # v14 or any other hypothetical release. HarfBuzz does promise
    # that their API/ABI has been stable since v0 and will continue to
    # be nonbreaking through major version bumps. See:
    # https://github.com/harfbuzz/harfbuzz#api-stability

    # We smooth this over with an invented version number:
    # VersionNumber(100, 1000*major + minor, patch).

    version = v"14.3.0"
    ygg_version = VersionNumber(100, 1000 * version.major + version.minor, version.patch)

    # Collection of sources required to build Harfbuzz
    sources = [
        ArchiveSource("https://github.com/harfbuzz/harfbuzz/releases/download/$(version)/harfbuzz-$(version).tar.xz",
                      "16070d77cfc4ba1f1e7327e83bf9b3f55898081cabdb94e56a33e04fc8874eae"),
        DirectorySource("../bundled"),
    ]

    # Bash recipe for building across all platforms
    # Side note: this is a great use-case for
    # https://github.com/JuliaPackaging/BinaryBuilder.jl/issues/778
    script = "ICU=$(icu)\n" * raw"""
cd $WORKSPACE/srcdir/harfbuzz-*/

# On MacOS, bypass broken check for CoreText
if [[ "${target}" == *-apple-darwin* ]]; then
    atomic_patch -p1 ../patches/coretext-check-bypass.patch
    # MacOS must not define a macro `verify` that clashes with HarfBuzz
    cpp_args='-D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0'
fi

# We need C++20 for the `auto` in template parameters.
# This is required when building the ICU bindings.
mkdir build && cd build
meson .. \
    --cross-file="${MESON_TARGET_TOOLCHAIN}" \
    --buildtype=release \
    -Dcpp_std=c++20 \
    -Dcpp_args="${cpp_args}" \
    -Dcairo=enabled \
    -Dfreetype=enabled \
    -Dglib=enabled \
    -Dgobject=enabled \
    -Dgraphite=enabled \
    -Dintrospection=disabled \
    -Ddocs=disabled \
    -Dtests=disabled \
    -Dicu=auto \
    -Dicu_builtin=false \
    -Dcoretext=enabled \
    -Dgdi=enabled \
    -Ddirectwrite=enabled \
    -Dutilities=disabled
ninja -j${nproc}
if [[ "${ICU}" == true ]]; then
    # Remove directories with symbol files (they confuse the `cp` command below)
    rm -rf src/libharfbuzz-icu*${dlext}*.p
    # Manually install only ICU-related files
    cp src/libharfbuzz-icu*${dlext}* ${libdir}/.
    cp meson-private/harfbuzz-icu.pc ${prefix}/lib/pkgconfig/.
    cp ../src/hb-icu.h ${includedir}/harfbuzz/.
else
    ninja install
fi
"""

    # These are the platforms we will build for by default, unless further
    # platforms are passed in on the command line
    platforms = supported_platforms()

    # The products that we will ensure are always built
    products = if icu
        [
            LibraryProduct("libharfbuzz-icu", :libharfbuzz_icu),
        ]
    else
        [
            LibraryProduct("libharfbuzz", :libharfbuzz),
            LibraryProduct("libharfbuzz-subset", :libharfbuzz_subset),
            LibraryProduct("libharfbuzz-gobject", :libharfbuzz_gobject),
        ]
    end

    # Dependencies that must be installed before this package can be built
    dependencies = [
        Dependency("Cairo_jll"; compat="1.18.5"),
        Dependency("Fontconfig_jll"; compat="2.16.0"),
        Dependency("FreeType2_jll"; compat="2.13.4"),
        Dependency("Glib_jll"; compat="2.84.0"),
        Dependency("Graphite2_jll"; compat="1.3.15"),
        Dependency("Libffi_jll"; compat="~3.4.7"),
        BuildDependency("Xorg_xorgproto_jll"),
    ]

    if icu
        append!(dependencies, [
            Dependency("HarfBuzz_jll"; compat="$(ygg_version)"),
            Dependency("ICU_jll"; compat="76.2"),
        ])
    end

    # Build the tarballs, and possibly a `build.jl` as well.
    # We need at lest GCC 8 to support C++ 20
    build_tarballs(ARGS, name, ygg_version, sources, script, platforms, products, dependencies;
                   clang_use_lld=false, julia_compat="1.6", preferred_gcc_version=v"8")
end
