#!/bin/bash
# OTA manifest 生成脚本
# 用法: ./ota/gen-manifest.sh <versionName> <versionCode> <apk-path>
# 构建号规则: 每次生成APK时 versionCode +1, 对应 git tag vX.Y.Z-dav-android-N
# CI 构建时自动运行, SHA256动态填入, 生成 manifest-stable.json 后上传到 Gitea Release

VERSION_NAME="${1:-0.1.0}"
VERSION_CODE="${2:-7}"
APK_PATH="${3:-packages/client-android/android/app/build/outputs/apk/debug/v0.1.0-dav-android-7-debug.apk}"
RELEASE_TAG="v${VERSION_NAME}-dav-android-${VERSION_CODE}"
APK_NAME="v${VERSION_NAME}-dav-android-${VERSION_CODE}-debug.apk"

SHA256=$(sha256sum "$APK_PATH" | cut -d' ' -f1)
SIZE=$(stat -c%s "$APK_PATH")

cat > ota/manifest-stable.json <<JSON
{
  "version": "${VERSION_NAME}",
  "builds": {
    "android": {
      "type": "apk",
      "tag": "${RELEASE_TAG}",
      "url": "https://git.childish-ghost.com/LukeMackin/Yuzu-GCA/releases/download/${RELEASE_TAG}/${APK_NAME}",
      "sha256": "${SHA256}",
      "size": ${SIZE},
      "channel": "stable",
      "minVersion": "${VERSION_NAME}"
    }
  }
}
JSON

echo "Generated: ota/manifest-stable.json"
echo "  tag:    ${RELEASE_TAG}"
echo "  sha256: ${SHA256}"
echo "  size:   ${SIZE}"
echo ""
echo "Upload: curl -X POST 'https://git.childish-ghost.com/api/v1/repos/LukeMackin/Yuzu-GCA/releases/\${RELEASE_ID}/assets?name=manifest-stable.json' -H 'Authorization: token \$TOKEN' -H 'Content-Type: application/json' --data-binary @ota/manifest-stable.json"
