#!/bin/bash
# Download CVE JSON data (overwrite mode)
# Usage: els_os_cve_status_daily.sh <output_file>

set -euo pipefail

# Check argument
if [[ $# -lt 1 ]]; then
	  echo "Usage: $0 <output_file>"
	    exit 1
fi

DEST_FILE="$1"

# Download the file quietly, fail if any error
curl -sSf "https://cve.tuxcare.com/els/download-json?orderBy=updated-desc" -o "$DEST_FILE"

# Optional: verify it's valid JSON (if jq is installed)
if command -v jq >/dev/null 2>&1; then
  if ! jq empty "$DEST_FILE" >/dev/null 2>&1; then
    echo "$(date): Invalid JSON downloaded — keeping previous version if any." >&2
    exit 1
  fi
fi

echo "$(date): Successfully updated $DEST_FILE"
