summaryrefslogtreecommitdiff
path: root/start.sh
blob: b68a234754d4ce0287432fb47ce8c3634c188c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env sh
# Quick-start script for Player testing
# Usage: ./start.sh [restart|stop|status]
set -e

MEDIA_ROOT="${MEDIA_ROOT:-/data/nfs/earthdata/playtest}"
DB_PATH="${DB_PATH:-/tmp/playtest.db}"
PORT="${PORT:-18080}"
PIDFILE="/tmp/player.pid"
COOKIE="/tmp/player_cookies.txt"
LOGFILE="/tmp/player.log"
TAIL_PID=""

BASEURL="http://localhost:${PORT}"
BINARY="${BINARY:-./player}"

stop() {
  if [ -f "$PIDFILE" ]; then
    pid=$(cat "$PIDFILE")
    if kill -0 "$pid" 2>/dev/null; then
      echo "Stopping server (PID $pid)..."
      kill -TERM "$pid" 2>/dev/null || true
      sleep 1
      kill -0 "$pid" 2>/dev/null && kill -9 "$pid" 2>/dev/null || true
    fi
    rm -f "$PIDFILE"
  fi
}

bootstrap_auth() {
  echo "Bootstrapping admin user..."
  curl -fs "${BASEURL}/api/bootstrap" \
    -d '{"username":"test","password":"test123"}' \
    -H 'Content-Type: application/json' > /dev/null 2>&1 || true

  echo "Logging in..."
  curl -fs "${BASEURL}/api/login" \
    -d '{"username":"test","password":"test123"}' \
    -H 'Content-Type: application/json' \
    -c "$COOKIE" > /dev/null 2>&1 || true
}

case "${1:-run}" in
  stop)
    stop
    exit 0
    ;;
  status)
    if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then
      echo "Running (PID $(cat "$PIDFILE"))"
    else
      echo "Not running"
    fi
    exit 0
    ;;
esac

cd "$(dirname "$0")"

# Build unconditionally — this script is for development
echo "Building player binary..."
go build -o player ./cmd/mediaplayer

stop

# Restart action backgrounds; default run blocks in foreground.
BACKGROUND=false
if [ "${1:-run}" = "restart" ]; then
  BACKGROUND=true
fi

# Start server in foreground so Ctrl+C kills it directly.
export MEDIA_ROOT DB_PATH PORT LOG_LEVEL="${LOG_LEVEL:-debug}"
: > "$LOGFILE"
"$BINARY" > "$LOGFILE" 2>&1 &
SERVER_PID=$!
echo "$SERVER_PID" > "$PIDFILE"
echo "Server started (PID $SERVER_PID) at ${BASEURL}"
echo "Server log: $LOGFILE"

sleep 2

bootstrap_auth

echo "Library rescan starting..."
curl -fs -X POST "${BASEURL}/api/admin/rescan" -b "$COOKIE" > /dev/null 2>&1 || true
echo "Rescan running in background (check tail -f $LOGFILE)"

if [ "$BACKGROUND" != "true" ]; then
  tail -n +1 -f "$LOGFILE" &
  TAIL_PID=$!
fi

echo ""
echo "Ready. Open ${BASEURL} in your browser."
echo "Login: test / test123"
echo ""
echo "Quick tips:"
echo "  s  -- show/hide sets sidebar"
echo "  t  -- show/hide toolbar"
echo "  /  -- search"
echo "  ?  -- help"

if [ "$BACKGROUND" = "true" ]; then
  exit 0
fi

# Foreground mode: trap ctrl+c so when this script exits,the player also stops.
shutdown_server() {
  echo ""
  echo "Shutting down..."
  if [ -n "$TAIL_PID" ]; then
    kill "$TAIL_PID" 2>/dev/null || true
  fi
  kill -TERM "$SERVER_PID" 2>/dev/null || true
  wait "$SERVER_PID" 2>/dev/null || true
  rm -f "$PIDFILE"
  exit 0
}
trap shutdown_server INT TERM EXIT
wait "$SERVER_PID"