#!/usr/bin/env bash
# update_check.sh — mechanical part of the update-airflow3-connectors skill.
#
# Shallow-clones the dlt-connectors monorepo (main) and emits a compact diff
# summary for the local connectors/ folder. Makes NO changes to the local repo.
#
# Usage: update_check.sh [--connectors-dir <path>] [--repo <git-url>]
#
# Output (stdout, one finding per line):
#   UPSTREAM_CLONE=<path>          # kept for follow-up merge/copy steps — remove when done
#   UPSTREAM_COMMIT=<sha>
#   == existing connectors ==
#   connector <name>: IDENTICAL | DIFFERS | NOT_UPSTREAM
#     changed: <file>              # exists on both sides, content differs
#     local-only: <file>           # exists only locally (customization or dropped upstream)
#     upstream-only: <file>        # exists only upstream (new/removed locally)
#     example-toml differs: <file> # committed .dlt example file content differs
#   == new upstream connectors ==
#   <name>                         # upstream connector not present locally
#
# Line endings are neutralized everywhere (CRLF-only differences are ignored).
# The real credential files are never touched: the whole .dlt/ directory is
# excluded from the recursive diff; only *_example.toml files are compared,
# by normalized checksum.

set -euo pipefail

CONNECTORS_DIR="connectors"
REPO_URL="git@github.com:Gemma-Analytics/dlt-connectors.git"

while [ $# -gt 0 ]; do
  case "$1" in
    --connectors-dir) CONNECTORS_DIR="$2"; shift 2 ;;
    --repo) REPO_URL="$2"; shift 2 ;;
    *) echo "unknown argument: $1" >&2; exit 2 ;;
  esac
done

if [ ! -d "$CONNECTORS_DIR" ]; then
  echo "ERROR: '$CONNECTORS_DIR' not found — run from the Airflow repo root or pass --connectors-dir" >&2
  echo "HINT: no connectors dir can also mean a different repo shape:" >&2
  echo "  - connectors.lock.yml present   -> Airflow 2 lockfile repo: use gemma-dlt/sync-dlt-connectors" >&2
  echo "  - EWAH/airflowprovider, no connectors dir -> use setup-dlt-uv-connector-airflow (modern hard-copy variant) first" >&2
  exit 1
fi

UPSTREAM_DIR=$(mktemp -d)
# Self-clean on error only — on success the clone is deliberately kept for the
# follow-up merge/copy steps (the caller removes it when done).
trap 'rm -rf "$UPSTREAM_DIR"' ERR
git clone --quiet --depth 1 "$REPO_URL" "$UPSTREAM_DIR"
echo "UPSTREAM_CLONE=$UPSTREAM_DIR"
echo "UPSTREAM_COMMIT=$(git -C "$UPSTREAM_DIR" rev-parse --short HEAD)"

norm_hash() { sed -e 's/\r$//' "$1" | sha256sum | cut -d' ' -f1; }

echo "== existing connectors =="
for local_dir in "$CONNECTORS_DIR"/*/; do
  name=$(basename "$local_dir")
  upstream="$UPSTREAM_DIR/connectors/$name"

  if [ ! -d "$upstream" ]; then
    echo "connector $name: NOT_UPSTREAM"
    continue
  fi

  # diff exits 0 = identical, 1 = differences (expected), >=2 = real trouble
  # (unreadable dir, ...). Let only 0/1 pass — masking >=2 would misreport a
  # broken directory as IDENTICAL.
  rc=0
  raw=$(diff -rq --strip-trailing-cr "$local_dir" "$upstream" \
    --exclude=.venv --exclude=__pycache__ --exclude='*.duckdb' --exclude='*.wal' \
    --exclude=.dlt --exclude=.cursor --exclude='.e*') || rc=$?
  if [ "$rc" -ge 2 ]; then
    echo "ERROR: diff failed for connector $name (exit $rc)" >&2
    rm -rf "$UPSTREAM_DIR"  # exit does not fire the ERR trap
    exit "$rc"
  fi

  # Committed .dlt example files: compare by normalized checksum only.
  toml_report=""
  for f in "$local_dir".dlt/*_example.toml; do
    [ -f "$f" ] || continue
    b=$(basename "$f")
    if [ -f "$upstream/.dlt/$b" ]; then
      if [ "$(norm_hash "$f")" != "$(norm_hash "$upstream/.dlt/$b")" ]; then
        toml_report="${toml_report}  example-toml differs: $b"$'\n'
      fi
    else
      toml_report="${toml_report}  example-toml local-only: $b"$'\n'
    fi
  done
  for f in "$upstream/.dlt/"*_example.toml; do
    [ -f "$f" ] || continue
    b=$(basename "$f")
    [ -f "$local_dir.dlt/$b" ] || toml_report="${toml_report}  example-toml upstream-only: $b"$'\n'
  done

  if [ -z "$raw" ] && [ -z "$toml_report" ]; then
    echo "connector $name: IDENTICAL"
    continue
  fi

  echo "connector $name: DIFFERS"
  while IFS= read -r line; do
    [ -z "$line" ] && continue
    case "$line" in
      Files\ *)
        f=${line#Files }; f=${f%% and *}; f=${f#"$local_dir"}
        echo "  changed: $f"
        ;;
      Only\ in\ "$UPSTREAM_DIR"*)
        d=${line#Only in }; d=${d%%: *}; f=${line##*: }
        rel=${d#"$upstream"}; rel=${rel#/}
        echo "  upstream-only: ${rel:+$rel/}$f"
        ;;
      Only\ in\ *)
        d=${line#Only in }; d=${d%%: *}; f=${line##*: }
        rel=${d#"$local_dir"}; rel=${rel#/}
        echo "  local-only: ${rel:+$rel/}$f"
        ;;
    esac
  done <<< "$raw"
  printf '%s' "$toml_report"
done

echo "== new upstream connectors =="
for d in "$UPSTREAM_DIR/connectors/"*/; do
  n=$(basename "$d")
  [ -d "$CONNECTORS_DIR/$n" ] || echo "$n"
done
