#!/bin/bash
[% INCLUDE 'build_common' %]

echo "Starting the creation of the fat AAR $(date)"

tar -C $builddir -xf [% c('input_files_by_name/geckoview_armv7') %]
tar -C $builddir -xf [% c('input_files_by_name/geckoview_aarch64') %]
tar -C $builddir -xf [% c('input_files_by_name/geckoview_x86') %]
tar -C $builddir -xf [% c('input_files_by_name/geckoview_x86_64') %]
tar -C $builddir -xf [% project %]-[% c('version') %].tar.[% c('compress_tar') %]

# Specify the architectures we want to merge
export MOZ_ANDROID_FAT_AAR_ARCHITECTURES=armeabi-v7a,arm64-v8a,x86,x86_64
export MOZ_ANDROID_FAT_AAR_ARMEABI_V7A=$builddir/geckoview/*armeabi-v7a*.aar
export MOZ_ANDROID_FAT_AAR_ARM64_V8A=$builddir/geckoview/*arm64-v8a*.aar
# Specifying just "x86" is not differentiating enough
export MOZ_ANDROID_FAT_AAR_X86=$builddir/geckoview/*x86-*.aar
export MOZ_ANDROID_FAT_AAR_X86_64=$builddir/geckoview/*x86_64*.aar

cd $builddir/[% project %]-[% c("version") %]
ln -s mozconfig-android-all .mozconfig
cat >> mozconfig-android-all << 'MOZCONFIG_EOF'
mk_add_options MOZ_PARALLEL_BUILD=[% c("num_procs") %]
export MOZ_INCLUDE_SOURCE_INFO=1
export MOZ_SOURCE_REPO="[% c('var/gitlab_project') %]"
export MOZ_SOURCE_CHANGESET=[% c("var/git_commit") %]

ac_add_options --enable-update-channel=[% c("var/variant") %]
MOZCONFIG_EOF
echo "ac_add_options --with-branding=$branding_dir" >> mozconfig-android-all

# We still need to specify --tor-browser-version due to bug 34005.
./mach configure \
  --with-base-browser-version=[% c("var/torbrowser_version") %] \
  --enable-update-channel=[% c("var/variant") %] \
  [% IF !c("var/rlbox") -%]--without-wasm-sandboxed-libraries[% END %]

./mach build --verbose

[% INCLUDE 'build_ac_fenix' %]

cd $distdir/
[% c('tar', {
    tar_src => [ project ],
    tar_args => '-caf ' _ dest_dir _ '/' _ c('filename'),
  }) %]
