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