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