#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
distdir=/var/tmp/dist
builddir=/var/tmp/build/[% project %]
mkdir $distdir/[% project %]
mkdir /var/tmp/build

tar -C $GRADLE_HOME -xf $rootdir/[% c('input_files_by_name/gradle') %]

cat > get-moz-build-date << "EOF"
[% INCLUDE "get-moz-build-date" %]
EOF

[% IF !c("var/fetch_gradle_dependencies") %]
  [% pc('python', 'var/setup', { python_tarfile => c('input_files_by_name/python') }) %]
  tar -C $distdir -xf [% c('input_files_by_name/geckoview') %]
  gradle_repo=$rootdir/[% c('input_files_by_name/gradle-dependencies') %]
  # This overwrites the release, beta, and nightly geckoview .aar files in our
  # gradle-dependencies directory to be sure that only the one we provide is
  # getting used when building beta/release code.
  find $gradle_repo -type f -name geckoview*.aar -exec cp -f $distdir/geckoview/geckoview*.aar {} \;
  cp -r $gradle_repo/dl/android/maven2/* $gradle_repo
  cp -r $gradle_repo/maven2/* $gradle_repo
  cp -r $gradle_repo/m2/* $gradle_repo
  # Put the Kotlin/Native compiler at the right place, see:
  # tor-browser-build#40217.
  kotlin_dir=/home/rbm/.konan
  mkdir $kotlin_dir
  find $gradle_repo -type f -name kotlin-native-prebuilt-linux*tar.gz -exec tar -C $kotlin_dir -xaf {} \;
  tar -C $distdir -xf [% c('input_files_by_name/application-services') %]
  # XXX: We could be smarter and just copy over the projects that are needed
  # according to the Gradle dependencies list.
  cp -rf $distdir/application-services/maven/org $gradle_repo
[% END %]
tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.gz

cd $builddir-[% c('version') %]
patch -p1 < $rootdir/git.patch
[% IF c("var/fetch_gradle_dependencies") %]
  # XXX: fetching deps for `assembleGeckoBeta -x lint` by using that same target
  # results in some missing dependencies for yet unknown reasons. Thus, we use
  # `assemble` instead for now.
  gradle --debug --no-daemon assemble > gradle.log 2>&1 || ( cat gradle.log; exit 1 )
  $rootdir/gen_gradle_deps_file.sh gradle.log
  mv gradle-dependencies-list.txt '[% dest_dir _ "/" _ c("filename") %]'
[% ELSE %]
  # Prepare Glean dependencies for offline build
  tar -xf $rootdir/[% c('input_files_by_name/glean') %]
  # We need to set `LC_ALL` and `LANG` to something that is not ASCII as encoding
  # otherwise `click` barfs. See: https://click.palletsprojects.com/python3/
  export LC_ALL=C.UTF-8
  export LANG=C.UTF-8
  patch -p1 < $rootdir/mavenLocal.patch

  eval $(perl $rootdir/get-moz-build-date [% pc("fenix", "var/copyright_year") %] [% pc("fenix", "var/torbrowser_version") %])
  if [ -z $MOZ_BUILD_DATE ]
  then
    echo "MOZ_BUILD_DATE is not set"
    exit 1
  fi

  # We want to make sure only our code is built. Overwrite engine-gecko code we
  # don't need.
  cd components/browser
  [% IF c("var/release") %]
    rm -rf engine-gecko-beta/src/*
    rm -rf engine-gecko-nightly/src/*
    cp -rf engine-gecko/src/* engine-gecko-beta/src/
    cp -rf engine-gecko/src/* engine-gecko-nightly/src/
  [% ELSE %]
    rm -rf engine-gecko/src/*
    rm -rf engine-gecko-nightly/src/*
    cp -rf engine-gecko-beta/src/* engine-gecko/src/
    cp -rf engine-gecko-beta/src/* engine-gecko-nightly/src/
  [% END %]
  cd ../../
  gradle_args="--offline --no-daemon -Dmaven.repo.local=$gradle_repo"

  gradle $gradle_args assembleGecko[% c('variant') %] -x lint
  gradle $gradle_args publish

  # We only need the archives and .pom files for now.
  find components -regex '.*[% c('var/android_components_version') %].\(aar\|jar\|pom\)' -exec cp --parents {} $distdir \;

  # Package
  cd $distdir
  # Package just the maven directory structure for easier replacement of
  # Mozilla's android-components.
  find components -type d -name maven -exec cp -rf {} [% project %] \;
  [% c('tar', {
        tar_src => [ project ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
    }) %]
[% END %]
