#!/bin/sh
set -eu

ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
target="$ROOT/.env.android-test"

if [ -f "$target" ]; then
  echo ".env.android-test already exists; no setting was changed."
  exit 0
fi

port=$(od -An -N4 -tu4 /dev/urandom | awk '{print 30000 + ($1 % 20000)}')
umask 077
{
  printf 'WNH_ANDROID_TEST_BASE_URL=http://127.0.0.1:%s\n' "$port"
} > "$target"

chmod 600 "$target"
echo "Generated .env.android-test with an isolated device-loopback origin and mode 0600."
