#!/bin/bash # Audiobook Player - Start Script SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PID_FILE="$SCRIPT_DIR/app.pid" PORT=5000 show_help() { echo "📚 Audiobook Player" echo "" echo "Usage:" echo " ./start.sh start # Start server (foreground)" echo " ./start.sh start-bg # Start server (background)" echo " ./start.sh stop # Stop server" echo " ./start.sh status # Check status" echo " ./start.sh restart # Restart server" echo "" echo "URLs:" echo " Local: http://localhost:$PORT" echo " Public: http://$(curl -s ifconfig.me 2>/dev/null || echo 'YOUR_IP'):$PORT" } check_requirements() { python3 -c "import flask, edge_tts" 2>/dev/null if [ $? -ne 0 ]; then echo "Installing requirements..." pip3 install -r "$SCRIPT_DIR/requirements.txt" fi } start_server() { check_requirements if [ -f "$PID_FILE" ]; then PID=$(cat "$PID_FILE") if ps -p "$PID" > /dev/null 2>&1; then echo "âš ī¸ Server already running (PID: $PID)" echo " Visit: http://localhost:$PORT" return fi fi echo "🚀 Starting Audiobook Player..." echo " Port: $PORT" echo "" cd "$SCRIPT_DIR" python3 app.py & # Save PID echo $! > "$PID_FILE" sleep 2 if [ -f "$PID_FILE" ]; then PID=$(cat "$PID_FILE") if ps -p "$PID" > /dev/null 2>&1; then echo "✅ Server started (PID: $PID)" echo "" echo "📱 Access URLs:" echo " Local: http://localhost:$PORT" PUBLIC_IP=$(curl -s ifconfig.me 2>/dev/null) if [ -n "$PUBLIC_IP" ]; then echo " Public: http://$PUBLIC_IP:$PORT" fi fi fi } start_bg() { check_requirements if [ -f "$PID_FILE" ]; then PID=$(cat "$PID_FILE") if ps -p "$PID" > /dev/null 2>&1; then echo "âš ī¸ Server already running (PID: $PID)" return fi fi echo "🚀 Starting Audiobook Player in background..." cd "$SCRIPT_DIR" nohup python3 app.py > "$SCRIPT_DIR/server.log" 2>&1 & echo $! > "$PID_FILE" sleep 2 PID=$(cat "$PID_FILE") if ps -p "$PID" > /dev/null 2>&1; then echo "✅ Server started (PID: $PID)" echo "" echo "📱 Access URLs:" PUBLIC_IP=$(curl -s ifconfig.me 2>/dev/null) if [ -n "$PUBLIC_IP" ]; then echo " http://$PUBLIC_IP:$PORT" fi echo " http://localhost:$PORT" echo "" echo "Logs: tail -f $SCRIPT_DIR/server.log" fi } stop_server() { if [ -f "$PID_FILE" ]; then PID=$(cat "$PID_FILE") if ps -p "$PID" > /dev/null 2>&1; then echo "âšī¸ Stopping server (PID: $PID)..." kill "$PID" rm -f "$PID_FILE" echo "✅ Server stopped" else echo "âš ī¸ Server not running" rm -f "$PID_FILE" fi else # Try to find and kill by port PID=$(lsof -ti:$PORT 2>/dev/null) if [ -n "$PID" ]; then echo "âšī¸ Stopping server (PID: $PID)..." kill "$PID" 2>/dev/null echo "✅ Server stopped" else echo "âš ī¸ Server not running" fi fi } show_status() { if [ -f "$PID_FILE" ]; then PID=$(cat "$PID_FILE") if ps -p "$PID" > /dev/null 2>&1; then echo "✅ Server is running (PID: $PID)" echo " Port: $PORT" echo "" echo "📱 Access URLs:" echo " http://localhost:$PORT" PUBLIC_IP=$(curl -s ifconfig.me 2>/dev/null) if [ -n "$PUBLIC_IP" ]; then echo " http://$PUBLIC_IP:$PORT" fi else echo "âšī¸ Server not running (stale PID file)" rm -f "$PID_FILE" fi else PID=$(lsof -ti:$PORT 2>/dev/null) if [ -n "$PID" ]; then echo "✅ Server is running (PID: $PID)" else echo "âšī¸ Server not running" fi fi } case "${1:-help}" in start) start_server ;; start-bg|bg) start_bg ;; stop) stop_server ;; status) show_status ;; restart) stop_server sleep 2 start_bg ;; help|--help|-h) show_help ;; *) echo "❌ Unknown command: $1" show_help exit 1 ;; esac