#!/bin/bash

# Shared definition of "which themes participate in the WordPress.org sync".
#
# Both `deploy-dotorg.sh` (which writes to SVN) and `check-dotorg-sync.mjs`
# (which reports on themes whose SVN copy never went live) have to agree on the
# candidate set. If each carried its own copy of these rules they would drift,
# and the checker would report drift for themes the deploy was never going to
# touch. Change the rules here and both sides move together.
#
# Source it for the helper functions, or run it directly for the candidate list:
#
#   source .theme-utils/dotorg-sync-eligible.sh   # dotorg_* functions
#   bash .theme-utils/dotorg-sync-eligible.sh     # "<slug> <version>" per line
#
# Eligibility itself is local and offline. The two network helpers at the bottom
# answer "is this theme published yet", which is a different question and is
# deliberately kept separate: eligibility must be answerable without a network.

DOTORG_UTILS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOTORG_IGNORE_FILE="$DOTORG_UTILS_DIR/.dotorg-ignore"
DOTORG_REPO_ROOT="$(cd "$DOTORG_UTILS_DIR/.." && pwd)"

# Populates DOTORG_THEMES_TO_IGNORE from .dotorg-ignore, skipping comments and
# blank lines. A missing ignore list is fatal rather than empty: without it every
# theme in the repo silently becomes a sync candidate.
#
# .dotorg-ignore is theme slugs only. The rsync patterns that used to share the
# file now live in .dotorg-rsync-exclude, because a pattern landing in this list
# would silently drop a theme from the deploy and from the sync check.
dotorg_read_ignore_list() {
	DOTORG_THEMES_TO_IGNORE=()

	if [[ ! -f "$DOTORG_IGNORE_FILE" ]]; then
		echo "ERROR: ignore list not found at $DOTORG_IGNORE_FILE" >&2
		echo "Refusing to continue: without it every theme becomes a sync candidate." >&2
		return 1
	fi

	while IFS= read -r line || [[ -n "$line" ]]; do
		[[ "$line" =~ ^[[:space:]]*# ]] && continue
		[[ -z "${line// /}" ]] && continue
		DOTORG_THEMES_TO_IGNORE+=("$line")
	done < "$DOTORG_IGNORE_FILE"
}

# The version in the theme's style.css, with the `-wpcom` suffix stripped. The
# .org side only ever sees the numeric part, so that is what both callers
# compare against.
dotorg_theme_version() {
	local slug="$1"
	[[ -f "$DOTORG_REPO_ROOT/$slug/style.css" ]] || return 1

	grep -i '^[[:space:]]*Version:' "$DOTORG_REPO_ROOT/$slug/style.css" \
		| head -1 \
		| awk -F: '{ print $2 }' \
		| sed 's/[",]//g' \
		| sed 's/-wpcom//g' \
		| tr -d '[[:space:]]'
}

# A theme is eligible when it is a block theme (theme.json present -- our classic
# themes are not synced), it has a style.css to read a version from, and it is
# not on the ignore list. Call dotorg_read_ignore_list first.
dotorg_is_eligible() {
	local slug="$1"

	[[ -f "$DOTORG_REPO_ROOT/$slug/theme.json" ]] || return 1
	[[ -f "$DOTORG_REPO_ROOT/$slug/style.css" ]] || return 1

	local ignored
	for ignored in "${DOTORG_THEMES_TO_IGNORE[@]}"; do
		[[ "$ignored" == "$slug" ]] && return 1
	done

	return 0
}

# Prints "<slug> <version>" for every eligible theme, in directory order.
dotorg_eligible_themes() {
	dotorg_read_ignore_list || return 1

	local dir slug version
	for dir in "$DOTORG_REPO_ROOT"/*/ ; do
		slug="$(basename "$dir")"
		dotorg_is_eligible "$slug" || continue
		version="$(dotorg_theme_version "$slug")" || continue
		[[ -n "$version" ]] || continue
		echo "$slug $version"
	done
}

# Network helpers. Both return 0 on HTTP 200 and 1 on anything else, including a
# curl failure -- a theme we cannot reach is treated as not published, which
# makes the deploy skip it rather than attempt a doomed commit.
dotorg_is_in_directory() {
	local slug="$1"
	local response
	response=$(curl -s -o /dev/null -w "%{http_code}" "https://wordpress.org/themes/${slug}/")
	[[ "$response" == "200" ]]
}

dotorg_has_svn_repository() {
	local slug="$1"
	local response
	response=$(curl -s -o /dev/null -w "%{http_code}" "https://themes.svn.wordpress.org/${slug}/")
	[[ "$response" == "200" ]]
}

# Run directly rather than sourced: emit the candidate list.
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
	dotorg_eligible_themes
fi
