#!/data/data/com.termux/files/usr/bin/bash

APP_DIR="$HOME/storage/shared/Documents/repos/phone-message-board"
APP_FILE="$APP_DIR/app.py"
LOG_FILE="$APP_DIR/message-board.log"

case "$1" in
  start)
    if pgrep -f "$APP_FILE" >/dev/null; then
      echo "Message board already running."
      exit 0
    fi

    cd "$APP_DIR" || exit 1
    nohup python app.py > "$LOG_FILE" 2>&1 &
    echo "Message board started."
    echo "Open: http://192.168.44.1:8080"
    ;;

  stop)
    pkill -f "$APP_FILE" && echo "Message board stopped." || echo "Message board was not running."
    ;;

  restart)
    pkill -f "$APP_FILE" 2>/dev/null
    cd "$APP_DIR" || exit 1
    nohup python app.py > "$LOG_FILE" 2>&1 &
    echo "Message board restarted."
    echo "Open: http://192.168.44.1:8080"
    ;;

  status)
    if pgrep -f "$APP_FILE" >/dev/null; then
      echo "Message board is running."
      echo "Open: http://192.168.44.1:8080"
    else
      echo "Message board is stopped."
    fi
    ;;

  logs)
    tail -n 50 "$LOG_FILE"
    ;;

  *)
    echo "Usage: message-board {start|stop|restart|status|logs}"
    exit 1
    ;;
esac
