version updated
[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="brosk_auth.conf.pho"
12 brisk_debug="0x0400"
13 # brisk_debug="0xffff"
14 web_path="/home/nastasi/web/brosk"
15 ftok_path="/home/nastasi/brisk-priv/ftok/brosk"
16 proxy_path="/home/nastasi/brisk-priv/proxy/brosk"
17 legal_path="/home/nastasi/brisk-priv/brosk"
18 cookie_path="/brosk/"
19 brisk_conf="brosk.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 #
205 #  Installation
206 #
207 ftokk_path="${ftok_path}k"
208
209 if [ $players_n -ne 3 -a $players_n -ne 5 ]; then
210     echo "players_n ($players_n) out of range (3|5)"
211     exit 1
212 fi
213
214 if [ "$web_only" = "FALSE" ]; then
215     if [ ! -d "$ftok_path" -a ! -d "$ftokk_path" ]; then
216         echo "ftok_path (\"$ftok_path\") not exists"
217         exit 2
218     fi
219     if [ -d "$ftok_path" -a -d "$ftokk_path" ]; then
220         echo "ftok_path (\"$ftok_path\") and ftokk_path (\"$ftokk_path\") exist, cannot continue"
221         exit 4
222     fi
223     if [ -d "$ftok_path" ]; then
224         mv "$ftok_path" "$ftokk_path"
225     fi
226     touch $ftokk_path/spy.txt >/dev/null 2>&1
227     if [ $? -ne 0 ]; then
228         echo "ftokk_path (\"$ftokk_path\") write not allowed."
229         exit 3
230     fi
231     rm $ftokk_path/spy.txt
232     
233     # create the fs subtree to enable ftok-ing
234     touch ${ftokk_path}/main
235     chmod 666 ${ftokk_path}/main
236     touch ${ftokk_path}/challenges
237     chmod 666 ${ftokk_path}/challenges
238     touch ${ftokk_path}/hardbans
239     chmod 666 ${ftokk_path}/hardbans
240     touch ${ftokk_path}/warrant
241     chmod 666 ${ftokk_path}/warrant
242     touch ${ftokk_path}/poll
243     chmod 666 ${ftokk_path}/poll
244     for i in $(seq 0 $max_players); do
245         touch ${ftokk_path}/user$i
246         chmod 666 ${ftokk_path}/user$i
247     done
248
249     if [ ! -d ${ftokk_path}/bin5 ]; then
250         mkdir ${ftokk_path}/bin5
251         chmod 777 ${ftokk_path}/bin5
252     fi
253
254     for i in $(seq 0 $((tables_n - 1))); do
255         if [ ! -d ${ftokk_path}/bin5/table$i ]; then
256             mkdir ${ftokk_path}/bin5/table$i
257         fi
258         chmod 777 ${ftokk_path}/bin5/table$i
259         touch ${ftokk_path}/bin5/table$i/table
260         chmod 666 ${ftokk_path}/bin5/table$i/table
261         for e in $(seq 0 4); do
262             touch ${ftokk_path}/bin5/table$i/user$e
263             chmod 666 ${ftokk_path}/bin5/table$i/user$e
264         done
265         # create subdirectories in proxy path
266         if [ ! -d ${proxy_path}/bin5/table$i ]; then
267             mkdir -p ${proxy_path}/bin5/table$i
268         fi
269     done
270     chmod -R 777 ${proxy_path}/bin5
271
272 fi
273 install -d ${web_path}__
274 for i in $(find web -type d | grep -v /CVS | sed 's/^....//g'); do
275     install -d ${web_path}__/$i 
276 done
277
278 for i in $(find web -name '*.php' -o -name '*.phh' -o -name '*.pho' -o -name '*.css' -o -name '*.js' -o -name '*.mp3' -o -name '*.swf' | grep -v /CVS | sed 's/^....//g'); do
279     install -m 644 web/$i ${web_path}__/$i
280 done
281
282 cd web
283 find . -name '.htaccess' -exec install -m 644 {} ${web_path}__/{} \;
284 cd - >/dev/null 2>&1
285
286 if [ $players_n -eq 5 ]; then
287    send_time=250
288 else
289    send_time=10
290 fi
291
292 # .js substitutions
293 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]\+' {} \;)
294
295 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]\+' {} \;)
296
297 # .ph[pho] substitutions
298 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]\+ *)' {} \;)
299
300 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]\+ *)' {} \;)
301
302 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,[^)]*)' {} \;)
303
304 sed -i "s@define *( *TABLES_N,[^)]*)@define(TABLES_N, $tables_n)@g" ${web_path}__/Obj/brisk.phh
305
306 sed -i "s@define *( *TABLES_AUTH_N,[^)]*)@define(TABLES_AUTH_N, $tables_auth_n)@g" ${web_path}__/Obj/brisk.phh
307
308 sed -i "s@define *( *BRISK_DEBUG,[^)]*)@define(BRISK_DEBUG, $brisk_debug)@g" ${web_path}__/Obj/brisk.phh
309
310 sed -i "s@define *( *LEGAL_PATH,[^)]*)@define(LEGAL_PATH, \"$legal_path\")@g" ${web_path}__/Obj/brisk.phh
311
312 sed -i "s@define *( *PROXY_PATH,[^)]*)@define(PROXY_PATH, \"$proxy_path\")@g" ${web_path}__/Obj/brisk.phh
313
314 sed -i "s@define *( *BRISK_CONF,[^)]*)@define(BRISK_CONF, \"$brisk_conf\")@g" ${web_path}__/Obj/brisk.phh
315
316 sed -i "s@define *( *BRISK_AUTH_CONF,[^)]*)@define(BRISK_AUTH_CONF, \"$brisk_auth_conf\")@g" ${web_path}__/Obj/auth.phh
317
318 sed -i "s@var \+xhr_rd_cookiepath \+= \+\"[^\"]*\";@var xhr_rd_cookiepath = \"$cookie_path\";@g" ${web_path}__/xhr.js
319 sed -i "s@var \+cookiepath \+= \+\"[^\"]*\";@var cookiepath = \"$cookie_path\";@g" ${web_path}__/commons.js
320
321 if [ -d ${web_path} ]; then
322     mv ${web_path} ${web_path}.old
323 fi
324
325 if [ -d ../brisk-img ]; then
326     cd ../brisk-img
327     ./INSTALL.sh -w ${web_path}__
328     cd - >/dev/null 2>&1
329 fi
330
331 # config file installation or diff
332 if [ -f "$etc_path/$brisk_conf" ]; then
333     echo "Config file $etc_path/$brisk_conf exists."
334     echo "=== Dump the diff. ==="
335     # diff -u "$etc_path/$brisk_conf" "${web_path}__""/Obj/brisk.conf-templ.pho"
336     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 )
337     echo "===   End dump.    ==="
338 else
339     echo "Config file $etc_path/$brisk_conf not exists."
340     echo "Install a template."
341     cp  "${web_path}__""/Obj/brisk.conf-templ.pho" "$etc_path/$brisk_conf"
342 fi
343
344 mv ${web_path}__ ${web_path}
345 if [ -d ${web_path}.old ]; then
346     rm -rf ${web_path}.old
347 fi
348 if [ "$web_only" = "FALSE" ]; then
349     mv "$ftokk_path" "$ftok_path"
350 fi
351 exit 0