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