#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
SDK="${ANDROID_SDK_ROOT:-${ANDROID_HOME:-}}"
KOTLIN_HOME="${KOTLIN_HOME:-$HOME/.sdkman/candidates/kotlin/current}"
BUILD_TOOLS="${BUILD_TOOLS:-35.0.0}"
PLATFORM="${PLATFORM:-android-35}"

[[ -n "$SDK" ]] || { echo "Set ANDROID_SDK_ROOT (or ANDROID_HOME)." >&2; exit 1; }
[[ -d "$KOTLIN_HOME" ]] || { echo "Set KOTLIN_HOME to a Kotlin compiler distribution." >&2; exit 1; }
BT="$SDK/build-tools/$BUILD_TOOLS"
AJ="$SDK/platforms/$PLATFORM/android.jar"

if [[ ! -d "$BT" ]]; then
  BUILD_TOOLS="$(find "$SDK/build-tools" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort -V | tail -1)"
  BT="$SDK/build-tools/$BUILD_TOOLS"
fi
if [[ ! -f "$AJ" ]]; then
  PLATFORM="$(find "$SDK/platforms" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort -V | tail -1)"
  AJ="$SDK/platforms/$PLATFORM/android.jar"
fi

for tool in aapt d8 apksigner; do
  [[ -x "$BT/$tool" ]] || { echo "Missing $BT/$tool" >&2; exit 1; }
done
[[ -f "$AJ" ]] || { echo "Android platform android.jar not found" >&2; exit 1; }
[[ -x "$KOTLIN_HOME/bin/kotlinc" ]] || { echo "kotlinc not found under KOTLIN_HOME" >&2; exit 1; }
CORO="$KOTLIN_HOME/lib/kotlinx-coroutines-core-jvm.jar"
[[ -f "$CORO" ]] || { echo "Missing $CORO (install a Kotlin distribution that includes kotlinx-coroutines-core-jvm.jar)" >&2; exit 1; }
[[ -f "$ROOT/lib/arm64-v8a/libdragontcp_client.so" ]] || { echo "Run ../build_core.sh first" >&2; exit 1; }

B="$ROOT/build"
rm -rf "$B"
mkdir -p "$B/kclasses" "$B/jclasses" "$B/dex"

echo "[apk] Resources..."
"$BT/aapt" package -f -M "$ROOT/AndroidManifest.xml" -S "$ROOT/res" -A "$ROOT/assets" -I "$AJ" -F "$B/resources.ap_"

echo "[apk] Kotlin TUN adapter..."
CP="$AJ:$CORO"
find "$ROOT/src" -name '*.kt' -print > "$B/kotlin-sources.txt"
"$KOTLIN_HOME/bin/kotlinc" -jvm-target 1.8 -classpath "$CP" -d "$B/kclasses" @"$B/kotlin-sources.txt"

echo "[apk] Java UI/service..."
JCP="$CP:$B/kclasses:$KOTLIN_HOME/lib/kotlin-stdlib.jar:$KOTLIN_HOME/lib/kotlin-stdlib-jdk7.jar:$KOTLIN_HOME/lib/kotlin-stdlib-jdk8.jar"
javac -source 8 -target 8 -classpath "$JCP" -d "$B/jclasses" $(find "$ROOT/src" -name '*.java' -print)
jar cf "$B/kclasses.jar" -C "$B/kclasses" .
jar cf "$B/jclasses.jar" -C "$B/jclasses" .

echo "[apk] DEX..."
"$BT/d8" --lib "$AJ" --min-api 29 --output "$B/dex" \
  "$B/kclasses.jar" "$B/jclasses.jar" \
  "$KOTLIN_HOME/lib/kotlin-stdlib.jar" \
  "$KOTLIN_HOME/lib/kotlin-stdlib-jdk7.jar" \
  "$KOTLIN_HOME/lib/kotlin-stdlib-jdk8.jar" \
  "$CORO"

cp "$B/resources.ap_" "$B/DragonTCP-Hybrid-unsigned.apk"
(cd "$B/dex" && zip -q "$B/DragonTCP-Hybrid-unsigned.apk" classes*.dex)
(cd "$ROOT" && zip -q -r "$B/DragonTCP-Hybrid-unsigned.apk" lib)

KEYSTORE="${KEYSTORE:-$ROOT/dragontcp-lite-debug.jks}"
KS_PASS="${KS_PASS:-dragontcp}"
KEY_ALIAS="${KEY_ALIAS:-dragontcp}"
KEY_PASS="${KEY_PASS:-dragontcp}"
if [[ ! -f "$KEYSTORE" ]]; then
  keytool -genkeypair -keystore "$KEYSTORE" -storepass "$KS_PASS" -keypass "$KEY_PASS" \
    -alias "$KEY_ALIAS" -keyalg RSA -keysize 2048 -validity 10000 \
    -dname 'CN=DragonTCP Lite,O=DragonTCP,C=US'
fi

"$BT/apksigner" sign --ks "$KEYSTORE" --ks-pass "pass:$KS_PASS" --key-pass "pass:$KEY_PASS" \
  --out "$B/DragonTCP-Hybrid-arm64.apk" "$B/DragonTCP-Hybrid-unsigned.apk"
"$BT/apksigner" verify --verbose "$B/DragonTCP-Hybrid-arm64.apk"
echo "APK: $B/DragonTCP-Hybrid-arm64.apk"
