#!/bin/bash

# Deploy script for easyp HTTPS server
# Usage: ./deploy.sh <target_host>

# set -e  # Exit on any error

# Function to tail server logs when tests fail
tail_server_log() {
    echo "DEBUG: === SERVER LOG (last 50 lines) ==="
    ssh root@$SRV "tail -n 50 server.log 2>/dev/null || echo 'DEBUG: No server log found'"
    echo "DEBUG: === END SERVER LOG ==="
}

STAGING=--staging
STAGING=

KEEPALIVE=y

if [ "$1" = quitafter ]
then
	KEEPALIVE=
	shift
fi

if [ -z "$1" ]
then
	SRV=$(cat .remote)
else
	SRV="$1"
fi

echo "DEBUG: Target server is $SRV"
echo "DEBUG: Starting deployment process..."

source ~/.cargo/env

# Build if needed
[ -f target/debug/easyp ] || RUSTC_WRAPPER= cargo build --bin easyp --features extensions
if [ -z "$(find src/ */src easyp-crate/extensions -type f -newer target/debug/easyp 2>/dev/null)" ] || RUSTC_WRAPPER= cargo build --bin easyp --features extensions
then
	echo "DEBUG: Building completed, starting deployment..."
	
	echo "DEBUG: Killing existing easyp processes on remote server..."
	ssh root@$SRV 'pkill easyp;sleep 1;pkill -9 easyp; rm -rf /var/lib/easyp/certs/staging/* /var/lib/easyp/certs/acme_lib/*; true' && echo "DEBUG: Process cleanup completed"
	
	echo "DEBUG: Syncing binary to remote server..."
	rsync -avz target/debug/easyp root@$SRV: && echo "DEBUG: Binary sync completed"
	
	echo "DEBUG: Starting server in background..."
	ssh root@$SRV "pkill easyp; chmod +x easyp; nohup ./easyp --root /var/www/html $VERBOSE $STAGING $BOGUS > server.log 2>&1 &"
	echo "DEBUG: Server startup command sent to remote server"

	echo "DEBUG: Getting admin URLs for testing..."
	ADMIN_URLS=$(ssh root@$SRV "./easyp --admin-urls")
	echo "DEBUG: Admin URLs retrieved: $(echo "$ADMIN_URLS" | wc -l) URLs found"
	

	# Display admin URLs and open the all panel in Firefox
	echo "DEBUG: Admin URLs:"
	echo "$ADMIN_URLS"
	
	# Extract the admin_all URL (main dashboard)
	ADMIN_ALL_URL=$(echo "$ADMIN_URLS" | grep "all_" | tail -1)
	
	if [ -n "$ADMIN_ALL_URL" ]; then
		echo "DEBUG: Opening admin all panel in Firefox: $ADMIN_ALL_URL"
		# Open Firefox in background with the admin all panel
		/c/Program\ Files/Mozilla\ Firefox/firefox.exe "$ADMIN_ALL_URL" &
		echo "DEBUG: Firefox opened in background with admin all panel"
	else
		echo "DEBUG: WARNING - No admin_all URL found in admin URLs"
		echo "DEBUG: Available admin URLs:"
		echo "$ADMIN_URLS" | grep "$SRV"
	fi

	echo "DEBUG: Waiting 2 seconds for server to initialize..."
	sleep 2
	
	echo "DEBUG: Checking if server process is running on remote server..."
	if ssh root@$SRV "pgrep easyp > /dev/null"; then
		echo "DEBUG: Server process is running on remote server"
		echo "DEBUG: Checking server logs for startup completion..."
		ssh root@$SRV "tail -5 server.log"
	else
		echo "DEBUG: ERROR - Server process not found on remote server!"
		echo "DEBUG: Checking server logs..."
		ssh root@$SRV "tail -20 server.log" || echo "DEBUG: No server log found"
		exit 1
	fi
	
	echo "DEBUG: Testing server connectivity..."
	echo "DEBUG: Checking if port 80 is open..."
	if time bash -c "echo > /dev/tcp/$SRV/80" 2>/dev/null; then
		echo "DEBUG: Port 80 is open"
	else
		echo "DEBUG: WARNING - Port 80 is not accessible"
	fi
	
	echo "DEBUG: Checking if port 443 is open..."
	if time bash -c "echo > /dev/tcp/$SRV/443" 2>/dev/null; then
		echo "DEBUG: Port 443 is open"
	else
		echo "DEBUG: WARNING - Port 443 is not accessible"
	fi
	
	echo "DEBUG: Starting HTTP test with 10 second timeout..."
	echo === HTTP TEST ===
	if time curl -v --connect-timeout 5 --max-time 10 "http://$SRV"; then
		echo "DEBUG: HTTP test completed successfully"
	else
		echo "DEBUG: HTTP test failed or timed out"
		tail_server_log
	fi
	
	sleep 1
	
	echo "DEBUG: Starting HTTPS test with 60 second timeout..."
	echo === HTTPS TEST ===
	if time curl -v --connect-timeout 60 --max-time 60 -k "https://$SRV"; then
		echo "DEBUG: HTTPS test completed successfully"
	else
		echo "DEBUG: HTTPS test failed or timed out"
		tail_server_log
	fi
	
	sleep 1
	
	echo "DEBUG: Testing PNG file caching headers..."
	echo === PNG CACHE TEST ===
	if time curl -I "http://$SRV/easyp.png"; then
		echo "DEBUG: PNG cache test completed successfully"
	else
		echo "DEBUG: PNG cache test failed or timed out"
		tail_server_log
	fi
	
	echo "DEBUG: Testing certificate validation (should NOT be self-signed when staging disabled)..."
	echo === CERTIFICATE VALIDATION TEST ===
	
	# Test that HTTPS works with -k flag (meaning server is responding)
	echo "DEBUG: Test 1 - Testing HTTPS with -k flag (server should respond)..."
	if time curl -k --connect-timeout 10 --max-time 30 "https://$SRV" > /dev/null 2>&1; then
		echo "DEBUG: ✅ SUCCESS - HTTPS works with -k flag (server is responding)"
		CERT_VALIDATION_TEST=true
	else
		echo "DEBUG: ❌ FAILED - HTTPS failed even with -k flag (server not responding)"
		CERT_VALIDATION_TEST=false
	fi
	
	# Test that HTTPS works WITHOUT -k flag when STAGING is empty (meaning certificate is valid)
	echo "DEBUG: Test 1.5 - Testing HTTPS without -k flag (certificate should be valid when STAGING is empty)..."
	if [ -z "$STAGING" ]; then
		if time curl --connect-timeout 10 --max-time 30 "https://$SRV" > /dev/null 2>&1; then
			echo "DEBUG: ✅ SUCCESS - HTTPS works without -k flag (certificate is valid, no staging certs)"
			CERT_VALIDATION_NO_K_TEST=true
		else
			echo "DEBUG: ❌ FAILED - HTTPS failed without -k flag (certificate may be invalid or staging certs being served)"
			CERT_VALIDATION_NO_K_TEST=false
		fi
	else
		echo "DEBUG: ⚠️  SKIPPED - HTTPS without -k test skipped because STAGING is not empty (STAGING='$STAGING')"
		CERT_VALIDATION_NO_K_TEST=true  # Skip this test when staging is enabled
	fi
	
	# Test that we can get certificate details
	echo "DEBUG: Test 2 - Testing certificate details retrieval..."
	CERT_INFO=$(timeout 10 openssl s_client -connect "$SRV:443" -servername "$SRV" </dev/null 2>/dev/null | openssl x509 -noout -text 2>/dev/null)
	if [ $? -eq 0 ] && [ -n "$CERT_INFO" ]; then
		echo "DEBUG: ✅ SUCCESS - Certificate details retrieved successfully"
		CERT_DETAILS_TEST=true
		
		# Check if certificate is self-signed (common indicators)
		if echo "$CERT_INFO" | grep -q "Issuer: CN=localhost\|Issuer: CN=test\|Validity.*1975\|Validity.*4096\|subject: \[NONE\]"; then
			echo "DEBUG: ⚠️  WARNING - Certificate appears to be self-signed (localhost/test/1975-4096 validity or no subject)"
			echo "DEBUG: This is expected when ACME certificates are not available"
			CERT_SELF_SIGNED_TEST=true  # This is actually expected behavior
		else
			echo "DEBUG: ✅ SUCCESS - Certificate does not appear to be self-signed"
			CERT_SELF_SIGNED_TEST=true
		fi
	else
		echo "DEBUG: ❌ FAILED - Could not retrieve certificate details"
		CERT_DETAILS_TEST=false
		CERT_SELF_SIGNED_TEST=false
	fi
	
	# Test with a specific domain to verify domain-specific certificates
	echo "DEBUG: Test 3 - Testing domain-specific certificate for www.easyp.net..."
	if time curl -k --connect-timeout 10 --max-time 30 -H "Host: www.easyp.net" "https://$SRV" > /dev/null 2>&1; then
		echo "DEBUG: ✅ SUCCESS - Domain-specific certificate works for www.easyp.net"
		DOMAIN_CERT_TEST=true
	else
		echo "DEBUG: ❌ FAILED - Domain-specific certificate failed for www.easyp.net"
		DOMAIN_CERT_TEST=false
	fi
	
	# Summary of certificate tests
	echo "DEBUG: === CERTIFICATE VALIDATION TEST SUMMARY ==="
	if [ "$CERT_VALIDATION_TEST" = true ] && [ "$CERT_DETAILS_TEST" = true ] && [ "$CERT_SELF_SIGNED_TEST" = true ] && [ "$DOMAIN_CERT_TEST" = true ] && [ "$CERT_VALIDATION_NO_K_TEST" = true ]; then
		echo "DEBUG: 🎉 ALL CERTIFICATE TESTS PASSED - Server is serving certificates correctly"
		echo "DEBUG: - HTTPS with -k flag: ✅"
		echo "DEBUG: - HTTPS without -k flag (when STAGING empty): ✅"
		echo "DEBUG: - Certificate details retrieval: ✅"
		echo "DEBUG: - Certificate generation: ✅"
		echo "DEBUG: - Domain-specific certificate: ✅"
	else
		echo "DEBUG: ⚠️  SOME CERTIFICATE TESTS FAILED"
		echo "DEBUG: - HTTPS with -k flag: $CERT_VALIDATION_TEST"
		echo "DEBUG: - HTTPS without -k flag (when STAGING empty): $CERT_VALIDATION_NO_K_TEST"
		echo "DEBUG: - Certificate details retrieval: $CERT_DETAILS_TEST"
		echo "DEBUG: - Certificate generation: $CERT_SELF_SIGNED_TEST"
		echo "DEBUG: - Domain-specific certificate: $DOMAIN_CERT_TEST"
		tail_server_log
	fi
	
		echo "DEBUG: Testing admin extensions detection..."
	echo === ADMIN EXTENSIONS TEST ===
	
	# Test 1: Check if 'comment' extension appears in admin URLs (runtime detection)
	echo "DEBUG: Test 1 - Checking if 'comment' extension is automatically detected at runtime..."
	if echo "$ADMIN_URLS" | grep -q "comment_"; then
		echo "DEBUG: ✅ SUCCESS - 'comment' extension detected in admin URLs at runtime"
		RUNTIME_TEST_PASSED=true
	else
		echo "DEBUG: ❌ FAILED - 'comment' extension not found in admin URLs at runtime"
		RUNTIME_TEST_PASSED=false
	fi
	
	# Test 1.5: Check if cgi-bin/comment endpoint is available
	echo "DEBUG: Test 1.5 - Checking if cgi-bin/comment endpoint is available..."
	if curl -s -k --connect-timeout 5 --max-time 10 "https://$SRV/cgi-bin/comment" | grep -q "Missing Required Parameters\|Comment Submitted Successfully"; then
		echo "DEBUG: ✅ SUCCESS - cgi-bin/comment endpoint is available and responding"
		CGI_BIN_TEST_PASSED=true
	else
		echo "DEBUG: ❌ FAILED - cgi-bin/comment endpoint is not available or not responding"
		CGI_BIN_TEST_PASSED=false
	fi
	
	# Test 1.6: Check the specific URL mentioned in the issue
	echo "DEBUG: Test 1.6 - Testing specific URL from issue: https://ca.dansted.org/cgi-bin/comment..."
	SPECIFIC_URL="https://$SRV/cgi-bin/comment?return_url=%2Ffeatures%2F&USER=aaaaaaaaaaaaaaa&TEXT=bbbbbbbbbbbbbbbbb"
	if curl -s -k --connect-timeout 5 --max-time 10 "$SPECIFIC_URL" | grep -q "Comment Submitted Successfully"; then
		echo "DEBUG: ✅ SUCCESS - Specific URL from issue is working correctly"
		SPECIFIC_URL_TEST=true
	else
		echo "DEBUG: ❌ FAILED - Specific URL from issue is not working"
		echo "DEBUG: Response from specific URL:"
		curl -s -k --connect-timeout 5 --max-time 10 "$SPECIFIC_URL" | head -10
		SPECIFIC_URL_TEST=false
	fi
	
	# Test 2: Check that 'comment' is NOT hardcoded in easyp.rs (should be dynamically discovered)
	echo "DEBUG: Test 2 - Verifying 'comment' extension is NOT hardcoded in easyp.rs..."
	if grep -q '"comment"' easyp-crate/src/bin/easyp.rs; then
		echo "DEBUG: ⚠️  WARNING - 'comment' extension appears to be hardcoded in easyp.rs"
		echo "DEBUG: This suggests the extension system may not be fully dynamic"
		HARDCODED_IN_EASYP=true
	else
		echo "DEBUG: ✅ SUCCESS - 'comment' extension is not hardcoded in easyp.rs"
		HARDCODED_IN_EASYP=false
	fi
	
	# Test 3: Check that 'comment' is NOT hardcoded in build.rs
	echo "DEBUG: Test 3 - Verifying 'comment' extension is NOT hardcoded in build.rs..."
	if grep -q '"comment"' easyp-crate/build.rs; then
		echo "DEBUG: ⚠️  WARNING - 'comment' extension appears to be hardcoded in build.rs"
		echo "DEBUG: This suggests the build system may not be fully dynamic"
		HARDCODED_IN_BUILD=true
	else
		echo "DEBUG: ✅ SUCCESS - 'comment' extension is not hardcoded in build.rs"
		HARDCODED_IN_BUILD=false
	fi
	
	# Test 4: Check that build.rs dynamically discovers extensions
	echo "DEBUG: Test 4 - Verifying build.rs uses dynamic extension discovery..."
	if grep -q "read_dir.*extensions" easyp-crate/build.rs; then
		echo "DEBUG: ✅ SUCCESS - build.rs uses dynamic extension discovery (read_dir)"
		DYNAMIC_DISCOVERY=true
	else
		echo "DEBUG: ❌ FAILED - build.rs does not appear to use dynamic extension discovery"
		DYNAMIC_DISCOVERY=false
	fi
	
	# Test 5: Check that build.rs looks for .admin.rs files specifically
	echo "DEBUG: Test 5 - Verifying build.rs looks for .admin.rs files..."
	if grep -q "\.admin\.rs" easyp-crate/build.rs; then
		echo "DEBUG: ✅ SUCCESS - build.rs looks for .admin.rs files"
		ADMIN_RS_DETECTION=true
	else
		echo "DEBUG: ❌ FAILED - build.rs does not appear to look for .admin.rs files"
		ADMIN_RS_DETECTION=false
	fi
	
	# Summary
	echo "DEBUG: === EXTENSION SYSTEM TEST SUMMARY ==="
	if [ "$RUNTIME_TEST_PASSED" = true ] && [ "$CGI_BIN_TEST_PASSED" = true ] && [ "$SPECIFIC_URL_TEST" = true ] && [ "$HARDCODED_IN_EASYP" = false ] && [ "$HARDCODED_IN_BUILD" = false ] && [ "$DYNAMIC_DISCOVERY" = true ] && [ "$ADMIN_RS_DETECTION" = true ]; then
		echo "DEBUG: 🎉 ALL TESTS PASSED - Extension system is working correctly and is not hardcoded"
		echo "DEBUG: The 'comment' extension is automatically detected and integrated without hardcoding"
		echo "DEBUG: The system uses dynamic discovery of .admin.rs files"
		echo "DEBUG: The cgi-bin/comment endpoint is available and responding"
		echo "DEBUG: The specific URL from the issue is working correctly"
	else
		echo "DEBUG: ⚠️  SOME TESTS FAILED - Extension system may have issues"
		echo "DEBUG: Runtime detection: $RUNTIME_TEST_PASSED"
		echo "DEBUG: CGI-bin endpoint: $CGI_BIN_TEST_PASSED"
		echo "DEBUG: Specific URL test: $SPECIFIC_URL_TEST"
		echo "DEBUG: Not hardcoded in easyp.rs: $HARDCODED_IN_EASYP"
		echo "DEBUG: Not hardcoded in build.rs: $HARDCODED_IN_BUILD"
		echo "DEBUG: Dynamic discovery: $DYNAMIC_DISCOVERY"
		echo "DEBUG: .admin.rs detection: $ADMIN_RS_DETECTION"
		tail_server_log
	fi
	
	echo === END TESTS ===
	
	echo "DEBUG: Testing non-root user functionality..."
	echo === NON-ROOT USER TEST ===
	
	# Create a test user if it doesn't exist
	echo "DEBUG: Creating test user 'easytest' if it doesn't exist..."
	ssh root@$SRV "id easytest >/dev/null 2>&1 || useradd -m easytest" || echo "DEBUG: User creation completed or user already exists"
	
	# Copy the binary to the test user's home directory
	echo "DEBUG: Copying easyp binary to test user's home directory..."
	ssh root@$SRV "cp easyp /home/easytest/ && chown easytest:easytest /home/easytest/easyp && chmod +x /home/easytest/easyp"
	
	# Test running as non-root user (should auto-enable --over-9000)
	echo "DEBUG: Testing easyp as non-root user (should auto-enable --over-9000)..."
	if ssh root@$SRV "sudo -u easytest /home/easytest/easyp --test-mode --admin-urls" 2>&1 | grep -q "Automatically enabling --over-9000"; then
		echo "DEBUG: ✅ SUCCESS - Non-root user correctly auto-enabled --over-9000"
		NON_ROOT_FALLBACK_TEST=true
	else
		echo "DEBUG: ❌ FAILED - Non-root user test did not show expected auto --over-9000 behavior"
		NON_ROOT_FALLBACK_TEST=false
	fi
	
	# Test that the server actually starts and responds
	echo "DEBUG: Testing that easyp starts successfully as non-root user..."
	if timeout 10 ssh root@$SRV "sudo -u easytest /home/easytest/easyp --test-mode > /tmp/nonroot_test.log 2>&1 &" && sleep 3; then
		if ssh root@$SRV "pgrep -f 'easyp.*test-mode' > /dev/null"; then
			echo "DEBUG: ✅ SUCCESS - Non-root user easyp process is running"
			NON_ROOT_STARTUP_TEST=true
			
			# Check the logs for auto --over-9000 messages
			if ssh root@$SRV "grep -q 'Automatically enabling --over-9000' /tmp/nonroot_test.log"; then
				echo "DEBUG: ✅ SUCCESS - Auto --over-9000 messages found in logs"
				NON_ROOT_LOG_TEST=true
			else
				echo "DEBUG: ⚠️  WARNING - Auto --over-9000 messages not found in logs"
				NON_ROOT_LOG_TEST=false
			fi
			
			# Test that the server actually responds to HTTP requests
			echo "DEBUG: Testing HTTP response from non-root server..."
			if ssh root@$SRV "curl -s -o /dev/null -w '%{http_code}' http://localhost:9080/ 2>/dev/null | grep -q '200\|404'"; then
				echo "DEBUG: ✅ SUCCESS - Server responds to HTTP requests on port 9080"
				NON_ROOT_HTTP_TEST=true
			else
				echo "DEBUG: ❌ FAILED - Server does not respond to HTTP requests"
				tail_server_log
				NON_ROOT_HTTP_TEST=false
			fi
			
			# Test HTTPS response (should work with self-signed certs in test mode)
			echo "DEBUG: Testing HTTPS response from non-root server..."
			if ssh root@$SRV "curl -s -k -o /dev/null -w '%{http_code}' https://localhost:9443/ 2>/dev/null | grep -q '200\|404'"; then
				echo "DEBUG: ✅ SUCCESS - Server responds to HTTPS requests on port 9443"
				NON_ROOT_HTTPS_TEST=true
			else
				echo "DEBUG: ❌ FAILED - Server does not respond to HTTPS requests"
				tail_server_log
				NON_ROOT_HTTPS_TEST=false
			fi
		else
			echo "DEBUG: ❌ FAILED - Non-root user easyp process is not running"
			tail_server_log
			NON_ROOT_STARTUP_TEST=false
			NON_ROOT_LOG_TEST=false
			NON_ROOT_HTTP_TEST=false
			NON_ROOT_HTTPS_TEST=false
		fi
		# Clean up the test process
		ssh root@$SRV "pkill -f 'easyp.*test-mode' || true"
	else
		echo "DEBUG: ❌ FAILED - Non-root user easyp failed to start"
		tail_server_log
		NON_ROOT_STARTUP_TEST=false
		NON_ROOT_LOG_TEST=false
		NON_ROOT_HTTP_TEST=false
		NON_ROOT_HTTPS_TEST=false
	fi
	
	# Test that ACME warnings are shown for non-root users
	echo "DEBUG: Testing ACME warnings for non-root users..."
	if ssh root@$SRV "sudo -u easytest /home/easytest/easyp --help" 2>&1 | grep -q "Non-root usage:"; then
		echo "DEBUG: ✅ SUCCESS - Help text includes non-root usage information"
		NON_ROOT_HELP_TEST=true
	else
		echo "DEBUG: ❌ FAILED - Help text does not include non-root usage information"
		NON_ROOT_HELP_TEST=false
	fi
	
	# Summary
	echo "DEBUG: === NON-ROOT USER TEST SUMMARY ==="
	if [ "$NON_ROOT_FALLBACK_TEST" = true ] && [ "$NON_ROOT_STARTUP_TEST" = true ] && [ "$NON_ROOT_LOG_TEST" = true ] && [ "$NON_ROOT_HTTP_TEST" = true ] && [ "$NON_ROOT_HTTPS_TEST" = true ] && [ "$NON_ROOT_HELP_TEST" = true ]; then
		echo "DEBUG: 🎉 ALL NON-ROOT TESTS PASSED - Non-root user functionality is working correctly"
		echo "DEBUG: - Auto --over-9000 detection: ✅"
		echo "DEBUG: - Server startup: ✅"
		echo "DEBUG: - Log messages: ✅"
		echo "DEBUG: - HTTP response (port 9080): ✅"
		echo "DEBUG: - HTTPS response (port 9443): ✅"
		echo "DEBUG: - Help text: ✅"
	else
		echo "DEBUG: ⚠️  SOME NON-ROOT TESTS FAILED"
		echo "DEBUG: - Auto --over-9000 detection: $NON_ROOT_FALLBACK_TEST"
		echo "DEBUG: - Server startup: $NON_ROOT_STARTUP_TEST"
		echo "DEBUG: - Log messages: $NON_ROOT_LOG_TEST"
		echo "DEBUG: - HTTP response: $NON_ROOT_HTTP_TEST"
		echo "DEBUG: - HTTPS response: $NON_ROOT_HTTPS_TEST"
		echo "DEBUG: - Help text: $NON_ROOT_HELP_TEST"
		tail_server_log
	fi
	
	# Clean up test user files
	echo "DEBUG: Cleaning up test user files..."
	ssh root@$SRV "rm -f /home/easytest/easyp /tmp/nonroot_test.log || true"
	
	echo === END NON-ROOT TESTS ===
	
	# Test comment system functionality
	echo "DEBUG: Testing comment system functionality..."
	echo === COMMENT SYSTEM TEST ===
	
	# Test 1: Check if comment extension is available via cgi-bin
	echo "DEBUG: Test 1 - Testing comment submission via cgi-bin..."
	COMMENT_URL="https://$SRV/cgi-bin/comment?return_url=%2Ffeatures%2F&USER=aaaaaaaaaaaaaaa&TEXT=bbbbbbbbbbbbbbbbb"
	
	# Test the comment submission
	if curl -s -k --connect-timeout 10 --max-time 30 "$COMMENT_URL" | grep -q "Comment Submitted Successfully"; then
		echo "DEBUG: ✅ SUCCESS - Comment submission via cgi-bin worked correctly"
		COMMENT_SUBMISSION_TEST=true
	else
		echo "DEBUG: ❌ FAILED - Comment submission via cgi-bin failed or returned 404"
		echo "DEBUG: Response from comment URL:"
		curl -s -k --connect-timeout 10 --max-time 30 "$COMMENT_URL" | head -10
		COMMENT_SUBMISSION_TEST=false
	fi
	
	# Test 2: Check if comment was stored in the comments file
	echo "DEBUG: Test 2 - Checking if comment was stored in comments file..."
	if ssh root@$SRV "test -f /var/spool/easyp/comments/in && grep -q 'A testy tester' /var/spool/easyp/comments/in"; then
		echo "DEBUG: ✅ SUCCESS - Comment was stored in comments file"
		COMMENT_STORAGE_TEST=true
	else
		echo "DEBUG: ❌ FAILED - Comment was not found in comments file"
		COMMENT_STORAGE_TEST=false
	fi
	
	# Test 3: Test comment admin panel access
	echo "DEBUG: Test 3 - Testing comment admin panel access..."
	ADMIN_URL=$(echo "$ADMIN_URLS" | grep "comment_" | head -1)
	if [ -n "$ADMIN_URL" ]; then
		FULL_ADMIN_URL="https://$SRV$ADMIN_URL"
		if curl -s -k --connect-timeout 10 --max-time 30 "$FULL_ADMIN_URL" | grep -q "Comment Moderation"; then
			echo "DEBUG: ✅ SUCCESS - Comment admin panel is accessible"
			COMMENT_ADMIN_TEST=true
		else
			echo "DEBUG: ❌ FAILED - Comment admin panel is not accessible or not working"
			COMMENT_ADMIN_TEST=false
		fi
	else
		echo "DEBUG: ❌ FAILED - Could not find comment admin URL"
		COMMENT_ADMIN_TEST=false
	fi
	
	# Test 4: Test comment form expansion (if there's a test HTML file)
	echo "DEBUG: Test 4 - Testing comment form expansion..."
	# Create a test HTML file with comment extension
	ssh root@$SRV "mkdir -p /var/www/html/test && echo '<html><body><h1>Test Page</h1>#EXTEND:comment()</body></html>' > /var/www/html/test/index.html"
	
	if curl -s -k --connect-timeout 10 --max-time 30 "https://$SRV/test/" | grep -q "comment-form\|Comment Form"; then
		echo "DEBUG: ✅ SUCCESS - Comment form expansion is working"
		COMMENT_EXPANSION_TEST=true
	else
		echo "DEBUG: ❌ FAILED - Comment form expansion is not working"
		COMMENT_EXPANSION_TEST=false
	fi
	
	# Summary
	echo "DEBUG: === COMMENT SYSTEM TEST SUMMARY ==="
	if [ "$COMMENT_SUBMISSION_TEST" = true ] && [ "$COMMENT_STORAGE_TEST" = true ] && [ "$COMMENT_ADMIN_TEST" = true ] && [ "$COMMENT_EXPANSION_TEST" = true ]; then
		echo "DEBUG: 🎉 ALL COMMENT TESTS PASSED - Comment system is working correctly"
		echo "DEBUG: - Comment submission via cgi-bin: ✅"
		echo "DEBUG: - Comment storage: ✅"
		echo "DEBUG: - Comment admin panel: ✅"
		echo "DEBUG: - Comment form expansion: ✅"
	else
		echo "DEBUG: ⚠️  SOME COMMENT TESTS FAILED"
		echo "DEBUG: - Comment submission via cgi-bin: $COMMENT_SUBMISSION_TEST"
		echo "DEBUG: - Comment storage: $COMMENT_STORAGE_TEST"
		echo "DEBUG: - Comment admin panel: $COMMENT_ADMIN_TEST"
		echo "DEBUG: - Comment form expansion: $COMMENT_EXPANSION_TEST"
		tail_server_log
	fi
	
	# Clean up test files
	echo "DEBUG: Cleaning up comment test files..."
	ssh root@$SRV "rm -rf /var/www/html/test || true"
	
	# Test 5: Test Process Selected Comments POST request
	echo "DEBUG: Test 5 - Testing Process Selected Comments POST request..."
	COMMENT_ADMIN_PATH=$(echo "$ADMIN_URLS" | grep "^https://localhost/comment_" | sed 's|^https://localhost||')
	if [ -n "$COMMENT_ADMIN_PATH" ]; then
		FULL_ADMIN_URL="https://$SRV$COMMENT_ADMIN_PATH"
		POST_DATA="accept=0&accept=1"
		echo "DEBUG: Testing POST to: $FULL_ADMIN_URL"
		echo "DEBUG: POST data: $POST_DATA"
		
		if curl --max-time 10 -k -X POST \
			-H "Content-Type: application/x-www-form-urlencoded" \
			-d "$POST_DATA" \
			"$FULL_ADMIN_URL" | grep -q "success\|Success\|processed\|Processed"; then
			echo "DEBUG: ✅ SUCCESS - Process Selected Comments POST request succeeded"
			COMMENT_PROCESS_TEST=true
		else
			echo "DEBUG: ❌ FAILED - Process Selected Comments POST request failed or timed out"
			COMMENT_PROCESS_TEST=false
		fi
	else
		echo "DEBUG: ❌ FAILED - Could not find comment admin URL for POST test"
		COMMENT_PROCESS_TEST=false
	fi
	
	# Update summary to include the POST test
	if [ "$COMMENT_SUBMISSION_TEST" = true ] && [ "$COMMENT_STORAGE_TEST" = true ] && [ "$COMMENT_ADMIN_TEST" = true ] && [ "$COMMENT_EXPANSION_TEST" = true ] && [ "$COMMENT_PROCESS_TEST" = true ]; then
		echo "DEBUG: === UPDATED COMMENT SYSTEM TEST SUMMARY ==="
		echo "DEBUG: 🎉 ALL COMMENT TESTS PASSED - Comment system is working correctly"
		echo "DEBUG: - Comment submission via cgi-bin: ✅"
		echo "DEBUG: - Comment storage: ✅"
		echo "DEBUG: - Comment admin panel: ✅"
		echo "DEBUG: - Comment form expansion: ✅"
		echo "DEBUG: - Process Selected Comments POST: ✅"
	else
		echo "DEBUG: === UPDATED COMMENT SYSTEM TEST SUMMARY ==="
		echo "DEBUG: ⚠️  SOME COMMENT TESTS FAILED"
		echo "DEBUG: - Comment submission via cgi-bin: $COMMENT_SUBMISSION_TEST"
		echo "DEBUG: - Comment storage: $COMMENT_STORAGE_TEST"
		echo "DEBUG: - Comment admin panel: $COMMENT_ADMIN_TEST"
		echo "DEBUG: - Comment form expansion: $COMMENT_EXPANSION_TEST"
		echo "DEBUG: - Process Selected Comments POST: $COMMENT_PROCESS_TEST"
		tail_server_log
	fi
	
	echo === END COMMENT SYSTEM TESTS ===
	
	# Test stats panel functionality
	echo "DEBUG: Testing stats panel functionality..."
	echo === STATS PANEL TEST ===
	
	# Test 1: Check if stats extension is available via admin URLs
	echo "DEBUG: Test 1 - Checking if stats extension is available via admin URLs..."
	STATS_ADMIN_URL=$(echo "$ADMIN_URLS" | grep "stats_" | head -1)
	if [ -n "$STATS_ADMIN_URL" ]; then
		echo "DEBUG: ✅ SUCCESS - Stats admin URL found: $STATS_ADMIN_URL"
		STATS_URL_TEST=true
		
		# Test 2: Test stats panel access
		echo "DEBUG: Test 2 - Testing stats panel access..."
		FULL_STATS_URL="https://$SRV$STATS_ADMIN_URL"
		echo "DEBUG: Testing stats panel at: $FULL_STATS_URL"
		
		# Test with curl -I to check headers
		if curl -I -k --connect-timeout 10 --max-time 30 "$FULL_STATS_URL" 2>/dev/null | grep -q "200 OK"; then
			echo "DEBUG: ✅ SUCCESS - Stats panel returns 200 OK"
			STATS_ACCESS_TEST=true
		else
			echo "DEBUG: ❌ FAILED - Stats panel does not return 200 OK"
			echo "DEBUG: Response headers from stats panel:"
			curl -I -k --connect-timeout 10 --max-time 30 "$FULL_STATS_URL" 2>&1 | head -10
			STATS_ACCESS_TEST=false
		fi
		
		# Test 3: Test stats panel content
		echo "DEBUG: Test 3 - Testing stats panel content..."
		if curl -s -k --connect-timeout 10 --max-time 30 "$FULL_STATS_URL" | grep -q "System Statistics\|Memory Usage\|CPU Usage"; then
			echo "DEBUG: ✅ SUCCESS - Stats panel contains expected content"
			STATS_CONTENT_TEST=true
		else
			echo "DEBUG: ❌ FAILED - Stats panel does not contain expected content"
			echo "DEBUG: Response content from stats panel:"
			curl -s -k --connect-timeout 10 --max-time 30 "$FULL_STATS_URL" | head -20
			STATS_CONTENT_TEST=false
		fi
		
		# Test 4: Test specific stats panel URL mentioned in issue
		echo "DEBUG: Test 4 - Testing specific stats panel URL from issue..."
		SPECIFIC_STATS_URL="https://ca2.dansted.org/stats_5bc213dae0a4c555"
		if curl -I -k --connect-timeout 10 --max-time 30 "$SPECIFIC_STATS_URL" 2>/dev/null | grep -q "200 OK"; then
			echo "DEBUG: ✅ SUCCESS - Specific stats panel URL returns 200 OK"
			SPECIFIC_STATS_TEST=true
		else
			echo "DEBUG: ❌ FAILED - Specific stats panel URL does not return 200 OK"
			echo "DEBUG: Response from specific stats URL:"
			curl -I -k --connect-timeout 10 --max-time 30 "$SPECIFIC_STATS_URL" 2>&1 | head -10
			SPECIFIC_STATS_TEST=false
		fi
		
	else
		echo "DEBUG: ❌ FAILED - Could not find stats admin URL"
		STATS_URL_TEST=false
		STATS_ACCESS_TEST=false
		STATS_CONTENT_TEST=false
		SPECIFIC_STATS_TEST=false
	fi
	
	# Summary
	echo "DEBUG: === STATS PANEL TEST SUMMARY ==="
	if [ "$STATS_URL_TEST" = true ] && [ "$STATS_ACCESS_TEST" = true ] && [ "$STATS_CONTENT_TEST" = true ] && [ "$SPECIFIC_STATS_TEST" = true ]; then
		echo "DEBUG: 🎉 ALL STATS PANEL TESTS PASSED - Stats panel is working correctly"
		echo "DEBUG: - Stats admin URL found: ✅"
		echo "DEBUG: - Stats panel access (200 OK): ✅"
		echo "DEBUG: - Stats panel content: ✅"
		echo "DEBUG: - Specific stats URL: ✅"
	else
		echo "DEBUG: ⚠️  SOME STATS PANEL TESTS FAILED"
		echo "DEBUG: - Stats admin URL found: $STATS_URL_TEST"
		echo "DEBUG: - Stats panel access (200 OK): $STATS_ACCESS_TEST"
		echo "DEBUG: - Stats panel content: $STATS_CONTENT_TEST"
		echo "DEBUG: - Specific stats URL: $SPECIFIC_STATS_TEST"
		tail_server_log
	fi
	
	echo === END STATS PANEL TESTS ===
	
	# Test file upload functionality
	echo "DEBUG: Testing file upload functionality..."
	echo === FILE UPLOAD TEST ===
	
	# Get the upload admin URL
	echo "DEBUG: Test 1 - Finding upload admin URL..."
	UPLOAD_ADMIN_PATH=$(echo "$ADMIN_URLS" | grep -o "upload_[a-f0-9]*" | head -1)
	if [ -z "$UPLOAD_ADMIN_PATH" ]; then
		echo "DEBUG: ❌ FAILED - Could not find upload admin URL"
		UPLOAD_URL_TEST=false
	else
		UPLOAD_ADMIN_URL="/$UPLOAD_ADMIN_PATH"
		echo "DEBUG: ✅ SUCCESS - Upload admin URL found: $UPLOAD_ADMIN_URL"
		UPLOAD_URL_TEST=true
		
		# Test 2: Test upload panel access
		echo "DEBUG: Test 2 - Testing upload panel access..."
		FULL_UPLOAD_URL="https://$SRV$UPLOAD_ADMIN_URL"
		if curl -k --connect-timeout 10 --max-time 30 "$FULL_UPLOAD_URL" 2>/dev/null | grep -q "File Upload Manager"; then
			echo "DEBUG: ✅ SUCCESS - Upload panel accessible"
			UPLOAD_ACCESS_TEST=true
		else
			echo "DEBUG: ❌ FAILED - Upload panel not accessible"
			curl -k --connect-timeout 10 --max-time 30 "$FULL_UPLOAD_URL" 2>&1 | head -10
			UPLOAD_ACCESS_TEST=false
		fi
		
		# Test 3: Test file upload via curl
		echo "DEBUG: Test 3 - Testing file upload via curl..."
		TEST_FILE="test_upload_file.txt"
		echo "This is a test file for upload testing. $(date)" > "$TEST_FILE"
		
		if curl -k --connect-timeout 10 --max-time 30 \
			-F "file=@$TEST_FILE" \
			"$FULL_UPLOAD_URL" | grep -q "Upload Successful"; then
			echo "DEBUG: ✅ SUCCESS - File upload succeeded"
			UPLOAD_SUBMIT_TEST=true
		else
			echo "DEBUG: ❌ FAILED - File upload failed"
			curl -k --connect-timeout 10 --max-time 30 \
				-F "file=@$TEST_FILE" \
				"$FULL_UPLOAD_URL" | head -20
			UPLOAD_SUBMIT_TEST=false
		fi
		
		# Test 4: Check if file was actually saved
		echo "DEBUG: Test 4 - Checking if uploaded file was saved..."
		if ssh root@$SRV "test -f /var/www/html/uploads/$TEST_FILE"; then
			echo "DEBUG: ✅ SUCCESS - File was saved to uploads directory"
			UPLOAD_SAVE_TEST=true
			
			# Test 5: Check file contents
			echo "DEBUG: Test 5 - Checking file contents..."
			if ssh root@$SRV "grep -q 'test file for upload testing' /var/www/html/uploads/$TEST_FILE"; then
				echo "DEBUG: ✅ SUCCESS - File contents are correct"
				UPLOAD_CONTENT_TEST=true
			else
				echo "DEBUG: ❌ FAILED - File contents are incorrect"
				UPLOAD_CONTENT_TEST=false
			fi
		else
			echo "DEBUG: ❌ FAILED - File was not saved to uploads directory"
			UPLOAD_SAVE_TEST=false
			UPLOAD_CONTENT_TEST=false
		fi
		
		# Test 6: List uploaded files
		echo "DEBUG: Test 6 - Testing file listing..."
		if curl -s -k --connect-timeout 10 --max-time 30 "$FULL_UPLOAD_URL" | grep -q "$TEST_FILE"; then
			echo "DEBUG: ✅ SUCCESS - Uploaded file appears in file list"
			UPLOAD_LIST_TEST=true
		else
			echo "DEBUG: ❌ FAILED - Uploaded file does not appear in file list"
			UPLOAD_LIST_TEST=false
		fi
		
		# Cleanup
		rm -f "$TEST_FILE"
		ssh root@$SRV "rm -f /var/www/html/uploads/$TEST_FILE" || true
	fi
	
	# Summary
	echo "DEBUG: === FILE UPLOAD TEST SUMMARY ==="
	if [ "$UPLOAD_URL_TEST" = true ] && [ "$UPLOAD_ACCESS_TEST" = true ] && [ "$UPLOAD_SUBMIT_TEST" = true ] && [ "$UPLOAD_SAVE_TEST" = true ] && [ "$UPLOAD_CONTENT_TEST" = true ] && [ "$UPLOAD_LIST_TEST" = true ]; then
		echo "DEBUG: 🎉 ALL FILE UPLOAD TESTS PASSED - File upload functionality is working correctly"
		echo "DEBUG: - Upload admin URL found: ✅"
		echo "DEBUG: - Upload panel access (200 OK): ✅"
		echo "DEBUG: - File upload submission: ✅"
		echo "DEBUG: - File saved to disk: ✅"
		echo "DEBUG: - File contents correct: ✅"
		echo "DEBUG: - File listed in manager: ✅"
	else
		echo "DEBUG: ⚠️  SOME FILE UPLOAD TESTS FAILED"
		echo "DEBUG: - Upload admin URL found: $UPLOAD_URL_TEST"
		echo "DEBUG: - Upload panel access: $UPLOAD_ACCESS_TEST"
		echo "DEBUG: - File upload submission: $UPLOAD_SUBMIT_TEST"
		echo "DEBUG: - File saved to disk: $UPLOAD_SAVE_TEST"
		echo "DEBUG: - File contents correct: $UPLOAD_CONTENT_TEST"
		echo "DEBUG: - File listed in manager: $UPLOAD_LIST_TEST"
		tail_server_log
	fi
	
	echo === END FILE UPLOAD TESTS ===
	
	if [ -z "$KEEPALIVE" ]
	then
		ssh root@$SRV "pkill easyp; sleep 1; pkill -9 easyp" || echo "DEBUG: Server process cleanup completed"
		echo "DEBUG: Stopping server process on remote server..."
	fi
	
	echo "DEBUG: Test script completed"
        
fi

echo Finished at: `date`
