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