#!/bin/bash
set -euo pipefail

echo "=== Copy www (dereference symlinks) Run Script ==="
echo "PROJECT_DIR: $PROJECT_DIR"
echo "BUILT_PRODUCTS_DIR: $BUILT_PRODUCTS_DIR"
echo "CONTENTS_FOLDER_PATH: $CONTENTS_FOLDER_PATH"

SRC="${PROJECT_DIR}/testWebServer/www/"
DST="${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/www/"

echo "SRC = $SRC"
echo "DST = $DST"

# Ensure destination exists
mkdir -p "$DST"

# Ensure rsync exists
if ! command -v rsync >/dev/null 2>&1; then
  echo "Error: rsync not found in PATH"
  exit 1
fi

# rsync options:
# -a    : archive (recursive, preserve perms, times, ...)
# -v    : verbose
# -L    : copy the referent of symbolic links (dereference)
# --delete : remove files in destination that were removed from source
# --exclude : exclude VCS or other files you don't want
rsync -a -v -L --delete --exclude='.git' --exclude='.DS_Store' "$SRC" "$DST"

echo "=== Done copying www ==="

