#!/usr/bin/env bash # ============================================================= # Dragon Wind — First-Run Setup # Generates a .env file with randomized ports if one doesn't # already exist, then optionally starts the stack. # ============================================================= set -euo pipefail ENV_FILE="$(dirname "$0")/.env" # ---- Random port helper (avoids well-known ports) ----------- rand_port() { # Pick a random port in range 10000–59999 echo $(( RANDOM % 50000 + 10000 )) } # ---- Only generate .env if it doesn't exist ----------------- if [ -f "$ENV_FILE" ]; then echo "✅ .env already exists — skipping generation." echo " Edit $ENV_FILE to change ports or credentials." else WEB_PORT=$(rand_port) RELAY_TCP_PORT=$(rand_port) RELAY_UDP_PORT=$(rand_port) # Ensure all three are distinct while [ "$RELAY_TCP_PORT" -eq "$WEB_PORT" ]; do RELAY_TCP_PORT=$(rand_port); done while [ "$RELAY_UDP_PORT" -eq "$WEB_PORT" ] || [ "$RELAY_UDP_PORT" -eq "$RELAY_TCP_PORT" ]; do RELAY_UDP_PORT=$(rand_port) done cat > "$ENV_FILE" <