abinfo 'Setting up build environment ...'
# FIXME: Chromium fails to detect the correct LLVM/Clang prefix unless the
# the full absolute path to the compiler is passed with CC/CXX.
export CC=$(readlink -f $(which clang-22))
export CXX=$(readlink -f $(which clang++-22))
RUSTC_VERSION="$(rustc --version | cut -d' ' -f2)"
CLANG_VERSION="$(clang-22 --version | sed -n 's/clang version //p' | cut -d. -f1)"

abinfo 'Setting $GNFLAGS ...'
GNFLAGS=(
    'google_api_key="AIzaSyBGpe01okUSW2GQDIgLovY23Mj1RKzzOOY"'
    'custom_toolchain="//build/toolchain/linux/unbundle:default"'
    'host_toolchain="//build/toolchain/linux/unbundle:default"'
    'is_clang=true'
    'clang_base_path="/usr"'
    'clang_use_chrome_plugins=false'
    # AOSC LLVM installs compiler-rt in lib/<os>, not per-target directories.
    'clang_use_per_target_runtime_dir=false'
    "clang_version=\"$CLANG_VERSION\""
    'chrome_pgo_phase=0'
    'is_debug=false'
    'use_debug_fission=false'
    'symbol_level=1'
    'is_official_build=true'
    'fatal_linker_warnings=false'
    'treat_warnings_as_errors=false'
    'disable_fieldtrial_testing_config=true'
    'ffmpeg_branding="Chrome"'
    'proprietary_codecs=true'
    'link_pulseaudio=true'
    'use_cups=true'
    'use_sysroot=false'
    'enable_hangout_services_extension=true'
    'enable_widevine=true'
    'use_vaapi=true'
    'use_cfi_icall=false'
    'use_ozone=true'
    'use_qt5=true'
    'use_system_libffi=true'
    'is_cfi=false'
    'ozone_platform_wayland=true'
    'ozone_platform_x11=true'
    'ozone_auto_platforms=true'
    "rustc_version=\"$RUSTC_VERSION\""
    'rust_sysroot_absolute="/usr"'
    'rust_bindgen_root="/usr"'
    'use_lld=true'
)

if ab_match_arch loongarch64; then
    abinfo 'Enabling LSX on LoongArch64 in $GNFLAGS …'
    GNFLAGS+=('loongarch64_use_lsx=true')
fi

abinfo "Running gn to generate build rules ..."
gn gen "$SRCDIR"/out/Release \
    --args="${GNFLAGS[*]}" \
    --script-executable=/usr/bin/python3

abinfo "Building Chromium ..."
ninja \
    --verbose \
    -C "$SRCDIR"/out/Release \
    chrome chrome_sandbox chromedriver || exit 10

abinfo "Installing Chromium ..."
install -Dvm755 "$SRCDIR"/out/Release/chrome \
    "$PKGDIR"/usr/lib/chromium/chromium
cp -av \
    "$SRCDIR"/out/Release/{chrome_{100,200}_percent,resources}.pak \
    "$SRCDIR"/out/Release/{*.bin,chromedriver} \
    "$SRCDIR"/out/Release/locales \
    "$SRCDIR"/out/Release/icudtl.dat \
    "$SRCDIR"/out/Release/chrome_crashpad_handler \
    "$SRCDIR"/out/Release/libqt5_shim.so \
    "$SRCDIR"/out/Release/libvk_swiftshader.so \
    "$SRCDIR"/third_party/swiftshader/src/Vulkan/vk_swiftshader_icd.json \
    "$SRCDIR"/out/Release/libEGL.so \
    "$SRCDIR"/out/Release/libGLESv2.so \
    "$PKGDIR"/usr/lib/chromium/
mkdir -pv "$PKGDIR"/usr/bin
ln -sv /usr/lib/chromium/chromedriver \
    "$PKGDIR"/usr/bin/chromedriver

abinfo "Installing .desktop entry ..."
install -Dvm644 "$SRCDIR"/chrome/installer/linux/common/desktop.template \
    "$PKGDIR/usr/share/applications/chromium.desktop"

abinfo "Installing man page ..."
install -Dvm644 "$SRCDIR"/chrome/app/resources/manpage.1.in \
    "$PKGDIR/usr/share/man/man1/chromium.1"
sed -i \
    -e "s/@@MENUNAME@@/Chromium/g" \
    -e "s/@@PACKAGE@@/chromium/g" \
    -e "s/@@USR_BIN_SYMLINK_NAME@@/chromium/g" \
    "$PKGDIR/usr/share/applications/chromium.desktop" \
    "$PKGDIR/usr/share/man/man1/chromium.1"

abinfo "Installing icons ..."
for size in 24 48 64 128 256; do
    install -Dvm644 "$SRCDIR"/chrome/app/theme/chromium/product_logo_$size.png \
        "$PKGDIR"/usr/share/icons/hicolor/${size}x${size}/apps/chromium.png
done
for size in 16 32; do
    install -Dvm644 "$SRCDIR"/chrome/app/theme/default_100_percent/chromium/product_logo_$size.png \
        "$PKGDIR"/usr/share/icons/hicolor/${size}x${size}/apps/chromium.png
done

# abinfo "Creating symlinks to essential GL runtime ..."
# ln -sv ../libEGL.so \
#     "$PKGDIR"/usr/lib/chromium/libEGL.so
# ln -sv ../libGLESv2.so \
#    "$PKGDIR"/usr/lib/chromium/libGLESv2.so

(
    cd "$SRCDIR"/../chromium-launcher-*

    abinfo "Building chromium-launcher ..."
    make PREFIX=/usr

    abinfo "Installing chromium-launcher ..."
    make install \
        DESTDIR="$PKGDIR" \
        PREFIX=/usr
)
