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