-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.sh
More file actions
executable file
·171 lines (142 loc) · 4.82 KB
/
Copy pathmain.sh
File metadata and controls
executable file
·171 lines (142 loc) · 4.82 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(dirname "$0")"
SCRIPT_DIR="$(realpath $SCRIPT_DIR)"
cd $SCRIPT_DIR || exit 1
ENV_FILE_NAME=config.env
COMPOSE_FILE="$(realpath ./docker/docker-compose.yml)"
# --------------------------------------------------
# Load .env file if present (docker-compose style)
# --------------------------------------------------
if [ -f "$SCRIPT_DIR/$ENV_FILE_NAME" ]; then
echo "Loading configuration from $SCRIPT_DIR/$ENV_FILE_NAME"
set -o allexport
# shellcheck disable=SC1090
source "$SCRIPT_DIR/$ENV_FILE_NAME"
set +o allexport
fi
docker(){
sudo -E docker $@
}
is_true() {
case "${1,,}" in
1|true) return 0 ;;
0|false) return 1 ;;
*)
echo "Invalid boolean value: $1" >&2
return 2
;;
esac
}
start_local(){
# --------------------------------------------------
# Defaults (can be overridden by .env or env vars)
# --------------------------------------------------
: "${ELO_RATING:=-1}"
: "${GAME_TIMER_MS:=150000}"
: "${FIRST_MOVE_W:=e2e4}"
: "${NEXT_GAME_AUTO:=True}"
: "${STOCKFISH_VERSION:=sf_16}"
: "${STOCKFISH_ARCHIVE:=stockfish-ubuntu-x86-64.tar}"
: "${STOCKFISH_URL:=https://github.com/official-stockfish/Stockfish/releases/download/${STOCKFISH_VERSION}/${STOCKFISH_ARCHIVE}}"
VENV="$SCRIPT_DIR/venv"
STOCKFISH_DIR="$SCRIPT_DIR/stockfish"
STOCKFISH_PATH="$STOCKFISH_DIR/stockfish"
# --------------------------------------------------
# Virtualenv setup
# --------------------------------------------------
if [ ! -d "$VENV" ]; then
echo "Creating virtualenv..."
if ! command -v virtualenv >/dev/null 2>&1; then
echo "Installing virtualenv..."
pip install virtualenv
fi
virtualenv "$VENV"
"$VENV/bin/pip" install -r "$SCRIPT_DIR/requirements.txt"
fi
[ ! -d $STOCKFISH_DIR ] && mkdir -p $STOCKFISH_DIR
# --------------------------------------------------
# Stockfish setup
# --------------------------------------------------
if [ ! -f "$STOCKFISH_PATH" ]; then
echo "Downloading and extracting Stockfish..."
mkdir -p "$STOCKFISH_DIR"
command -v wget >/dev/null 2>&1 || { echo "wget not found"; exit 1; }
command -v tar >/dev/null 2>&1 || { echo "tar not found"; exit 1; }
wget -O "$STOCKFISH_ARCHIVE" "$STOCKFISH_URL"
tar -xvf "$STOCKFISH_ARCHIVE"
mv stockfish/stockfish-ubuntu-x86-64 "$STOCKFISH_PATH"
chmod +x "$STOCKFISH_PATH"
rm "$STOCKFISH_ARCHIVE"
fi
#exec "$VENV/bin/python" src/main.py --help
# -------------------------------------------------
# Run application
# --------------------------------------------------
exec "$VENV/bin/python" src/main.py \
--elo-rating "$ELO_RATING" \
--game-timer-ms "$GAME_TIMER_MS" \
--first-move-w "$FIRST_MOVE_W" \
--enable-move-delay "$ENABLE_MOVE_DELAY" \
--next-game-auto "$START_NEW_GAME_AUTOMATICALLY" \
--autostart "$AUTOSTART_FIRST_GAME" \
--game-type="$GAME_TYPE"
}
start_docker(){
if $(is_true $OPEN_BROWSER) && [[ "$compose_args" = *"up"* ]]; then
echo "Opening container URLs automatically"
open_ports_in_webbrowser &
fi
echo "Compose args: $compose_args; Compose file: $COMPOSE_FILE"
docker compose -f $COMPOSE_FILE $compose_args
}
wait_for_port() {
local port="$1"
echo "Waiting for localhost:${port}..."
# Wait until TCP connection succeeds
until nc -z localhost "$port" 2>/dev/null; do
sleep 0.5
done
echo "Port ${port} is reachable."
}
open_port() {
local port="$1"
wait_for_port "$port"
python3 -m webbrowser "http://localhost:${port}?autoconnect=1&resize=scale"
}
open_ports_in_webbrowser(){
if [[ -z "${REPLICA_PORTS:-}" ]]; then
echo "Error: REPLICA_PORTS is not set"
exit 1
fi
# Range case
if [[ "$REPLICA_PORTS" =~ ^([0-9]+)-([0-9]+)$ ]]; then
start="${BASH_REMATCH[1]}"
end="${BASH_REMATCH[2]}"
if (( start > end )); then
echo "Error: Invalid range ($start > $end)"
exit 1
fi
for ((port=start; port<=end; port++)); do
open_port "$port"
done
# Single port case
elif [[ "$REPLICA_PORTS" =~ ^[0-9]+$ ]]; then
open_port "$REPLICA_PORTS"
else
echo "Error: REPLICA_PORTS must be a single port (e.g. 7910) or range (e.g. 7910-7913)"
exit 1
fi
}
compose_args="${@:1}"
if [ -z "$compose_args" ]; then
compose_args="up --build"
fi
if [ "$STARTUP_TYPE" = "docker" ]; then
start_docker
elif [ "$STARTUP_TYPE" = "local" ]; then
start_local
else
echo "Unknown startup type: $STARTUP_TYPE. Possible values ($ENV_FILE_NAME): docker / local"
exit 1
fi