#!/usr/bin/env bash
set -euo pipefail

BASE="/maven.wso2.org/nexus/sonatype-work/nexus/storage/wso2gpl"
ATTR_DIR="$BASE/.nexus/attributes"

OUT="${1:-/tmp/nexus_lastRequested_gpl.csv}"

# CSV header
printf 'artifact_path,lastRequested,timestamp_utc\n' > "$OUT"

if command -v jq >/dev/null 2>&1; then
  # jq path: floor seconds to avoid floats like 1.753e+09
  find "$ATTR_DIR" -type f -print0 \
  | xargs -0 -I{} jq -r '
      [
        ."storageItem-path",
        (."storageItem-lastRequested" // ""),
        (
          if (."storageItem-lastRequested")
          then ((."storageItem-lastRequested"|tonumber)/1000 | floor | strftime("%Y-%m-%d %H:%M:%S"))
          else ""
          end
        )
      ] | @csv
    ' {} >> "$OUT"
else
  # awk fallback: strip last 3 digits (ms -> sec) to keep it integer
  while IFS= read -r -d '' f; do
    awk '
      BEGIN { path=""; ts="" }
      {
        if (match($0, /"storageItem-path":"([^"]+)"/, a)) { path=a[1] }
        if (match($0, /"storageItem-lastRequested":"([0-9]+)"/, b)) { ts=b[1] }
      }
      END {
        if (path != "") {
          # Convert ms -> sec by chopping last 3 digits (integer-safe)
          sec=""
          if (ts != "" && length(ts) > 3) {
            sec = substr(ts, 1, length(ts)-3)
          }
          # Build human timestamp only if we have seconds
          human=""
          if (sec != "") {
            cmd = "date -u -d @" sec " +\"%Y-%m-%d %H:%M:%S\""
            cmd | getline human
            close(cmd)
          }
          # Escape quotes for CSV
          gsub(/"/, "\"\"", path)
          gsub(/"/, "\"\"", ts)
          gsub(/"/, "\"\"", human)
          printf "\"%s\",\"%s\",\"%s\"\n", path, ts, human
        }
      }' "$f" >> "$OUT"
  done < <(find "$ATTR_DIR" -type f -print0)
fi

echo "Done. CSV written to: $OUT"
