sac-a-push daemon added to the current brisk tree
[brisk.git] / INSTALL.sh
1 #!/bin/bash
2 # set -x
3 #
4 # Defaults
5 #
6 CONFIG_FILE="$HOME/.brisk_install"
7
8 apache_path="/etc/apache2/sites-available/"
9 apache_conf="default"
10
11 players_n=3
12 tables_n=44
13 tables_auth_n=12
14 brisk_auth_conf="brisk_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 legal_path="/home/nastasi/brisk-priv/brisk"
21 cookie_path="/brisk/"
22 brisk_conf="brisk.conf.pho"
23 web_only="FALSE"
24
25
26 if [ "$1" = "chk" ]; then
27     set -e
28     oldifs="$IFS"
29     IFS='
30 '
31     for i in $(find -name '*.pho' -o -name '*.phh' -o -name '*.php'); do
32         php5 -l $i
33     done
34     exit 0
35 fi
36
37 # before all check errors on the sources
38 $0 chk || exit 3
39
40 if [ "$1" = "pkg" ]; then
41     if [ "$2" != "" ]; then
42         tag="$2"
43     else
44         tag="$(git describe)"
45     fi
46     nam1="brisk_${tag}.tgz"
47     nam2="brisk-img_${tag}.tgz"
48     echo "Build packages ${nam1} and ${nam2}."
49     read -p "Proceed [y/n]: " a
50     if [ "$a" != "y" -a  "$a" != "Y" ]; then
51         exit 1
52     fi
53     git archive --format=tar --prefix=brisk-${tag}/brisk/ $tag | gzip > ../$nam1
54     cd ../brisk-img
55     git archive --format=tar --prefix=brisk-${tag}/brisk-img/ $tag | gzip > ../$nam2
56     cd -
57     exit 0
58 fi
59     
60 if [ -f "$CONFIG_FILE" ]; then
61    source "$CONFIG_FILE"
62 fi
63
64 if [ "x$cookie_path" = "x" ]; then
65    cookie_path=$web_path
66 fi
67
68 function usage () {
69     echo
70     echo "$1 -h"
71     echo "$1 chk                          - run lintian on all ph* files."
72     echo "$1 pkg                          - build brisk packages."
73     echo "$1 [-W] [-n 3|5] [-t <(n>=4)>] [-T <auth_tab>] [-a <auth_file_name>] [-f conffile] [-p outconf] [-d TRUE|FALSE] [-w web_dir] [-k <ftok_dir>] [-l <legal_path>] [-y <proxy_path>] [-c <cookie_path>]"
74     echo "  -h this help"
75     echo "  -f use this config file"
76     echo "  -p save preferences in the file"
77     echo "  -W web files only"
78     echo "  -n number of players            - def. $players_n"
79     echo "  -t number of tables             - def. $tables_n"
80     echo "  -T number of auth-only tables   - def. $tables_auth_n"
81     echo "  -a authorization file name      - def. \"$brisk_auth_conf\""
82     echo "  -d activate dabug               - def. $brisk_debug"
83     echo "  -w dir where place the web tree - def. \"$web_path\""
84     echo "  -k dir where place ftok files   - def. \"$ftok_path\""
85     echo "  -l dir where save logs          - def. \"$legal_path\""
86     echo "  -y dir where place proxy files  - def. \"$proxy_path\""
87     echo "  -c cookie path                  - def. \"$cookie_path\""
88     echo "  -C config filename              - def. \"$brisk_conf\""
89     
90     echo
91 }
92
93 function get_param () {
94     echo "X$2" | grep -q "^X$1\$"
95     if [ $? -eq 0 ]; then
96         # echo "DECHE" >&2
97         echo "$3"
98         return 2
99     else
100         # echo "DELA" >&2
101         echo "$2" | cut -c 3-
102         return 1
103     fi
104     return 0
105 }
106
107 function searchetc() {
108     local dstart dname pp
109     dstart="$1"
110     dname="$2"
111
112     pp="$dstart"
113     while [ "$pp" != "/" ]; do
114         if [ -d "$pp/$dname" ]; then
115             echo "$pp/$dname"
116             return 0
117         fi
118         pp="$(dirname "$pp")"
119     done
120     
121     return 1
122 }
123
124 #
125 #  MAIN
126 #
127 while [ $# -gt 0 ]; do
128     # echo aa $1 xx $2 bb
129     conffile=""
130     case $1 in
131         -f*) conffile="$(get_param "-f" "$1" "$2")"; sh=$?;;
132         -p*) outconf="$(get_param "-p" "$1" "$2")"; sh=$?;;
133         -n*) players_n="$(get_param "-n" "$1" "$2")"; sh=$?;;
134         -t*) tables_n="$(get_param "-t" "$1" "$2")"; sh=$?;;
135         -T*) tables_auth_n="$(get_param "-T" "$1" "$2")"; sh=$?;;
136         -a*) brisk_auth_conf="$(get_param "-a" "$1" "$2")"; sh=$?;;
137         -d*) brisk_debug="$(get_param "-d" "$1" "$2")"; sh=$?;;
138         -w*) web_path="$(get_param "-w" "$1" "$2")"; sh=$?;;
139         -k*) ftok_path="$(get_param "-k" "$1" "$2")"; sh=$?;;
140         -y*) proxy_path="$(get_param "-y" "$1" "$2")"; sh=$?;;
141         -c*) cookie_path="$(get_param "-c" "$1" "$2")"; sh=$?;;
142         -C*) brisk_conf="$(get_param "-C" "$1" "$2")"; sh=$?;;
143         -l*) legal_path="$(get_param "-l" "$1" "$2")"; sh=$?;;
144         -W) web_only="TRUE";;
145         -h) usage $0; exit 0;;
146         *) usage $0; exit 1;;
147     esac
148     if [ ! -z "$conffile" ]; then
149         if [ ! -f "$conffile" ]; then
150             echo "config file [$conffile] not found"
151             exit 1
152         fi
153         . "$conffile"
154     fi
155     shift $sh
156 done
157
158 #
159 #  Show parameters
160 #
161 echo "    outconf:    \"$outconf\""
162 echo "    players_n:   $players_n"
163 echo "    tables_n:   $tables_n"
164 echo "    tables_auth_n: $tables_auth_n"
165 echo "    brisk_auth_conf: \"$brisk_auth_conf\""
166 echo "    brisk_debug:\"$brisk_debug\""
167 echo "    web_path:   \"$web_path\""
168 echo "    ftok_path:  \"$ftok_path\""
169 echo "    legal_path: \"$legal_path\""
170 echo "    proxy_path: \"$proxy_path\""
171 echo "    cookie_path:\"$cookie_path\""
172 echo "    brisk_conf:\"$brisk_conf\""
173 echo "    web_only:   \"$web_only\""
174
175 if [ ! -z "$outconf" ]; then
176   ( 
177     echo "#"
178     echo "#  Produced automatically by brisk::INSTALL.sh"
179     echo "#"
180     echo "players_n=$players_n"
181     echo "tables_n=$tables_n"
182     echo "tables_auth_n=$tables_auth_n"
183     echo "brisk_auth_conf=\"$brisk_auth_conf\""
184     echo "brisk_debug=\"$brisk_debug\""
185     echo "web_path=\"$web_path\""
186     echo "ftok_path=\"$ftok_path\""
187     echo "proxy_path=\"$proxy_path\""
188     echo "legal_path=\"$legal_path\""
189     echo "cookie_path=\"$cookie_path\""
190     echo "brisk_conf=\"$brisk_conf\""
191     echo "web_only=\"$web_only\""
192   ) > "$outconf"
193 fi
194
195 max_players=$((40 + players_n * tables_n))
196 #
197 #  Pre-check
198 #
199 # check for etc path existence
200 dsta="$(dirname "$web_path")"
201 etc_path="$(searchetc "$dsta" Etc)"
202 if [ $? -ne 0 ]; then
203     echo "Etc directory not found"
204     exit 1
205 fi
206
207 IFS='
208 '
209 #
210 #  Installation
211 #
212 ftokk_path="${ftok_path}k"
213
214 if [ $players_n -ne 3 -a $players_n -ne 5 ]; then
215     echo "players_n ($players_n) out of range (3|5)"
216     exit 1
217 fi
218
219 if [ "$web_only" = "FALSE" ]; then
220     if [ ! -d "$ftok_path" -a ! -d "$ftokk_path" ]; then
221         echo "ftok_path (\"$ftok_path\") not exists"
222         exit 2
223     fi
224     if [ -d "$ftok_path" -a -d "$ftokk_path" ]; then
225         echo "ftok_path (\"$ftok_path\") and ftokk_path (\"$ftokk_path\") exist, cannot continue"
226         exit 4
227     fi
228     if [ -d "$ftok_path" ]; then
229         mv "$ftok_path" "$ftokk_path"
230     fi
231     touch $ftokk_path/spy.txt >/dev/null 2>&1
232     if [ $? -ne 0 ]; then
233         echo "ftokk_path (\"$ftokk_path\") write not allowed."
234         exit 3
235     fi
236     rm $ftokk_path/spy.txt
237     
238     # create the fs subtree to enable ftok-ing
239     touch ${ftokk_path}/main
240     chmod 666 ${ftokk_path}/main
241     touch ${ftokk_path}/challenges
242     chmod 666 ${ftokk_path}/challenges
243     touch ${ftokk_path}/hardbans
244     chmod 666 ${ftokk_path}/hardbans
245     touch ${ftokk_path}/warrant
246     chmod 666 ${ftokk_path}/warrant
247     touch ${ftokk_path}/poll
248     chmod 666 ${ftokk_path}/poll
249     for i in $(seq 0 $max_players); do
250         touch ${ftokk_path}/user$i
251         chmod 666 ${ftokk_path}/user$i
252     done
253
254     if [ ! -d ${ftokk_path}/bin5 ]; then
255         mkdir ${ftokk_path}/bin5
256         chmod 777 ${ftokk_path}/bin5
257     fi
258
259     for i in $(seq 0 $((tables_n - 1))); do
260         if [ ! -d ${ftokk_path}/bin5/table$i ]; then
261             mkdir ${ftokk_path}/bin5/table$i
262         fi
263         chmod 777 ${ftokk_path}/bin5/table$i
264         touch ${ftokk_path}/bin5/table$i/table
265         chmod 666 ${ftokk_path}/bin5/table$i/table
266         for e in $(seq 0 4); do
267             touch ${ftokk_path}/bin5/table$i/user$e
268             chmod 666 ${ftokk_path}/bin5/table$i/user$e
269         done
270         # create subdirectories in proxy path
271         if [ ! -d ${proxy_path}/bin5/table$i ]; then
272             mkdir -p ${proxy_path}/bin5/table$i
273         fi
274     done
275     chmod -R 777 ${proxy_path}/bin5
276
277 fi
278 install -d ${web_path}__
279 for i in $(find web -type d | sed 's/^....//g'); do
280     install -d ${web_path}__/$i 
281 done
282
283 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
284     install -m 644 "web/$i" "${web_path}__/$i"
285 done
286 chmod 755 "${web_path}__/spush/brisk-spush.php"
287
288 if [ $players_n -eq 5 ]; then
289    send_time=250
290 else
291    send_time=10
292 fi
293
294 # .js substitutions
295 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]\+' {} \;)
296
297 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]\+' {} \;)
298
299 # .ph[pho] substitutions
300 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]\+ *)' {} \;)
301
302 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]\+ *)' {} \;)
303
304 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',[^)]*)' {} \;)
305
306 sed -i "s@define *( *'TABLES_N',[^)]*)@define('TABLES_N', $tables_n)@g" ${web_path}__/Obj/brisk.phh
307
308 sed -i "s@define *( *'TABLES_AUTH_N',[^)]*)@define('TABLES_AUTH_N', $tables_auth_n)@g" ${web_path}__/Obj/brisk.phh
309
310 sed -i "s@define *( *'BRISK_DEBUG',[^)]*)@define('BRISK_DEBUG', $brisk_debug)@g" ${web_path}__/Obj/brisk.phh
311
312 sed -i "s@define *( *'LEGAL_PATH',[^)]*)@define('LEGAL_PATH', \"$legal_path\")@g" ${web_path}__/Obj/brisk.phh
313
314 sed -i "s@define *( *'PROXY_PATH',[^)]*)@define('PROXY_PATH', \"$proxy_path\")@g" ${web_path}__/Obj/brisk.phh
315
316 sed -i "s@define *( *'BRISK_CONF',[^)]*)@define('BRISK_CONF', \"$brisk_conf\")@g" ${web_path}__/Obj/brisk.phh
317
318 sed -i "s@define *( *'BRISK_AUTH_CONF',[^)]*)@define('BRISK_AUTH_CONF', \"$brisk_auth_conf\")@g" ${web_path}__/Obj/auth.phh
319
320 sed -i "s@\( \+xhr_rd_cookiepath *: *\)\"[^\"]*\" *,@\1 \"$cookie_path\",@g" ${web_path}__/http_streaming.js
321 sed -i "s@var \+cookiepath \+= \+\"[^\"]*\";@var cookiepath = \"$cookie_path\";@g" ${web_path}__/commons.js
322
323 document_root="$(grep DocumentRoot "${apache_path}${apache_conf}" | awk '{ print $2 }')"
324 sed -i "s@^\(\$DOCUMENT_ROOT *= *[\"']\)[^\"']*\([\"']\)@\1$document_root\2@g" ${web_path}__/spush/*.ph*
325
326 if [ -d ${web_path} ]; then
327     mv ${web_path} ${web_path}.old
328 fi
329
330 if [ -d ../brisk-img ]; then
331     cd ../brisk-img
332     ./INSTALL.sh -w ${web_path}__
333     cd - >/dev/null 2>&1
334 fi
335
336 # config file installation or diff
337 if [ -f "$etc_path/$brisk_conf" ]; then
338     echo "Config file $etc_path/$brisk_conf exists."
339     echo "=== Dump the diff. ==="
340     # diff -u "$etc_path/$brisk_conf" "${web_path}__""/Obj/brisk.conf-templ.pho"
341     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 )
342     echo "===   End dump.    ==="
343 else
344     echo "Config file $etc_path/$brisk_conf not exists."
345     echo "Install a template."
346     cp  "${web_path}__""/Obj/brisk.conf-templ.pho" "$etc_path/$brisk_conf"
347 fi
348
349 mv ${web_path}__ ${web_path}
350 if [ -d ${web_path}.old ]; then
351     rm -rf ${web_path}.old
352 fi
353 if [ "$web_only" = "FALSE" ]; then
354     mv "$ftokk_path" "$ftok_path"
355 fi
356 exit 0