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