#!/bin/bash

# system

for CHAN in VANILLA GAPPS FOSS; do

eval $(curl -s https://raw.githubusercontent.com/waydroid/OTA/refs/heads/master/system/lineage/waydroid_x86_64/$CHAN.json | jq -r '.response | .[0] | "DATE=\(.datetime) FILENAME=\"\(.filename)\" ROMTYPE=\(.romtype) HASH=\(.id) URL=\"\(.url)\" VERSION=\(.version)"')

FILE="./system/lineage/waydroid_x86_64/$FILENAME"
MANI="./system/lineage/waydroid_x86_64/$CHAN.json"

echo "Downloading $FILE..."

curl -sL $URL | pv > $FILE;

URL="/waydroid/system/lineage/waydroid_x86_64/$FILENAME"

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

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

done

# vendor

for CHAN in MAINLINE; do

eval $(curl -s https://raw.githubusercontent.com/waydroid/OTA/refs/heads/master/vendor/waydroid_x86_64/$CHAN.json | jq -r '.response | .[0] | "DATE=\(.datetime) FILENAME=\"\(.filename)\" ROMTYPE=\(.romtype) HASH=\(.id) URL=\"\(.url)\" VERSION=\(.version)"')

FILE="./vendor/waydroid_x86_64/$FILENAME"
MANI="./vendor/waydroid_x86_64/$CHAN.json"

echo "Downloading $FILE..."

curl -sL $URL | pv > $FILE;

URL="/waydroid/vendor/waydroid_x86_64/$FILENAME"

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

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

done
