#!/bin/bash
[% c("var/set_default_env") -%]

[% pc(c('var/compiler'), 'var/setup', {
    compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')),
    gradle_tarfile => c("input_files_by_name/gradle"),
  }) %]
pushd /var/tmp/dist/android-toolchain/android-sdk-linux/build-tools/
unzip $rootdir/'[% c("input_files_by_name/build_tools_30") %]'
mv android-11 30.0.3
popd

distdir=/var/tmp/dist/[% project %]
builddir=/var/tmp/build/[% project %]
mkdir $distdir
mkdir /var/tmp/build

tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/rust') %]
tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/ninja') %]
export PATH=/var/tmp/dist/rust/bin:/var/tmp/dist/ninja:$PATH
export RUST_ANDROID_GRADLE_PYTHON_COMMAND=python3

export JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64

[% INCLUDE 'fake-git' %]

[% IF !c('var/fetch_gradle_dependencies') %]
  gradle_repo=/var/tmp/dist/gradle-dependencies
  mv $rootdir/[% c('input_files_by_name/gradle-dependencies') %] $gradle_repo
  cp -rl $gradle_repo/dl/android/maven2/* $gradle_repo || true
  cp -rl $gradle_repo/m2/* $gradle_repo || true
  cp -rl $gradle_repo/maven2/* $gradle_repo || true
[% END %]

tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.[% c('compress_tar') %]

mv /var/tmp/build/[% project %]-[% c('version') %] $builddir

cd $builddir

# Prepare the offline build.

tar -xf $rootdir/[% c('input_files_by_name/glean-wheels') %]
export GLEAN_PYTHON_WHEELS_DIR=$builddir/glean-wheels
# Setting GLEAN_PYTHON_WHEELS_DIR is not enough: a Rust build script will still
# try to install Glean with pip.
cat > $rootdir/pip.conf << 'EOF'
[global]
find-links = /var/tmp/build/application-services/glean-wheels
index-url =
no-index = yes
EOF
export PIP_CONFIG_FILE=$rootdir/pip.conf
# Move the directory for hardcoding the path in .cargo/config.
tar -C $builddir -xf $rootdir/[% c('input_files_by_name/cargo_vendor') %]
# Make sure our vendored crates are used for offline builds.
cat >> .cargo/config.toml << 'EOF'

[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "/var/tmp/build/application-services/vendor"

[net]
offline=true
EOF

tar -xf $rootdir/[% c('input_files_by_name/uniffi-rs') %]

# We add a suffix to the version to make it super specific.
# This is useful for developer builds, which seem to try to fetch
# latest versions of dependencies sometimes.
sed -i '$ s/$/-TORBROWSER/' version.txt

cp $rootdir/cargo-no-build.sh megazords/full/android/
chmod +x megazords/full/android/cargo-no-build.sh
patch -p1 < $rootdir/a-s-noop.diff

pushd components/external/glean
cp $rootdir/cargo-no-build.sh glean-core/android-native/
cp $builddir/uniffi-rs/uniffi-bindgen glean-core/android-native/
patch -p1 < $rootdir/glean-noop.diff
popd

pushd libs
ln -s $rootdir/[% c("input_files_by_name/nss") %] ./
ln -s $rootdir/bug_13028.patch
patch -p2 < $rootdir/apply-bug-13028.diff
patch -p2 < $rootdir/no-ndk-lookup.diff
./build-all.sh desktop
./build-all.sh android
popd

[% IF c('var/fetch_gradle_dependencies') %]
  # XXX: `assemble` is still not enough to see all fetched dependencies via
  # Gradle's --debug. See: tor-browser-build#40056.
  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 %]
  # 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/local-repository.diff
  patch -p1 < $rootdir/ohttp-no-git.diff
  patch -p1 < $rootdir/bug40485-nimbus-fml-reproducibility.diff
  patch -p1 < $rootdir/offline-nimbus-fml.diff
  patch -p1 < $rootdir/41422-disable-viaduct.diff
  gradle_flags="--offline --no-daemon"
  gradle $gradle_flags assembleRelease
  gradle $gradle_flags publish
  mv build/maven $distdir

  pushd components/support/nimbus-fml
  cargo build --release
  popd
  cp target/release/nimbus-fml $distdir

  cd /var/tmp/dist
  [% c('tar', {
      tar_src => [ project ],
      tar_args => '-caf ' _ dest_dir _ '/' _ c('filename'),
  }) %]
[% END %]
