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