#!/usr/bin/python3
"""
Slackware ARM: add a Linux_aarch64-gcc3 entry to Firefox's bundled
Widevine manifest. Run from the top of the Firefox source tree.

Mozilla's update server offers no Widevine for aarch64 Linux, so
media.gmp-widevinecdm.forceInstall seeds the plugin from this entry, and
media.gmp-widevinecdm.force-chromium-update then replaces the URL and
version with whatever Google's component updater currently serves.

The values below are only a seed: they never need updating. They are
used directly only if Google's redirect fails.
"""
import json
import sys
from pathlib import Path

TARGET = "Linux_aarch64-gcc3"
SEED = {
    "fileUrl":
        "https://dl.google.com/release2/chrome_component/"
        "acahc54oqallc53nnj2msku6d3da_4.10.3112.0/"
        "oimompecagnajdejgnnjijobebaeigek_4.10.3112.0_"
        "linux_arm64_hb2er34y3mmnehk3ubgy64yql4.crx3",
    "mirrorUrls": [
        "https://www.google.com/dl/release2/chrome_component/"
        "acahc54oqallc53nnj2msku6d3da_4.10.3112.0/"
        "oimompecagnajdejgnnjijobebaeigek_4.10.3112.0_"
        "linux_arm64_hb2er34y3mmnehk3ubgy64yql4.crx3",
    ],
    "filesize": 20046647,
    "hashValue":
        "27cdf2b8d8af0c6f96a7640c0fe59c1068ca4795888955ee0bb5b84d012446d9"
        "859f2b248505b1119561086721e6c68da83a538619030985b0819be999f1f270",
}


def die(msg):
    print(f"edit-widevine-manifest: {msg}", file=sys.stderr)
    sys.exit(1)


p = Path("toolkit/content/gmp-sources/widevinecdm.json")
if not p.is_file():
    die(f"{p} not found; run from the top of the Firefox source tree")

data = json.loads(p.read_text())
try:
    platforms = data["vendors"]["gmp-widevinecdm"]["platforms"]
except KeyError as e:
    die(f"manifest layout changed upstream (missing key {e})")

if TARGET in platforms:
    print(f"{TARGET} already present upstream; leaving it alone")
    sys.exit(0)

platforms[TARGET] = SEED
p.write_text(json.dumps(data, indent=2) + "\n")
print(f"added {TARGET} seed entry to {p}")

