curl-de-sac extensions added
[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 if [ "$1" = "chk" ]; then
94     set -e
95     oldifs="$IFS"
96     IFS='
97 '
98     for i in $(find -name '*.pho' -o -name '*.phh' -o -name '*.php'); do
99         php5 -l $i
100     done
101
102     taggit="$(git describe --tags | sed 's/^v//g')"
103     tagphp="$(grep "^\$G_brisk_version = " web/Obj/brisk.phh | sed 's/^[^"]\+"//g;s/".*//g')"
104     if [ "$taggit" != "$tagphp" ]; then
105         echo
106         echo "WARNING: taggit: [$taggit] tagphp: [$tagphp]"
107         echo
108     fi
109     exit 0
110 fi
111
112 # before all check errors on the sources
113 $0 chk || exit 3
114
115 if [ "$1" = "pkg" ]; then
116     if [ "$2" != "" ]; then
117         tag="$2"
118     else
119         tag="$(git describe)"
120     fi
121     nam1="brisk_${tag}.tgz"
122     nam2="brisk-img_${tag}.tgz"
123     nam2="curl-de-sac_${tag}.tgz"
124     echo "Build packages ${nam1}, ${nam2} and ${nam3}."
125     read -p "Proceed [y/n]: " a
126     if [ "$a" != "y" -a  "$a" != "Y" ]; then
127         exit 1
128     fi
129     git archive --format=tar --prefix=brisk-${tag}/brisk/ $tag | gzip > ../$nam1
130     cd ../brisk-img
131     git archive --format=tar --prefix=brisk-${tag}/brisk-img/ $tag | gzip > ../$nam2
132     cd -
133     cd ../curl-de-sac
134     git archive --format=tar --prefix=brisk-${tag}/curl-de-sac/ $tag | gzip > ../$nam3
135     cd -
136     exit 0
137 fi
138
139 if [ -f "$CONFIG_FILE" ]; then
140    source "$CONFIG_FILE"
141    conffile_in="$CONFIG_FILE"
142 fi
143
144 if [ "x$prefix_path" = "x" ]; then
145    prefix_path="$web_path"
146 fi
147
148 action=""
149 while [ $# -gt 0 ]; do
150     # echo aa $1 xx $2 bb
151     conffile=""
152     case $1 in
153         -A*) apache_conf="$(get_param "-A" "$1" "$2")"; sh=$?;;
154         -f*) conffile="$(get_param "-f" "$1" "$2")"; sh=$?;;
155         -p*) outconf="$(get_param "-p" "$1" "$2")"; sh=$?;;
156         -c*) card_hand="$(get_param "-c" "$1" "$2")"; sh=$?;;
157         -n*) players_n="$(get_param "-n" "$1" "$2")"; sh=$?;;
158         -t*) tables_n="$(get_param "-t" "$1" "$2")"; sh=$?;;
159         -T*) tables_auth_n="$(get_param "-T" "$1" "$2")"; sh=$?;;
160         -G*) tables_cert_n="$(get_param "-G" "$1" "$2")"; sh=$?;;
161         -a*) brisk_auth_conf="$(get_param "-a" "$1" "$2")"; sh=$?;;
162         -d*) brisk_debug="$(get_param "-d" "$1" "$2")"; sh=$?;;
163         -w*) web_path="$(get_param "-w" "$1" "$2")"; sh=$?;;
164         -k*) ftok_path="$(get_param "-k" "$1" "$2")"; sh=$?;;
165         -y*) proxy_path="$(get_param "-y" "$1" "$2")"; sh=$?;;
166         -P*) prefix_path="$(get_param "-P" "$1" "$2")"; sh=$?;;
167         -C*) brisk_conf="$(get_param "-C" "$1" "$2")"; sh=$?;;
168         -l*) legal_path="$(get_param "-l" "$1" "$2")"; sh=$?;;
169         -U*) usock_path="$(get_param "-U" "$1" "$2")"; sh=$?;;
170         -u*) sys_user="$(get_param "-u" "$1" "$2")"; sh=$?;;
171         system) action=system;;
172         -W) web_only="TRUE";;
173         -x) test_add="TRUE";;
174         -h) usage $0; exit 0;;
175         *) usage $0; exit 1;;
176     esac
177     if [ ! -z "$conffile" ]; then
178         if [ ! -f "$conffile" ]; then
179             echo "config file [$conffile] not found"
180             exit 1
181         fi
182         . "$conffile"
183         conffile_in="$conffile"
184     fi
185     shift $sh
186 done
187
188 #
189 #  Show parameters
190 #
191 echo "    outconf:    \"$outconf\""
192 echo "    apache_conf:\"$apache_conf\""
193 echo "    card_hand:   $card_hand"
194 echo "    players_n:   $players_n"
195 echo "    tables_n:    $tables_n"
196 echo "    tables_auth_n: $tables_auth_n"
197 echo "    tables_cert_n: $tables_cert_n"
198 echo "    brisk_auth_conf: \"$brisk_auth_conf\""
199 echo "    brisk_debug:\"$brisk_debug\""
200 echo "    web_path:   \"$web_path\""
201 echo "    ftok_path:  \"$ftok_path\""
202 echo "    legal_path: \"$legal_path\""
203 echo "    proxy_path: \"$proxy_path\""
204 echo "    prefix_path:\"$prefix_path\""
205 echo "    brisk_conf: \"$brisk_conf\""
206 echo "    usock_path: \"$usock_path\""
207 echo "    sys_user:   \"$sys_user\""
208 echo "    web_only:   \"$web_only\""
209 echo "    test_add:   \"$test_add\""
210
211 if [ ! -z "$outconf" ]; then
212   ( 
213     echo "#"
214     echo "#  Produced automatically by brisk::INSTALL.sh"
215     echo "#"
216     echo "apache_conf=$apache_conf"
217     echo "card_hand=$card_hand"
218     echo "players_n=$players_n"
219     echo "tables_n=$tables_n"
220     echo "tables_auth_n=$tables_auth_n"
221     echo "tables_cert_n=$tables_cert_n"
222     echo "brisk_auth_conf=\"$brisk_auth_conf\""
223     echo "brisk_debug=\"$brisk_debug\""
224     echo "web_path=\"$web_path\""
225     echo "ftok_path=\"$ftok_path\""
226     echo "proxy_path=\"$proxy_path\""
227     echo "legal_path=\"$legal_path\""
228     echo "prefix_path=\"$prefix_path\""
229     echo "brisk_conf=\"$brisk_conf\""
230     echo "usock_path=\"$usock_path\""
231     echo "sys_user=\"$sys_user\""
232     echo "web_only=\"$web_only\""
233     echo "test_add=\"$test_add\""
234   ) > "$outconf"
235 fi
236
237 max_players=$((40 + players_n * tables_n))
238
239 if [ "$action" = "system" ]; then
240     scrname="$(echo "$prefix_path" | sed 's@^/@@g;s@/$@@g;s@/@_@g;')"
241     echo
242     echo "script name:  [$scrname]"
243     echo "brisk path:   [$web_path]"
244     echo "private path: [$legal_path]"
245     echo "system user:  [$sys_user]"
246     echo
247     read -p "press enter to continue" sure
248     cp bin/brisk-init.sh brisk-init.sh.wrk
249     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
250
251     su -c "cp brisk-init.sh.wrk /etc/init.d/${scrname}"
252
253     rm brisk-init.sh.wrk
254     echo
255     echo "... DONE."
256     echo "DON'T FORGET: after the first installation you MUST configure your run-levels accordingly"
257     echo
258     echo "Example: su -c 'update-rc.d $scrname defaults'"
259     echo
260     exit 0
261 fi
262 #
263 #  Pre-check
264 #
265 # check for etc path existence
266 dsta="$(dirname "$web_path")"
267 etc_path="$(searchetc "$dsta" Etc)"
268 if [ $? -ne 0 ]; then
269     echo "Etc directory not found"
270     exit 1
271 fi
272
273 IFS='
274 '
275 #
276 #  Installation
277 #
278 ftokk_path="${ftok_path}k"
279
280 if [ $card_hand -lt 3 -o $card_hand -gt 8 ]; then
281     echo "card_hand ($card_hand) out of range (3 <= c <= 8)"
282     exit 1
283 fi
284
285 if [ $players_n -ne 3 -a $players_n -ne 5 ]; then
286     echo "players_n ($players_n) out of range (3|5)"
287     exit 1
288 fi
289
290 if [ "$web_only" = "FALSE" ]; then
291     if [ ! -d "$ftok_path" -a ! -d "$ftokk_path" ]; then
292         echo "ftok_path (\"$ftok_path\") not exists"
293         exit 2
294     fi
295     if [ -d "$ftok_path" -a -d "$ftokk_path" ]; then
296         echo "ftok_path (\"$ftok_path\") and ftokk_path (\"$ftokk_path\") exist, cannot continue"
297         exit 4
298     fi
299     if [ -d "$ftok_path" ]; then
300         mv "$ftok_path" "$ftokk_path"
301     fi
302     touch $ftokk_path/spy.txt >/dev/null 2>&1
303     if [ $? -ne 0 ]; then
304         echo "ftokk_path (\"$ftokk_path\") write not allowed."
305         exit 3
306     fi
307     rm $ftokk_path/spy.txt
308     
309     # create the fs subtree to enable ftok-ing
310     touch ${ftokk_path}/main
311     chmod 666 ${ftokk_path}/main
312     touch ${ftokk_path}/challenges
313     chmod 666 ${ftokk_path}/challenges
314     touch ${ftokk_path}/hardbans
315     chmod 666 ${ftokk_path}/hardbans
316     touch ${ftokk_path}/warrant
317     chmod 666 ${ftokk_path}/warrant
318     touch ${ftokk_path}/poll
319     chmod 666 ${ftokk_path}/poll
320     for i in $(seq 0 $max_players); do
321         touch ${ftokk_path}/user$i
322         chmod 666 ${ftokk_path}/user$i
323     done
324
325     if [ ! -d ${ftokk_path}/bin5 ]; then
326         mkdir ${ftokk_path}/bin5
327         chmod 777 ${ftokk_path}/bin5
328     fi
329
330     for i in $(seq 0 $((tables_n - 1))); do
331         if [ ! -d ${ftokk_path}/bin5/table$i ]; then
332             mkdir ${ftokk_path}/bin5/table$i
333         fi
334         chmod 777 ${ftokk_path}/bin5/table$i
335         touch ${ftokk_path}/bin5/table$i/table
336         chmod 666 ${ftokk_path}/bin5/table$i/table
337         for e in $(seq 0 4); do
338             touch ${ftokk_path}/bin5/table$i/user$e
339             chmod 666 ${ftokk_path}/bin5/table$i/user$e
340         done
341         # create subdirectories in proxy path
342         if [ ! -d ${proxy_path}/bin5/table$i ]; then
343             mkdir -p ${proxy_path}/bin5/table$i
344         fi
345     done
346     chmod -R 777 ${proxy_path}/bin5
347
348 fi
349 install -d ${web_path}__
350 for i in $(find web -type d | grep '/' | sed 's/^....//g'); do
351     install -d ${web_path}__/$i 
352 done
353
354 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
355     install -m 644 "web/$i" "${web_path}__/$i"
356 done
357 if [ "$test_add" = "TRUE" ]; then
358     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
359         install -m 644 "webtest/$i" "${web_path}__/$i"
360     done
361 fi
362
363 chmod 755 "${web_path}__/spush/brisk-spush.php"
364
365 prefix_path_len=$(echo -n "$prefix_path" | wc -c)
366
367 if [ $players_n -eq 5 ]; then
368    send_time=250
369 else
370    send_time=10
371 fi
372
373 # .js substitutions
374 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]\+' {} \;)
375 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]\+' {} \;)
376
377 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]\+' {} \;)
378
379 # .ph[pho] substitutions
380 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]\+ *)" {} \;)
381
382 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]\+ *)" {} \;)
383
384 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]\+ *)" {} \;)
385
386 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',[^)]*)" {} \;)
387
388 sed -i "s@define *( *'SITE_PREFIX',[^)]*)@define('SITE_PREFIX', \"$prefix_path\")@g;
389 s@define *( *'SITE_PREFIX_LEN',[^)]*)@define('SITE_PREFIX_LEN', $prefix_path_len)@g" ${web_path}__/Obj/sac-a-push.phh
390
391 sed -i "s@define *( *'USOCK_PATH',[^)]*)@define('USOCK_PATH', \"$usock_path\")@g" ${web_path}__/spush/brisk-spush.phh
392
393 sed -i "s@define *( *'TABLES_N',[^)]*)@define('TABLES_N', $tables_n)@g;
394 s@define *( *'TABLES_AUTH_N',[^)]*)@define('TABLES_AUTH_N', $tables_auth_n)@g;
395 s@define *( *'TABLES_CERT_N',[^)]*)@define('TABLES_CERT_N', $tables_cert_n)@g;
396 s@define *( *'BRISK_DEBUG',[^)]*)@define('BRISK_DEBUG', $brisk_debug)@g;
397 s@define *( *'LEGAL_PATH',[^)]*)@define('LEGAL_PATH', \"$legal_path\")@g;
398 s@define *( *'PROXY_PATH',[^)]*)@define('PROXY_PATH', \"$proxy_path\")@g;
399 s@define *( *'BRISK_CONF',[^)]*)@define('BRISK_CONF', \"$brisk_conf\")@g;" ${web_path}__/Obj/brisk.phh
400
401 sed -i "s@define *( *'BRISK_AUTH_CONF',[^)]*)@define('BRISK_AUTH_CONF', \"$brisk_auth_conf\")@g" ${web_path}__/Obj/auth.phh
402
403 sed -i "s@var \+cookiepath \+= \+\"[^\"]*\";@var cookiepath = \"$prefix_path\";@g" ${web_path}__/commons.js
404
405 sed -i "s@\( \+cookiepath *: *\)\"[^\"]*\" *,@\1 \"$prefix_path\",@g" ${web_path}__/xynt-streaming.js
406
407 document_root="$(grep DocumentRoot "${apache_conf}"  | grep -v '^[      ]*#' | awk '{ print $2 }')"
408 sed -i "s@^\(\$DOCUMENT_ROOT *= *[\"']\)[^\"']*\([\"']\)@\1$document_root\2@g" ${web_path}__/spush/*.ph*
409
410 if [ -d ${web_path} ]; then
411     mv ${web_path} ${web_path}.old
412 fi
413
414 if [ -d ../brisk-img ]; then
415     cd ../brisk-img
416     ./INSTALL.sh -w ${web_path}__
417     cd - >/dev/null 2>&1
418 fi
419 if [ -d ../curl-de-sac ]; then
420     cd ../curl-de-sac
421     if [ ! -z "$conffile_in" ]; then
422         ./INSTALL.sh -f "$conffile_in" -w ${web_path}__
423     else
424         ./INSTALL.sh -w ${web_path}__
425     fi
426     cd - >/dev/null 2>&1
427 fi
428
429 # config file installation or diff
430 if [ -f "$etc_path/$brisk_conf" ]; then
431     echo "Config file $etc_path/$brisk_conf exists."
432     echo "=== Dump the diff. ==="
433     # diff -u "$etc_path/$brisk_conf" "${web_path}__""/Obj/brisk.conf-templ.pho"
434     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 )
435     echo "===   End dump.    ==="
436 else
437     echo "Config file $etc_path/$brisk_conf not exists."
438     echo "Install a template."
439     cp  "${web_path}__""/Obj/brisk.conf-templ.pho" "$etc_path/$brisk_conf"
440 fi
441
442 mv ${web_path}__ ${web_path}
443 if [ -d ${web_path}.old ]; then
444     rm -rf ${web_path}.old
445 fi
446 if [ "$web_only" = "FALSE" ]; then
447     mv "$ftokk_path" "$ftok_path"
448 fi
449 if [ -f WARNING.txt ]; then
450     echo ; echo "    ==== WARNING ===="
451     echo
452     cat WARNING.txt
453     echo
454 fi
455 exit 0