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