#!/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/[% project %]
builddir=/var/tmp/build/[% project %]
mkdir $distdir
mkdir /var/tmp/build

tar -C $GRADLE_HOME -xf $rootdir/[% c('input_files_by_name/gradle') %]
cd /var/tmp/dist
[% FOREACH arch = ['armv7', 'aarch64', 'x86', 'x86_64'] -%]
  tar -xf $rootdir/[% c('input_files_by_name/nss-' _ arch) %]
  mv nss nss-[% arch %]
  tar -xf $rootdir/[% c('input_files_by_name/sqlcipher-' _ arch) %]
  mv sqlcipher sqlcipher-[% arch %]
[% END -%]
tar -xf $rootdir/[% c('input_files_by_name/rust') %]
tar -xf $rootdir/[% c('input_files_by_name/uniffi-rs') %]
export PATH=/var/tmp/dist/rust/bin:/var/tmp/dist/uniffi-rs:$PATH
cd $rootdir

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

[% IF !c('var/fetch_gradle_dependencies') %]
  [% pc('python', 'var/setup', { python_tarfile => c('input_files_by_name/python') }) %]
  gradle_repo=$rootdir/[% c('input_files_by_name/gradle-dependencies') %]
  cp -r $gradle_repo/dl/android/maven2/* $gradle_repo
  cp -r $gradle_repo/m2/* $gradle_repo
  cp -r $gradle_repo/maven2/* $gradle_repo
[% END %]

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

# Prepare the offline build.
# Move the directory for hardcoding the path in .cargo/config.
mv /var/tmp/build/[% project %]-[% c('version') %] $builddir
tar -C $builddir -xjf application-services-vendor-[% c('var/rust_vendor_version') %].tar.bz2
cd $builddir
# Make sure our vendored crates are used for offline builds.
cat >> .cargo/config << 'EOF'

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

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

[net]
offline=true
EOF

# Bug 40293: Patch vendored uniffi_bindgen.
pushd /var/tmp/build/application-services/vendor/
patch -p1 < $rootdir/0001-Store-the-universe-of-known-types-in-an-ordered-BTre.patch
# Update the expected checksum in the vendored source.
sed -i \
    's/e39e61458d0d28c2b3425852c8a55625cc185ce806fbc7c0c6682f8cc93dec8d/0909e5214eedf1f2e709909176b1cbda39887002b26388535c3bc551fb04da1b/' \
    uniffi_bindgen/.cargo-checksum.json
popd

patch -p1 < $rootdir/update-cargo-lock.patch
cd components/external/nimbus-sdk
patch -p1 < $rootdir/viaduct-workaround.patch
cd ../../../

# Move NSS and SQLCipher to the right place
# XXX: Maybe merge with the loop above.
archs="armv7 aarch64 x86 x86_64"
for a in $archs
do
  if [ "$a" == "armv7" ]
  then
    mkdir -p libs/android/armeabi-v7a
    mv /var/tmp/dist/nss-$a libs/android/armeabi-v7a/nss
    mv /var/tmp/dist/sqlcipher-$a libs/android/armeabi-v7a/sqlcipher
  elif [ "$a" == "aarch64" ]
  then
    mkdir -p libs/android/arm64-v8a
    mv /var/tmp/dist/nss-$a libs/android/arm64-v8a/nss
    mv /var/tmp/dist/sqlcipher-$a libs/android/arm64-v8a/sqlcipher
  else
    mkdir -p libs/android/$a
    mv /var/tmp/dist/nss-$a libs/android/$a/nss
    mv /var/tmp/dist/sqlcipher-$a libs/android/$a/sqlcipher
  fi
done

patch -p1 < $rootdir/no-git.patch
# XXX: We don't build the code for Linux for now as this involves additional
# complexity. That's needed for running unit tests later on, though.
patch -p1 < $rootdir/target.patch
[% 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 %]
  # Prepare Glean dependencies for offline build
  tar -xjf $rootdir/glean-parser-[% c('var/glean_parser') %].tar.bz2
  # 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
  gradle_flags="--offline --no-daemon -Dmaven.repo.local=$gradle_repo"
  gradle $gradle_flags assembleRelease
  gradle $gradle_flags publish

  cd build
  find maven -regex '.*[0-9].\(aar\|pom\)' -exec cp --parents {} $distdir \;

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