50 lines
947 B
Bash
Executable File
50 lines
947 B
Bash
Executable File
#!/bin/bash
|
|
|
|
BG=false
|
|
for arg in "$@"; do
|
|
[[ "$arg" == "bg" || "$arg" == "--bg" ]] && BG=true
|
|
done
|
|
|
|
log() { $BG || echo "$@"; }
|
|
|
|
# Find the HTML file
|
|
HTML_FILE=$(find . -maxdepth 2 -name "*.html" | head -1)
|
|
|
|
if [ -z "$HTML_FILE" ]; then
|
|
log "No HTML file found."
|
|
exit 1
|
|
fi
|
|
|
|
# Pick a free port
|
|
PORT=8000
|
|
while lsof -i :$PORT &>/dev/null; do
|
|
PORT=$((PORT + 1))
|
|
done
|
|
|
|
DIR=$(dirname "$HTML_FILE")
|
|
FILE=$(basename "$HTML_FILE")
|
|
|
|
log "Serving $HTML_FILE on http://localhost:$PORT/$FILE"
|
|
|
|
open_browser() {
|
|
local url=$1
|
|
if command -v xdg-open &>/dev/null; then
|
|
xdg-open "$url"
|
|
elif command -v open &>/dev/null; then
|
|
open "$url"
|
|
elif command -v start &>/dev/null; then
|
|
start "$url"
|
|
else
|
|
log "open: $url"
|
|
fi
|
|
}
|
|
|
|
(sleep 0.5 && open_browser "http://localhost:$PORT/$FILE") &
|
|
|
|
if $BG; then
|
|
python3 -m http.server "$PORT" --directory "$DIR" &>/dev/null &
|
|
disown
|
|
else
|
|
python3 -m http.server "$PORT" --directory "$DIR"
|
|
fi
|