#!/bin/bash

# === Configuration ===
INPUT_FOLDER="games"
ICON_FOLDER="icons/games"
OUTPUT_FILE="output.json"
> "$OUTPUT_FILE"

# === Generate JSON entries ===
first=1
for file in "$INPUT_FOLDER"/*.zip; do
    [ -e "$file" ] || continue  # Skip if no .zip files found

    filename=$(basename -- "$file")
    name="${filename%.zip}"
    img="${ICON_FOLDER}/${name}.png"
    href="${INPUT_FOLDER}/${filename}"
    desc="$filename"

    # Append JSON block with comma if needed
    if [ $first -eq 0 ]; then
        echo "        }," >> "$OUTPUT_FILE"
    fi
    first=0

    # Start JSON object
    cat >> "$OUTPUT_FILE" <<EOF
        {
            "name":"$name",
            "href":"$href",
            "img":"$img",
            "desc":"$desc",
            "version":""
EOF
done

# Close the last object if at least one file was processed
if [ $first -eq 0 ]; then
    echo "        }" >> "$OUTPUT_FILE"
fi

echo "JSON output written to $OUTPUT_FILE"
