-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_server.sh
executable file
·247 lines (207 loc) · 5.17 KB
/
run_server.sh
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/bin/bash
create_workdir() {
if [ -d "$1" ]
then
echo "Workdir \"$1\" already exist!" >&2
return 1
fi
if ! mkdir "$1"
then
echo "Cannot create workdir \"$1\"" >&2
return 1
fi
if ! test -f config/fc_auth.conf ; then
echo "Database config file config/fc_auth.conf missing" >&2
return 1
fi
if ! ln -s ../../config/fc_auth.conf "$1/"
then
echo "Cannot setup fc_auth.conf for \"$1\"" >&2
return 1
fi
echo "Using Freeciv version \"$2\" with \"$3\" ruleset" > "$1/fcversion.txt"
}
MAINDIR=$(cd $(dirname $0) ; pwd)
cd $MAINDIR
if test "$1" = "-h" || test "$1" = "--help" || test "$1" = "" ; then
echo "Usage: $(basename $0) <server version> [port] [metaserver info on/off=off] [ruleset] [extra config] [savegame]"
exit 0
fi
if test -f config/setup ; then
. config/setup
else
echo "No local setup file config/setup found!"
exit 1
fi
declare -i RAND_WAIT
if test "$FCTEST" != "" ; then
WDH="testruns"
RAND_WAIT=0
else
WDH="workdirs"
# Randomized wait so multiple servers do not lauch simultaneously
# Set WAIT_RAND_MAX and CONST_WAIT in config/setup
if test "$WAIT_RAND_MAX" = "" ; then
WAIT_RAND_MAX=40
fi
RAND_WAIT=${RANDOM}%${WAIT_RAND_MAX}
if test "$CONST_WAIT" != "" ; then
RAND_WAIT=${RAND_WAIT}+${CONST_WAIT}
fi
fi
if ! test -d "$WDH" || ! test -w "$WDH" ; then
echo "There's no directory \"$WDH\" with write permissions" >&2
exit 1
fi
RUNLOG="$WDH/runlog.log"
VERSION="$1"
if test "$5" != "-" ; then
if test "$5" != "" ; then
EXTRA_CONFIG="$5"
else
EXTRA_CONFIG="-d 3 -N"
fi
fi
if test "$6" != "" ; then
SAVEGAME="$6"
SAVEGAME_PARAM="-f $6"
else
SAVEGAME_PARAM=""
fi
if [ "$VERSION" = "" ] || ! [ -d "builds/$VERSION" ]
then
NOSER=true
else
SERDIR=$(cd "builds/$VERSION" ; pwd)
if ! test -x "$SERDIR/fcser"
then
NOSER=true
fi
fi
if [ "$NOSER" = "true" ]
then
echo "No server $VERSION/fcser"
exit 1
fi
if test "$4" = "rand" ; then
if ! test -f rulesets/$VERSION/rand.conf ; then
echo "Random ruleset requested, but there is no rand.conf" >&2
exit 1
fi
declare -i TOTALRAND=0
declare -i RANDOPTIONS=0
RULESET=$(cat rulesets/$VERSION/rand.conf | (
while read RS WEIGHT
do
RULESETS[$RANDOPTIONS]=$RS
WEIGHTS[$RANDOPTIONS]=$WEIGHT
TOTALRAND=$TOTALRAND+$WEIGHT
RANDOPTIONS=$RANDOPTIONS+1
done
declare -i RANDRULESET=$RANDOM%$TOTALRAND
RANDOPTIONS=0
while test $RANDRULESET -ge ${WEIGHTS[$RANDOPTIONS]}
do
RANDRULESET=$RANDRULESET-${WEIGHTS[$RANDOPTIONS]}
RANDOPTIONS=$RANDOPTIONS+1
done
echo ${RULESETS[$RANDOPTIONS]}
))
echo RULESET: $RULESET
if ! test -f rulesets/$VERSION/$RULESET.serv ; then
echo "No ruleset $RULESET available"
exit 1
fi
elif test "$4" != "" && test "$4" != "default" ; then
if ! test -f rulesets/$VERSION/$4.serv ; then
echo "No ruleset $4 available"
exit 1
fi
RULESET="$4"
else
RULESET="default"
fi
if test "$2" = ""
then
PORT_NAME="(default)"
PORT_PARAM=""
else
PORT_NAME="$2"
PORT_PARAM="-p $2"
fi
if test "$3" != "" && test "$3" != "on" && test "$3" != "off" ; then
echo "Illegal meta parameter \"$3\"" >&2
exit 1
fi
if test "$IDENTITY" = "" ; then
IDENT_PARAM=""
else
IDENT_PARAM="-i $IDENTITY"
fi
sleep $RAND_WAIT
if ! test -f $WDH/id.txt ; then
GAMEID=0
else
GAMEID=$(cat $WDH/id.txt)
fi
((GAMEID=GAMEID+1))
echo -n "$GAMEID" > $WDH/id.txt
if test "$RULESET" != "" && test "$RULESET" != "default"
then
TOPIC="$SERVERDESC / $RULESET (Game #$GAMEID)"
elif test "$SAVEGAME" != ""
then
TOPIC="$SERVERDESC \"$(basename $SAVEGAME | sed 's/\.sav.*//')\" (Game #$GAMEID)"
else
TOPIC="$SERVERDESC (Game #$GAMEID)"
fi
GAMEDIR="g${GAMEID}_p$$"
WORKDIR="$WDH/$GAMEDIR"
if ! create_workdir "$WORKDIR" "$VERSION" "$RULESET"
then
exit 1
fi
if test "$3" = "on" ; then
META_PARAM="-k"
if test "$METASERVER" != "" ; then
META_PARAM="$META_PARAM -M $METASERVER"
fi
else
META_PARAM=""
fi
STARTSTAMP=$(date +"%d.%m.%y %H:%M:%S")
STARTMSG="$STARTSTAMP : Server $PORT_NAME ($VERSION:$RULESET) start: \"$WORKDIR\""
echo "$STARTMSG"
echo "$STARTMSG" >> $RUNLOG
if test -f $MAINDIR/rulesets/$VERSION/ranking ; then
RANK_PARAM="-R ranking.log"
else
RANK_PARAM=
fi
cd "$WORKDIR"
export FREECIV_DATA_PATH="$MAINDIR/rulesets/$VERSION"
SERCMDLINE="$SERDIR/fcser -e -q 300 -l fc.log -a -D fc_auth.conf -A none $SAVEGAME_PARAM $RANK_PARAM $META_PARAM $PORT_PARAM $IDENT_PARAM -S $GAMEID $EXTRA_CONFIG"
echo "$SERCMDLINE" > cmdline.txt
ulimit -c 50000
(
echo "metamessage $TOPIC"
if test "$HOMEPAGE" != "" ; then
echo "metapatches See $HOMEPAGE"
fi
echo "set scorelog enabled"
if test "$RULESET" != "default" ; then
echo "read $MAINDIR/rulesets/$VERSION/$RULESET.serv"
fi
if test -e $MAINDIR/rulesets/$VERSION/$RULESET.msg ; then
echo "connectmsg $(cat $MAINDIR/rulesets/$VERSION/$RULESET.msg)"
fi
echo -n
) | $SERCMDLINE 2>stderr.log >stdout.log
cd $MAINDIR
if test -x analyze_workdir.sh ; then
./analyze_workdir.sh "$WORKDIR" "$VERSION" "$RULESET" "$GAMEID"
fi
STOPSTAMP=$(date +"%d.%m.%y %H:%M:%S")
STOPMSG="$STOPSTAMP : Server $PORT_NAME ($VERSION:$RULESET) finished: \"$WORKDIR\""
echo "$STOPMSG"
echo "$STOPMSG" >> $RUNLOG