#!/bin/bash
[% c("var/set_default_env") -%]
assets_dir=assets
ext_dir=$assets_dir/extensions
qa_apk=[% dest_dir %]/[% c('filename') %]/tor-browser-[% c("version") %]-[% c("var/osname") %]-multi-qa.apk
apk=$rootdir/fenix/app-[% c("var/abi") %]-*.apk
test_out_apk=[% dest_dir %]/[% c('filename') %]/tor-browser-[% c("version") %]-[% c("var/osname") %]-androidTest.apk
test_in_apk=$rootdir/fenix/app-*-androidTest.apk

tar xavf $rootdir/[% c('input_files_by_name/fenix') %]

# Bundle our extensions. We treat HTTPS-Everywhere and NoScript differently as
# the former should be an internal extension without getting any updates besides
# from Tor Browser releases. NoScript will be copied over to the profile folder
# being a "regular" browser extension.
mkdir -p /var/tmp/build/$ext_dir [% dest_dir _ '/' _ c('filename') %]
cd /var/tmp/build
unzip -d $ext_dir/https-everywhere $rootdir/[% c('input_files_by_name/https-everywhere') %]
mv $rootdir/[% c('input_files_by_name/noscript') %] $ext_dir/{73a6fe31-595d-460b-a920-fcc0f8843232}.xpi

[% IF c("var/verify_allowed_addons") %]
  # Check that allowed_addons.json contains the right versions of NoScript and HTTPS Everywhere
  # If so, replace the default allowed_addons.json by ours in the apk assets folder.
  $rootdir/verify_allowed_addons.py "$rootdir/allowed_addons.json" "$ext_dir/{73a6fe31-595d-460b-a920-fcc0f8843232}.xpi" "$rootdir/[% c('input_files_by_name/https-everywhere') %]"
[% END %]

mv $rootdir/allowed_addons.json $assets_dir/allowed_addons.json

[% c('zip', {
        zip_src => [ '$assets_dir' ],
        zip_args => '$apk',
    }) %]

# Sign a QA build. This .apk is not a debug version and doesn't contain a debug
# flag in the manifest.
java -jar /usr/lib/android-sdk/build-tools/debian/apksigner.jar sign --verbose --min-sdk-version [% c("var/android_min_api") %] --ks $rootdir/android-qa.keystore --out $qa_apk --in $apk --ks-key-alias androidqakey --key-pass pass:android --ks-pass pass:android

java -jar /usr/lib/android-sdk/build-tools/debian/apksigner.jar sign --verbose --min-sdk-version [% c("var/android_min_api") %] --ks $rootdir/android-qa.keystore --out $test_out_apk --in $test_in_apk  --ks-key-alias androidqakey --key-pass pass:android --ks-pass pass:android
