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