#!/usr/bin/env bash
# generate-release-keystore.sh — one-time setup for SIGNED Android releases.
#
# RUN THIS ONCE. After this, every APK you build is signed with the same
# key, so users can install the new version OVER the previous one without
# the "App not installed as package conflicts" error.
#
# WARNING: BACK UP THE GENERATED .jks FILE! If you ever lose it you will
# never be able to publish an update — every device will need to uninstall
# the old version first. Keep a copy outside this repo (cloud, USB, etc.).
#
# Usage:
#   ./scripts/generate-release-keystore.sh
#
# Or non-interactive (CI):
#   STORE_PASS=xxx KEY_PASS=xxx CN="Nadiplayer" ./scripts/generate-release-keystore.sh

set -euo pipefail

REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
ANDROID_DIR="$REPO_ROOT/frontend/android"
KEYSTORE="$ANDROID_DIR/release-keystore.jks"
PROPS="$ANDROID_DIR/keystore.properties"

if [ -f "$KEYSTORE" ]; then
  echo
  echo "❌ Refusing: $KEYSTORE already exists."
  echo "   If you regenerate the keystore, NO existing user will be able to update."
  echo "   They will all see 'App not installed' and have to uninstall first."
  echo
  echo "   If you really need a new key (e.g. you lost the old one) delete the"
  echo "   existing file manually, accept that fact, and re-run this script."
  exit 1
fi

if ! command -v keytool >/dev/null 2>&1; then
  echo "❌ keytool is missing. Install a JDK first (e.g. 'apt install default-jdk')." >&2
  exit 2
fi

# Defaults (all overridable via env)
ALIAS="${ALIAS:-nadiplayer}"
STORE_PASS="${STORE_PASS:-}"
KEY_PASS="${KEY_PASS:-}"
CN="${CN:-Nadiplayer}"
OU="${OU:-Mobile}"
O="${O:-Nadi Studio}"
L="${L:-Berlin}"
ST="${ST:-Berlin}"
C="${C:-DE}"

# Prompt for passwords if not supplied
if [ -z "$STORE_PASS" ]; then
  read -srp "Choose a STORE password (>=6 chars, you'll need this every release): " STORE_PASS; echo
fi
if [ -z "$KEY_PASS" ]; then
  KEY_PASS="$STORE_PASS"   # simplest: use same password
fi

if [ "${#STORE_PASS}" -lt 6 ]; then
  echo "❌ Password must be at least 6 characters." >&2
  exit 3
fi

mkdir -p "$ANDROID_DIR"

echo "🔑 Generating release keystore at $KEYSTORE …"
keytool -genkeypair -v \
  -keystore "$KEYSTORE" \
  -alias "$ALIAS" \
  -keyalg RSA -keysize 2048 -validity 36500 \
  -storepass "$STORE_PASS" -keypass "$KEY_PASS" \
  -dname "CN=$CN, OU=$OU, O=$O, L=$L, ST=$ST, C=$C" >/dev/null

# Write keystore.properties (gitignored — will be picked up by build.gradle)
cat > "$PROPS" <<EOF
# AUTO-GENERATED by scripts/generate-release-keystore.sh — DO NOT COMMIT.
storeFile=release-keystore.jks
storePassword=$STORE_PASS
keyAlias=$ALIAS
keyPassword=$KEY_PASS
EOF
chmod 600 "$PROPS" "$KEYSTORE"

cat <<MSG

✅ Done.

   Keystore:   $KEYSTORE
   Properties: $PROPS

🚨 IMMEDIATELY back these two files up to a SAFE place outside the repo
   (cloud drive, USB stick, password manager). If you lose them, every
   user will have to uninstall and reinstall to get the next update.

Now your normal release routine works:
   ./scripts/bump-version.sh 1.0.3
   cd frontend && yarn cap:sync && cd android && ./gradlew assembleRelease
   # → app/build/outputs/apk/release/app-release.apk  (signed)

Upload that APK to your server and set "Latest version" = 1.0.3 in the
admin panel. Users on 1.0.2 will install it directly — no uninstall needed.
MSG
