#!/bin/bash # Build all sites: generate nginx.conf, render templates, prepare build/ directory set -euo pipefail SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) BUILD_DIR="$SCRIPT_DIR/build" echo "=== Onepager Build ===" # Clean build directory rm -rf "$BUILD_DIR" mkdir -p "$BUILD_DIR" # 1. Generate nginx.conf echo "[1/3] Generating nginx.conf..." "$SCRIPT_DIR/nginx/generate-nginx.sh" "$SCRIPT_DIR/sites" > "$SCRIPT_DIR/nginx/nginx.conf" echo " -> nginx/nginx.conf written" # 2. Build each site echo "[2/3] Building sites..." count=0 for site_dir in "$SCRIPT_DIR/sites"/*/; do [ -f "$site_dir/site.yaml" ] || continue domain=$(basename "$site_dir") template=$(yq -r '.template // "custom"' "$site_dir/site.yaml") mkdir -p "$BUILD_DIR/$domain" if [ "$template" = "custom" ]; then # Copy everything except site.yaml find "$site_dir" -maxdepth 1 -not -name site.yaml -not -path "$site_dir" -exec cp -r {} "$BUILD_DIR/$domain/" \; echo " [custom] $domain" else template_file="$SCRIPT_DIR/templates/${template}.html" if [ ! -f "$template_file" ]; then echo " [ERROR] Template '$template' not found for $domain — skipping" >&2 continue fi "$SCRIPT_DIR/render.sh" "$site_dir/site.yaml" "$template_file" > "$BUILD_DIR/$domain/index.html" # Copy assets if present [ -d "$site_dir/assets" ] && cp -r "$site_dir/assets" "$BUILD_DIR/$domain/" echo " [${template}] $domain" fi count=$((count + 1)) done echo " -> $count sites built" # 3. Validate nginx config (if docker is available) echo "[3/3] Validating nginx config..." if command -v docker &>/dev/null; then docker run --rm \ -v "$SCRIPT_DIR/nginx/nginx.conf:/etc/nginx/nginx.conf:ro" \ -v "$BUILD_DIR:/usr/share/nginx/html:ro" \ nginx:alpine nginx -t 2>&1 && echo " -> nginx config valid" || echo " [WARN] nginx validation failed (non-fatal for local builds)" else echo " [SKIP] Docker not available, skipping nginx validation" fi echo "=== Build complete: $count sites ==="