#!/usr/bin/env bash
# Deploy the bw-2checkout-pricing plugin to copernic.demoing.info for testing.
#
# ONE-WAY SYNC: bw-plugins (source of truth) -> copernic (test install).
# Do NOT edit the plugin inside /srv/apps/copernic — edit it in
# /srv/apps/bw-plugins/wp-content/plugins/bw-2checkout-pricing/ and re-run this.
# (bw-plugins is where versioning, scans, and release.sh operate.)

set -euo pipefail

SRC=/srv/apps/bw-plugins/wp-content/plugins/bw-2checkout-pricing/
DEST=/srv/apps/copernic/wp-content/plugins/bw-2checkout-pricing/

echo "Syncing plugin -> copernic..."
# Mirror content only; do NOT preserve owner/group/perms/times (dest files are owned by
# www-data via the gateway, so attr preservation would fail). fix-permissions normalizes below.
rsync -r --delete --no-perms --no-owner --no-group --no-times "$SRC" "$DEST"

echo "Normalizing permissions..."
srv-gw fix-permissions --project copernic >/dev/null

echo "Ensuring active..."
srv-gw wp --project copernic -- plugin activate bw-2checkout-pricing >/dev/null 2>&1 || true

VER=$(grep -m1 -oE 'Version:[[:space:]]*[0-9.]+' "${DEST}bw-2checkout-pricing.php" | grep -oE '[0-9.]+')
echo "Deployed bw-2checkout-pricing v${VER} to copernic.demoing.info"
