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