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