pull conflicts solved
[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 #  Pre-check
188 #
189 # check for etc path existence
190 dsta="$(dirname "$web_path")"
191 etc_path="$(searchetc "$dsta" Etc)"
192 if [ $? -ne 0 ]; then
193     echo "Etc directory not found"
194     exit 1
195 fi
196
197 #
198 #  Installation
199 #
200 ftokk_path="${ftok_path}k"
201
202 if [ $players_n -ne 3 -a $players_n -ne 5 ]; then
203     echo "players_n ($players_n) out of range (3|5)"
204     exit 1
205 fi
206
207 if [ "$web_only" = "FALSE" ]; then
208     if [ ! -d "$ftok_path" -a ! -d "$ftokk_path" ]; then
209         echo "ftok_path (\"$ftok_path\") not exists"
210         exit 2
211     fi
212     if [ -d "$ftok_path" -a -d "$ftokk_path" ]; then
213         echo "ftok_path (\"$ftok_path\") and ftokk_path (\"$ftokk_path\") exist, cannot continue"
214         exit 4
215     fi
216     if [ -d "$ftok_path" ]; then
217         mv "$ftok_path" "$ftokk_path"
218     fi
219     touch $ftokk_path/spy.txt >/dev/null 2>&1
220     if [ $? -ne 0 ]; then
221         echo "ftokk_path (\"$ftokk_path\") write not allowed."
222         exit 3
223     fi
224     rm $ftokk_path/spy.txt
225     
226     # create the fs subtree to enable ftok-ing
227     touch ${ftokk_path}/main
228     chmod 666 ${ftokk_path}/main
229     touch ${ftokk_path}/challenges
230     chmod 666 ${ftokk_path}/challenges
231     touch ${ftokk_path}/hardbans
232     chmod 666 ${ftokk_path}/hardbans
233     touch ${ftokk_path}/warrant
234     chmod 666 ${ftokk_path}/warrant
235     touch ${ftokk_path}/poll
236     chmod 666 ${ftokk_path}/poll
237     for i in $(seq 0 99); do 
238         touch ${ftokk_path}/table$i
239         chmod 666 ${ftokk_path}/table$i
240     done
241     for i in $(seq 0 299); do
242         touch ${ftokk_path}/user$i
243         chmod 666 ${ftokk_path}/user$i
244     done
245 fi
246 install -d ${web_path}__
247 for i in $(find web -type d | grep -v /CVS | sed 's/^....//g'); do
248     install -d ${web_path}__/$i 
249 done
250
251 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
252     install -m 644 web/$i ${web_path}__/$i
253 done
254
255 cd web
256 find . -name '.htaccess' -exec install -m 644 {} ${web_path}__/{} \;
257 cd - >/dev/null 2>&1
258
259 if [ $players_n -eq 5 ]; then
260    send_time=250
261 else
262    send_time=10
263 fi
264
265 # .js substitutions
266 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]\+' {} \;)
267
268 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]\+' {} \;)
269
270 # .ph[pho] substitutions
271 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]\+ *)' {} \;)
272
273 sed -i "s/define *( *BRISKIN5_PLAYERS_N, *[0-9]\+ *)/define(BRISKIN5_PLAYERS_N, $players_n)/g" $(find ${web_path}__ -type f -name '*.ph*' -exec grep -l 'define *( *BRISKIN5_PLAYERS_N, *[0-9]\+ *)' {} \;)
274
275 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,[^)]*)' {} \;)
276
277 sed -i "s@define *( *TABLES_N,[^)]*)@define(TABLES_N, $tables_n)@g" ${web_path}__/Obj/brisk.phh
278
279 sed -i "s@define *( *TABLES_AUTH_N,[^)]*)@define(TABLES_AUTH_N, $tables_auth_n)@g" ${web_path}__/Obj/brisk.phh
280
281 sed -i "s@define *( *BRISK_DEBUG,[^)]*)@define(BRISK_DEBUG, $brisk_debug)@g" ${web_path}__/Obj/brisk.phh
282
283 sed -i "s@define *( *LEGAL_PATH,[^)]*)@define(LEGAL_PATH, \"$legal_path\")@g" ${web_path}__/Obj/brisk.phh
284
285 sed -i "s@define *( *PROXY_PATH,[^)]*)@define(PROXY_PATH, \"$proxy_path\")@g" ${web_path}__/Obj/brisk.phh
286
287 sed -i "s@define *( *BRISK_CONF,[^)]*)@define(BRISK_CONF, \"$brisk_conf\")@g" ${web_path}__/Obj/brisk.phh
288
289 sed -i "s@define *( *BRISK_AUTH_CONF,[^)]*)@define(BRISK_AUTH_CONF, \"$brisk_auth_conf\")@g" ${web_path}__/Obj/auth.phh
290
291 sed -i "s@var \+xhr_rd_cookiepath \+= \+\"[^\"]*\";@var xhr_rd_cookiepath = \"$cookie_path\";@g" ${web_path}__/xhr.js
292 sed -i "s@var \+cookiepath \+= \+\"[^\"]*\";@var cookiepath = \"$cookie_path\";@g" ${web_path}__/commons.js
293
294 if [ -d ${web_path} ]; then
295     mv ${web_path} ${web_path}.old
296 fi
297
298 if [ -d ../brisk-img ]; then
299     cd ../brisk-img
300     ./INSTALL.sh -w ${web_path}__
301     cd - >/dev/null 2>&1
302 fi
303
304 # config file installation or diff
305 if [ -f "$etc_path/$brisk_conf" ]; then
306     echo "Config file $etc_path/$brisk_conf exists."
307     echo "=== Dump the diff. ==="
308     # diff -u "$etc_path/$brisk_conf" "${web_path}__""/Obj/brisk.conf-templ.pho"
309     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 )
310     echo "===   End dump.    ==="
311 else
312     echo "Config file $etc_path/$brisk_conf not exists."
313     echo "Install a template."
314     cp  "${web_path}__""/Obj/brisk.conf-templ.pho" "$etc_path/$brisk_conf"
315 fi
316
317 mv ${web_path}__ ${web_path}
318 if [ -d ${web_path}.old ]; then
319     rm -rf ${web_path}.old
320 fi
321 if [ "$web_only" = "FALSE" ]; then
322     mv "$ftokk_path" "$ftok_path"
323 fi
324 exit 0