fixed missing fallback if serverside websocket fails
[brisk.git] / INSTALL.sh
1 #!/bin/bash
2 # set -x
3 #
4 # Defaults
5 #
6 CONFIG_FILE="$HOME/.brisk_install"
7
8 apache_conf="/etc/apache2/sites-available/default"
9 players_n=3
10 tables_n=44
11 tables_auth_n=12
12 brisk_auth_conf="brisk_spu_auth.conf.pho"
13 brisk_debug="0x0400"
14 # brisk_debug="0xffff"
15 web_path="/home/nastasi/web/brisk"
16 ftok_path="/home/nastasi/brisk-priv/ftok/brisk"
17 proxy_path="/home/nastasi/brisk-priv/proxy/brisk"
18 usock_path="/home/nastasi/brisk-priv/brisk.sock"
19 sys_user="www-data"
20 legal_path="/home/nastasi/brisk-priv/brisk"
21 prefix_path="/brisk/"
22 brisk_conf="brisk_spu.conf.pho"
23 web_only="FALSE"
24
25 #
26 # functions
27 function usage () {
28     echo
29     echo "$1 -h"
30     echo "$1 chk                          - run lintian on all ph* files."
31     echo "$1 pkg                          - build brisk packages."
32     echo "$1 [-W] [-n 3|5] [-t <(n>=4)>] [-T <auth_tab>] [-A <apache-conf>] [-a <auth_file_name>] [-f <conffile>] [-p <outconf>] [-U <usock_path>] [-u <sys_user>] [-d <TRUE|FALSE>] [-w <web_dir>] [-k <ftok_dir>] [-l <legal_path>] [-y <proxy_path>] [-P <prefix_path>]"
33     echo "  -h this help"
34     echo "  -f use this config file"
35     echo "  -p save preferences in the file"
36     echo "  -W web files only"
37     echo "  -A apache_conf                  - def. $apache_conf"
38     echo "  -n number of players            - def. $players_n"
39     echo "  -t number of tables             - def. $tables_n"
40     echo "  -T number of auth-only tables   - def. $tables_auth_n"
41     echo "  -a authorization file name      - def. \"$brisk_auth_conf\""
42     echo "  -d activate dabug               - def. $brisk_debug"
43     echo "  -w dir where place the web tree - def. \"$web_path\""
44     echo "  -k dir where place ftok files   - def. \"$ftok_path\""
45     echo "  -l dir where save logs          - def. \"$legal_path\""
46     echo "  -y dir where place proxy files  - def. \"$proxy_path\""
47     echo "  -P prefix path                  - def. \"$prefix_path\""
48     echo "  -C config filename              - def. \"$brisk_conf\""
49     echo "  -U unix socket path             - def. \"$usock_path\""
50     echo "  -u system user to run brisk dae - def. \"$sys_user\""
51     echo
52 }
53
54 function get_param () {
55     echo "X$2" | grep -q "^X$1\$"
56     if [ $? -eq 0 ]; then
57         # echo "DECHE" >&2
58         echo "$3"
59         return 2
60     else
61         # echo "DELA" >&2
62         echo "$2" | cut -c 3-
63         return 1
64     fi
65     return 0
66 }
67
68 function searchetc() {
69     local dstart dname pp
70     dstart="$1"
71     dname="$2"
72
73     pp="$dstart"
74     while [ "$pp" != "/" ]; do
75         if [ -d "$pp/$dname" ]; then
76             echo "$pp/$dname"
77             return 0
78         fi
79         pp="$(dirname "$pp")"
80     done
81     
82     return 1
83 }
84
85 #
86 #  MAIN
87 #
88 if [ "$1" = "chk" ]; then
89     set -e
90     oldifs="$IFS"
91     IFS='
92 '
93     for i in $(find -name '*.pho' -o -name '*.phh' -o -name '*.php'); do
94         php5 -l $i
95     done
96     exit 0
97 fi
98
99 # before all check errors on the sources
100 $0 chk || exit 3
101
102 if [ "$1" = "pkg" ]; then
103     if [ "$2" != "" ]; then
104         tag="$2"
105     else
106         tag="$(git describe)"
107     fi
108     nam1="brisk_${tag}.tgz"
109     nam2="brisk-img_${tag}.tgz"
110     echo "Build packages ${nam1} and ${nam2}."
111     read -p "Proceed [y/n]: " a
112     if [ "$a" != "y" -a  "$a" != "Y" ]; then
113         exit 1
114     fi
115     git archive --format=tar --prefix=brisk-${tag}/brisk/ $tag | gzip > ../$nam1
116     cd ../brisk-img
117     git archive --format=tar --prefix=brisk-${tag}/brisk-img/ $tag | gzip > ../$nam2
118     cd -
119     exit 0
120 fi
121
122 if [ -f "$CONFIG_FILE" ]; then
123    source "$CONFIG_FILE"
124 fi
125
126 if [ "x$prefix_path" = "x" ]; then
127    prefix_path="$web_path"
128 fi
129
130 action=""
131 while [ $# -gt 0 ]; do
132     # echo aa $1 xx $2 bb
133     conffile=""
134     case $1 in
135         -A*) apache_conf="$(get_param "-A" "$1" "$2")"; sh=$?;;
136         -f*) conffile="$(get_param "-f" "$1" "$2")"; sh=$?;;
137         -p*) outconf="$(get_param "-p" "$1" "$2")"; sh=$?;;
138         -n*) players_n="$(get_param "-n" "$1" "$2")"; sh=$?;;
139         -t*) tables_n="$(get_param "-t" "$1" "$2")"; sh=$?;;
140         -T*) tables_auth_n="$(get_param "-T" "$1" "$2")"; sh=$?;;
141         -a*) brisk_auth_conf="$(get_param "-a" "$1" "$2")"; sh=$?;;
142         -d*) brisk_debug="$(get_param "-d" "$1" "$2")"; sh=$?;;
143         -w*) web_path="$(get_param "-w" "$1" "$2")"; sh=$?;;
144         -k*) ftok_path="$(get_param "-k" "$1" "$2")"; sh=$?;;
145         -y*) proxy_path="$(get_param "-y" "$1" "$2")"; sh=$?;;
146         -P*) prefix_path="$(get_param "-P" "$1" "$2")"; sh=$?;;
147         -C*) brisk_conf="$(get_param "-C" "$1" "$2")"; sh=$?;;
148         -l*) legal_path="$(get_param "-l" "$1" "$2")"; sh=$?;;
149         -U*) usock_path="$(get_param "-U" "$1" "$2")"; sh=$?;;
150         -u*) sys_user="$(get_param "-u" "$1" "$2")"; sh=$?;;
151         system) action=system;;
152         -W) web_only="TRUE";;
153         -h) usage $0; exit 0;;
154         *) usage $0; exit 1;;
155     esac
156     if [ ! -z "$conffile" ]; then
157         if [ ! -f "$conffile" ]; then
158             echo "config file [$conffile] not found"
159             exit 1
160         fi
161         . "$conffile"
162     fi
163     shift $sh
164 done
165
166 #
167 #  Show parameters
168 #
169 echo "    outconf:    \"$outconf\""
170 echo "    apache_conf:\"$apache_conf\""
171 echo "    players_n:   $players_n"
172 echo "    tables_n:   $tables_n"
173 echo "    tables_auth_n: $tables_auth_n"
174 echo "    brisk_auth_conf: \"$brisk_auth_conf\""
175 echo "    brisk_debug:\"$brisk_debug\""
176 echo "    web_path:   \"$web_path\""
177 echo "    ftok_path:  \"$ftok_path\""
178 echo "    legal_path: \"$legal_path\""
179 echo "    proxy_path: \"$proxy_path\""
180 echo "    prefix_path:\"$prefix_path\""
181 echo "    brisk_conf: \"$brisk_conf\""
182 echo "    usock_path: \"$usock_path\""
183 echo "    sys_user:   \"$sys_user\""
184 echo "    web_only:   \"$web_only\""
185
186 if [ ! -z "$outconf" ]; then
187   ( 
188     echo "#"
189     echo "#  Produced automatically by brisk::INSTALL.sh"
190     echo "#"
191     echo "apache_conf=$apache_conf"
192     echo "players_n=$players_n"
193     echo "tables_n=$tables_n"
194     echo "tables_auth_n=$tables_auth_n"
195     echo "brisk_auth_conf=\"$brisk_auth_conf\""
196     echo "brisk_debug=\"$brisk_debug\""
197     echo "web_path=\"$web_path\""
198     echo "ftok_path=\"$ftok_path\""
199     echo "proxy_path=\"$proxy_path\""
200     echo "legal_path=\"$legal_path\""
201     echo "prefix_path=\"$prefix_path\""
202     echo "brisk_conf=\"$brisk_conf\""
203     echo "usock_path=\"$usock_path\""
204     echo "sys_user=\"$sys_user\""
205     echo "web_only=\"$web_only\""
206   ) > "$outconf"
207 fi
208
209 max_players=$((40 + players_n * tables_n))
210
211 if [ "$action" = "system" ]; then
212     scrname="$(echo "$prefix_path" | sed 's@^/@@g;s@/$@@g;s@/@_@g;')"
213     echo
214     echo "script name:  [$scrname]"
215     echo "brisk path:   [$web_path]"
216     echo "private path: [$legal_path]"
217     echo "system user:  [$sys_user]"
218     echo
219     read -p "press enter to continue" sure
220     cp bin/brisk-init.sh brisk-init.sh.wrk
221     sed -i "s@^BPATH=.*@BPATH=\"${web_path}\"@g;s@^PPATH=.*@PPATH=\"${legal_path}\"@g;s@^SSUFF=.*@SSUFF=\"${scrname}\"@g;s@^BUSER=.*@BUSER=\"${sys_user}\"@g" brisk-init.sh.wrk
222
223     su -c "cp brisk-init.sh.wrk /etc/init.d/${scrname}"
224
225     rm brisk-init.sh.wrk
226     echo
227     echo "... DONE."
228     echo "DON'T FORGET: after the first installation you MUST configure your run-levels accordingly"
229     echo
230     echo "Example: su -c 'update-rc.d $scrname defaults'"
231     echo
232     exit 0
233 fi
234 #
235 #  Pre-check
236 #
237 # check for etc path existence
238 dsta="$(dirname "$web_path")"
239 etc_path="$(searchetc "$dsta" Etc)"
240 if [ $? -ne 0 ]; then
241     echo "Etc directory not found"
242     exit 1
243 fi
244
245 IFS='
246 '
247 #
248 #  Installation
249 #
250 ftokk_path="${ftok_path}k"
251
252 if [ $players_n -ne 3 -a $players_n -ne 5 ]; then
253     echo "players_n ($players_n) out of range (3|5)"
254     exit 1
255 fi
256
257 if [ "$web_only" = "FALSE" ]; then
258     if [ ! -d "$ftok_path" -a ! -d "$ftokk_path" ]; then
259         echo "ftok_path (\"$ftok_path\") not exists"
260         exit 2
261     fi
262     if [ -d "$ftok_path" -a -d "$ftokk_path" ]; then
263         echo "ftok_path (\"$ftok_path\") and ftokk_path (\"$ftokk_path\") exist, cannot continue"
264         exit 4
265     fi
266     if [ -d "$ftok_path" ]; then
267         mv "$ftok_path" "$ftokk_path"
268     fi
269     touch $ftokk_path/spy.txt >/dev/null 2>&1
270     if [ $? -ne 0 ]; then
271         echo "ftokk_path (\"$ftokk_path\") write not allowed."
272         exit 3
273     fi
274     rm $ftokk_path/spy.txt
275     
276     # create the fs subtree to enable ftok-ing
277     touch ${ftokk_path}/main
278     chmod 666 ${ftokk_path}/main
279     touch ${ftokk_path}/challenges
280     chmod 666 ${ftokk_path}/challenges
281     touch ${ftokk_path}/hardbans
282     chmod 666 ${ftokk_path}/hardbans
283     touch ${ftokk_path}/warrant
284     chmod 666 ${ftokk_path}/warrant
285     touch ${ftokk_path}/poll
286     chmod 666 ${ftokk_path}/poll
287     for i in $(seq 0 $max_players); do
288         touch ${ftokk_path}/user$i
289         chmod 666 ${ftokk_path}/user$i
290     done
291
292     if [ ! -d ${ftokk_path}/bin5 ]; then
293         mkdir ${ftokk_path}/bin5
294         chmod 777 ${ftokk_path}/bin5
295     fi
296
297     for i in $(seq 0 $((tables_n - 1))); do
298         if [ ! -d ${ftokk_path}/bin5/table$i ]; then
299             mkdir ${ftokk_path}/bin5/table$i
300         fi
301         chmod 777 ${ftokk_path}/bin5/table$i
302         touch ${ftokk_path}/bin5/table$i/table
303         chmod 666 ${ftokk_path}/bin5/table$i/table
304         for e in $(seq 0 4); do
305             touch ${ftokk_path}/bin5/table$i/user$e
306             chmod 666 ${ftokk_path}/bin5/table$i/user$e
307         done
308         # create subdirectories in proxy path
309         if [ ! -d ${proxy_path}/bin5/table$i ]; then
310             mkdir -p ${proxy_path}/bin5/table$i
311         fi
312     done
313     chmod -R 777 ${proxy_path}/bin5
314
315 fi
316 install -d ${web_path}__
317 for i in $(find web -type d | sed 's/^....//g'); do
318     install -d ${web_path}__/$i 
319 done
320
321 for i in $(find web -name '.htaccess' -o -name '*.php' -o -name '*.phh' -o -name '*.pho' -o -name '*.css' -o -name '*.js' -o -name '*.mp3' -o -name '*.swf' | sed 's/^....//g'); do
322     install -m 644 "web/$i" "${web_path}__/$i"
323 done
324 chmod 755 "${web_path}__/spush/brisk-spush.php"
325
326 prefix_path_len=$(echo -n "$prefix_path" | wc -c)
327
328 if [ $players_n -eq 5 ]; then
329    send_time=250
330 else
331    send_time=10
332 fi
333
334 # .js substitutions
335 sed -i "s/PLAYERS_N *= *[0-9]\+/PLAYERS_N = $players_n/g" $(find ${web_path}__ -type f -name '*.js' -exec grep -l 'PLAYERS_N *= *[0-9]\+' {} \;)
336
337 sed -i "s/^var G_send_time *= *[0-9]\+/var G_send_time = $send_time/g" $(find ${web_path}__ -type f -name '*.js' -exec grep -l '^var G_send_time *= *[0-9]\+' {} \;)
338
339 # .ph[pho] substitutions
340 sed -i "s/define *( *'PLAYERS_N', *[0-9]\+ *)/define('PLAYERS_N', $players_n)/g" $(find ${web_path}__ -type f -name '*.ph*' -exec grep -l "define *( *'PLAYERS_N', *[0-9]\+ *)" {} \;)
341
342 sed -i "s/define *( *'BIN5_PLAYERS_N', *[0-9]\+ *)/define('BIN5_PLAYERS_N', $players_n)/g" $(find ${web_path}__ -type f -name '*.ph*' -exec grep -l "define *( *'BIN5_PLAYERS_N', *[0-9]\+ *)" {} \;)
343
344 sed -i "s@define *( *'FTOK_PATH',[^)]*)@define('FTOK_PATH', \"$ftok_path\")@g" $(find ${web_path}__ -type f -name '*.ph*' -exec grep -l "define *( *'FTOK_PATH',[^)]*)" {} \;)
345
346 sed -i "s@define *( *'SITE_PREFIX',[^)]*)@define('SITE_PREFIX', \"$prefix_path\")@g" ${web_path}__/Obj/sac-a-push.phh
347
348 sed -i "s@define *( *'SITE_PREFIX_LEN',[^)]*)@define('SITE_PREFIX_LEN', $prefix_path_len)@g" ${web_path}__/Obj/sac-a-push.phh
349
350 sed -i "s@define *( *'USOCK_PATH',[^)]*)@define('USOCK_PATH', \"$usock_path\")@g" ${web_path}__/spush/brisk-spush.phh
351
352 sed -i "s@define *( *'TABLES_N',[^)]*)@define('TABLES_N', $tables_n)@g" ${web_path}__/Obj/brisk.phh
353
354 sed -i "s@define *( *'TABLES_AUTH_N',[^)]*)@define('TABLES_AUTH_N', $tables_auth_n)@g" ${web_path}__/Obj/brisk.phh
355
356 sed -i "s@define *( *'BRISK_DEBUG',[^)]*)@define('BRISK_DEBUG', $brisk_debug)@g" ${web_path}__/Obj/brisk.phh
357
358 sed -i "s@define *( *'LEGAL_PATH',[^)]*)@define('LEGAL_PATH', \"$legal_path\")@g" ${web_path}__/Obj/brisk.phh
359
360 sed -i "s@define *( *'PROXY_PATH',[^)]*)@define('PROXY_PATH', \"$proxy_path\")@g" ${web_path}__/Obj/brisk.phh
361
362 sed -i "s@define *( *'BRISK_CONF',[^)]*)@define('BRISK_CONF', \"$brisk_conf\")@g" ${web_path}__/Obj/brisk.phh
363
364 sed -i "s@define *( *'BRISK_AUTH_CONF',[^)]*)@define('BRISK_AUTH_CONF', \"$brisk_auth_conf\")@g" ${web_path}__/Obj/auth.phh
365
366 sed -i "s@var \+cookiepath \+= \+\"[^\"]*\";@var cookiepath = \"$prefix_path\";@g" ${web_path}__/commons.js
367
368 sed -i "s@\( \+cookiepath *: *\)\"[^\"]*\" *,@\1 \"$prefix_path\",@g" ${web_path}__/xynt-streaming.js
369
370 document_root="$(grep DocumentRoot "${apache_conf}"  | grep -v '^[      ]*#' | awk '{ print $2 }')"
371 sed -i "s@^\(\$DOCUMENT_ROOT *= *[\"']\)[^\"']*\([\"']\)@\1$document_root\2@g" ${web_path}__/spush/*.ph*
372
373 if [ -d ${web_path} ]; then
374     mv ${web_path} ${web_path}.old
375 fi
376
377 if [ -d ../brisk-img ]; then
378     cd ../brisk-img
379     ./INSTALL.sh -w ${web_path}__
380     cd - >/dev/null 2>&1
381 fi
382
383 # config file installation or diff
384 if [ -f "$etc_path/$brisk_conf" ]; then
385     echo "Config file $etc_path/$brisk_conf exists."
386     echo "=== Dump the diff. ==="
387     # diff -u "$etc_path/$brisk_conf" "${web_path}__""/Obj/brisk.conf-templ.pho"
388     diff -u <(cat "$etc_path/$brisk_conf" | grep '\$[a-zA-Z_ ]\+=' | sed 's/ = .*/ = /g' | sort | uniq) <(cat "${web_path}__""/Obj/brisk.conf-templ.pho" | grep '\$[a-zA-Z_ ]\+=' | sed 's/ = .*/ = /g' | sort | uniq )
389     echo "===   End dump.    ==="
390 else
391     echo "Config file $etc_path/$brisk_conf not exists."
392     echo "Install a template."
393     cp  "${web_path}__""/Obj/brisk.conf-templ.pho" "$etc_path/$brisk_conf"
394 fi
395
396 mv ${web_path}__ ${web_path}
397 if [ -d ${web_path}.old ]; then
398     rm -rf ${web_path}.old
399 fi
400 if [ "$web_only" = "FALSE" ]; then
401     mv "$ftokk_path" "$ftok_path"
402 fi
403 exit 0