#!/bin/bash
# Build script to embed admin.html into the worker
set -e
echo "=================================================="
echo "Building Worker with Embedded Admin Panel"
echo "=================================================="
echo ""
# Get the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
cd "$PROJECT_DIR"
# Check if admin.html exists
if [ ! -f "worker/admin.html" ]; then
echo "✗ admin.html not found in worker directory"
exit 1
fi
# Read admin.html and escape it for JavaScript
ADMIN_HTML=$(cat worker/admin.html | sed 's/\\/\\\\/g' | sed 's/`/\\`/g' | sed 's/\$/\\$/g')
# Create the embedded version
echo "Creating worker with embedded admin HTML..."
# Read the enhanced worker
WORKER_CONTENT=$(cat worker/video-server-enhanced.js)
# Replace the placeholder getAdminHTML function
# We'll create a new version that includes the actual HTML
cat > worker/video-server-final.js << 'WORKER_START'
/**
* Enhanced Cloudflare Worker for serving videos from R2 storage
* Features:
* - Admin panel with authentication
* - Video visibility controls (private, shareable, clip_shareable)
* - Clip generation and serving
* - Permission-based access control
*/
WORKER_START
# Append the admin HTML as a constant
echo "const ADMIN_HTML = \`" >> worker/video-server-final.js
cat worker/admin.html >> worker/video-server-final.js
echo "\`;" >> worker/video-server-final.js
echo "" >> worker/video-server-final.js
# Append the rest of the worker code, but skip the getAdminHTML placeholder
cat worker/video-server-enhanced.js | sed -n '/^export default/,$p' | \
sed 's/async function getAdminHTML() {/async function getAdminHTML() { return ADMIN_HTML; } \/\/ OLD VERSION: {/g' >> worker/video-server-final.js
echo "✓ Created video-server-final.js with embedded admin HTML"
echo ""
echo "To use this version:"
echo " cp worker/video-server-final.js worker/video-server.js"
echo " cd worker && wrangler deploy"
echo ""