#!/bin/sh

set -eu

case $# in
   0)
      FILE=mirrors.json
      ;;
   1)
      FILE="$1"
      ;;
   *)
      exit 1
esac

if command -v python3-jsonschema >/dev/null 2>&1; then
    JSONSCHEMA=python3-jsonschema
elif command -v jsonschema >/dev/null 2>&1; then
    JSONSCHEMA=jsonschema
fi

"$JSONSCHEMA" -i "$FILE" schema.json

# To workaround #12405, make sure that the configuration is smaller than 8192 bytes.
MAX_SIZE=8192
SIZE=$(wc -c < "$FILE")
if [ "$SIZE" -ge "$MAX_SIZE" ]; then
    echo "$FILE too big ($SIZE >= 8192 B). Aborting..."
    exit 1
fi
