#!/bin/sh
set -eu

umask 077

ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
workspace=$(mktemp -d "${TMPDIR:-/tmp}/wnh-android-release-ci.XXXXXX")
signing_dir="$workspace/signing"
environment_file="$workspace/release.env"
output_dir="$workspace/output"

cleanup() {
  rm -rf "$workspace"
}
trap cleanup EXIT HUP INT TERM

printf '%s\n' \
  'WNH_BASE_URL=https://android-release-ci.invalid' \
  'WNH_TRACKING_MIN_TIME_MS=5000' \
  'WNH_TRACKING_HTTP_TIMEOUT_MS=15000' \
  'WNH_ANDROID_VERSION_CODE=1' \
  'WNH_ANDROID_VERSION_NAME=0.1.0-ci' \
  'WNH_ANDROID_SIGNING_KEY_ALIAS=who-need-help-ci-upload' \
  >"$environment_file"
chmod 600 "$environment_file"

WNH_ANDROID_SIGNING_DIR="$signing_dir" \
WNH_ANDROID_SIGNING_KEY_ALIAS=who-need-help-ci-upload \
  "$ROOT/scripts/init-android-release-signing.sh" >/dev/null

WNH_ANDROID_SIGNING_DIR="$signing_dir" \
WNH_ANDROID_RELEASE_OUTPUT_DIR="$output_dir" \
WNH_ENV_FILE="$environment_file" \
  "$ROOT/scripts/android-release-build.sh"

echo "Ephemeral signed Android release pipeline passed."
