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