#!/usr/bin/env python
# SPDX-License-Identifier: BSD-3-Clause

import hashlib
import json
import os
import sys

# This script is pretty hard-coded to assuming the three IWADs and
# URLs present as of the Freedoom 0.9 release.  Not much can be done
# about that, but it will have to be changed whenever the project
# expands or switches web host.

iwads = {
    "freedm.wad": {
        "description": "Deathmatch IWAD",
        "name": "FreeDM"},
    "freedoom1.wad": {
        "description": "Ultimate Doom compatible IWAD",
        "name": "Freedoom: Phase 1"},
    "freedoom2.wad": {
        "description": "Doom II compatible IWAD",
        "name": "Freedoom: Phase 2"}}

# Find the version to generate JSON for:

version = os.getenv("VERSION")

# Find the file to append objects to:

json_file = os.getenv("JSON")

if version is None:
    sys.stderr.write("Version is not specified for release\n")
    sys.exit(1)
if version[0] is 'v':
    # Strip the leading 'v' from versioning
    version = version[1:]

if json_file is None:
    sys.stderr.write("JSON file not specified!\n")
    sys.exit(1)

with open("wads/freedoom1.wad", "rb") as f:
    iwads["freedoom1.wad"]["md5"] = hashlib.md5(f.read()).hexdigest()

with open("wads/freedoom2.wad", "rb") as f:
    iwads["freedoom2.wad"]["md5"] = hashlib.md5(f.read()).hexdigest()

with open("wads/freedm.wad", "rb") as f:
    iwads["freedm.wad"]["md5"] = hashlib.md5(f.read()).hexdigest()

iwads["freedoom1.wad"]["url"] = "https://github.com/freedoom/freedoom/releases/download/v" + version + "/freedoom-" + version + ".zip"
iwads["freedoom1.wad"]["version"] = version
iwads["freedoom2.wad"]["url"] = "https://github.com/freedoom/freedoom/releases/download/v" + version + "/freedoom-" + version + ".zip"
iwads["freedoom2.wad"]["version"] = version
iwads["freedm.wad"]["url"] = "https://github.com/freedoom/freedoom/releases/download/v" + version + "/freedm-" + version + ".zip"
iwads["freedm.wad"]["version"] = version

with open(json_file, "w") as f:
    json.dump(iwads, f, indent=2, sort_keys=True)
