default_pre_build &&

# Allow building against system libraries in official builds
sed -i 's/OFFICIAL_BUILD/GOOGLE_CHROME_BUILD/' tools/generate_shim_headers/generate_shim_headers.py &&

# https://crbug.com/893950
  sed -i -e 's/\<xmlMalloc\>/malloc/' -e 's/\<xmlFree\>/free/' \
    third_party/blink/renderer/core/xml/*.cc \
    third_party/blink/renderer/core/xml/parser/xml_document_parser.cc \
    third_party/libxml/chromium/*.cc \
    third_party/maldoca/src/maldoca/ole/oss_utils.h &&

# Build now requires nodejs
mkdir -p third_party/node/linux/node-linux-x64/bin &&
ln -s /usr/bin/node third_party/node/linux/node-linux-x64/bin/node &&
ln -s /usr/bin/java third_party/jdk/current/bin/ &&

# Google API keys (see http://www.chromium.org/developers/how-tos/api-keys)
# The keys below are for Lunar Linux use ONLY. If you are maintaining chromium for
# another distribution, please get your own set of keys.
google_api_key=AIzaSyDhObDIVsBGV5fleLgLq6ZpfKldIVMvrG4 &&
google_default_client_id=1081365520390-t7vsmo6kde87fvbjoue2ea412vsjkvu0.apps.googleusercontent.com &&
google_default_client_secret=mz6fvViZFucQ5QVggWaqOjYX &&

# Remove bundled libraries, we will use system provided ones instead (using arch's approach)
local _libs &&
_libs=(brotli dav1d fontconfig freetype flac jsoncpp libaom libavif libpng woff2 libxml libxslt libwebp re2 icu opus zlib snappy harfbuzz-ng) &&
for _lib in ${_libs[@]}; do
  find "third_party/$_lib" -type f \
       \! -path "third_party/$_lib/chromium/*" \
       \! -path "third_party/$_lib/google/*" \
       \! -path "third_party/yasm/run_yasm.py" \
       \! -path "third_party/harfbuzz-ng/utils/hb_scoped.h" \
       \! -regex '.*\.\(gn\|gni\|isolate\)' \
       -delete
done &&

./build/linux/unbundle/replace_gn_files.py --system-libraries "${_libs[@]}" &&

pushd third_party/ffmpeg &&
chromium/scripts/build_ffmpeg.py linux x64 --branding Chrome
chromium/scripts/copy_config.sh
chromium/scripts/generate_gn.py
popd &&

export CC=clang &&
export CXX=clang++ &&
export AR=llvm-ar &&
export NM=nm &&

#gnome_keyring is depreciated hence its set to false below.
#pseudolocales is only for testing, set to false
#If compiling on AMD then add devtools_skip_typecheck=false, don't have such
#a system to test.
local _flags=(
  'clang_base_path="/usr"'
  'chrome_pgo_phase=0'
  'blink_symbol_level=0'
  'is_debug=false'
  'is_component_build=false'
  'is_official_build=true'
  'v8_symbol_level=0'
  'custom_toolchain="//build/toolchain/linux/unbundle:default"'
  'host_toolchain="//build/toolchain/linux/unbundle:default"'
  'icu_use_data_file=false'
  'clang_use_chrome_plugins=false'
  'treat_warnings_as_errors=false'
  'fatal_linker_warnings=false'
  'ffmpeg_branding="Chrome"'
  'proprietary_codecs=true'
  'use_gnome_keyring=false'
  'use_system_libffi=true'
  'use_sysroot=false'
  'use_gold=false'
  'use_custom_libcxx=false'
  'disable_fieldtrial_testing_config=true'
  'blink_enable_generated_code_formatting=false'
  'enable_hangout_services_extension=true'
  'enable_widevine=true'
  'enable_pseudolocales=false'
  'enable_nacl=false'
  'gtk_version=3'
  'symbol_level=0'
  "google_api_key=\"${google_api_key}\""
  "google_default_client_id=\"${google_default_client_id}\""
  "google_default_client_secret=\"${google_default_client_secret}\""
) &&

for o in $OPTS; do
  _flags+=( "$o" )
done &&

if module_installed qt5; then
  source /etc/profile.d/qt5.rc &&
  OPTS+=" --enable-features=AllowQ"
fi &&

export CHROMIUM_HOME=/usr/lib/$MODULE &&

gn gen out/Release --args="${_flags[*]}"
