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