#!/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', 'linux-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.11.0-openjdk-amd64

[% IF !c('var/fetch_gradle_dependencies') %]
  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('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

# Move NSS and SQLCipher to the right place
# XXX: Maybe merge with the loop above.
archs="armv7 aarch64 x86 x86_64 linux-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
  elif [ "$a" == "linux-x86_64" ]
  then
    mkdir -p libs/desktop/linux-x86-64
    mv /var/tmp/dist/nss-$a libs/desktop/linux-x86-64/nss
    mv /var/tmp/dist/sqlcipher-$a libs/desktop/linux-x86-64/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
export RUST_ANDROID_GRADLE_PYTHON_COMMAND=python3
[% 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 -xf $rootdir/[% c('input_files_by_name/glean-wheels') %]
  # 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

  pushd components/support/nimbus-fml
  cargo build --release
  popd

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

  cp target/release/nimbus-fml $distdir

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