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