#!/bin/bash

# system

SRC=(
system/lineage/waydroid_x86_64/VANILLA.json
system/lineage/waydroid_x86_64/GAPPS.json
vendor/waydroid_x86_64/MAINLINE.json
)

for CHAN in "${SRC[@]}"; do
  echo "Processing $CHAN..."
  eval $(curl -s "https://raw.githubusercontent.com/waydroid/OTA/master/$CHAN" | jq -r '.response | .[0] | "DATE=\(.datetime) FILENAME=\"\(.filename)\" ROMTYPE=\(.romtype) HASH=\(.id) URL=\"\(.url)\" VERSION=\(.version) SIZE=\(.size)"')

  BASE_PATH="$(dirname "$CHAN")"
  FILE="$BASE_PATH/$FILENAME"
  MANI="$BASE_PATH/$(basename "$CHAN")"
  DLFL="https://files.api.morgan.kr/waydroid/$FILE"

  if [ -f "$FILE" ]; then
    echo "Skipping $FILE"
    continue
  fi
  echo "Downloading $FILE..."

  mkdir -p "$BASE_PATH"
  curl -sL "$URL" | pv > "$FILE"

  SIZE="$(stat -c %s "$FILE")"

  cat << EOF > "$MANI"
{
  "response": [
    {
      "datetime": $DATE,
      "filename": "$FILENAME",
      "id": "$HASH",
      "romtype": "$ROMTYPE",
      "size": $SIZE,
      "url": "$DLFL",
      "version": "$VERSION"
    }
  ]
}
EOF

done
