Poll class 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     touch ${ftokk_path}/warrant
179     chmod 666 ${ftokk_path}/warrant
180     touch ${ftokk_path}/poll
181     chmod 666 ${ftokk_path}/poll
182     for i in `seq 0 99`; do 
183         touch ${ftokk_path}/table$i
184         chmod 666 ${ftokk_path}/table$i
185     done
186 fi
187 install -d ${web_path}__
188 for i in `find web -type d | grep -v /CVS | sed 's/^....//g'`; do
189     install -d ${web_path}__/$i 
190 done
191
192 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
193     install -m 644 web/$i ${web_path}__/$i
194 done
195
196 cd web
197 find . -name '.htaccess' -exec install -m 644 {} ${web_path}__/{} \;
198 cd - >/dev/null 2>&1
199
200 if [ $n_players -eq 5 ]; then
201    send_time=250
202 else
203    send_time=10
204 fi
205
206 # .js substitutions
207 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]\+' {} \;`
208
209 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]\+' {} \;`
210
211 # .ph[pho] substitutions
212 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]\+ *)' {} \;`
213
214 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]\+ *)' {} \;`
215
216 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,[^)]*)' {} \;`
217
218 sed -i "s@define *( *BRISK_DEBUG,[^)]*)@define(BRISK_DEBUG, $brisk_debug)@g" ${web_path}__/Obj/brisk.phh
219
220 sed -i "s@define *( *LEGAL_PATH,[^)]*)@define(LEGAL_PATH, \"$legal_path\")@g" ${web_path}__/Obj/brisk.phh
221
222 sed -i "s@define *( *PROXY_PATH,[^)]*)@define(PROXY_PATH, \"$proxy_path\")@g" ${web_path}__/Obj/brisk.phh
223
224 sed -i "s@define *( *BRISK_CONF,[^)]*)@define(BRISK_CONF, \"$brisk_conf\")@g" ${web_path}__/Obj/brisk.phh
225
226 sed -i "s@var \+xhr_rd_cookiepath \+= \+\"[^\"]*\";@var xhr_rd_cookiepath = \"$cookie_path\";@g" ${web_path}__/xhr.js
227 sed -i "s@var \+cookiepath \+= \+\"[^\"]*\";@var cookiepath = \"$cookie_path\";@g" ${web_path}__/commons.js
228
229 if [ -d ${web_path} ]; then
230     mv ${web_path} ${web_path}.old
231 fi
232
233 if [ -d ../brisk-img ]; then
234     cd ../brisk-img
235     ./INSTALL.sh -w ${web_path}__
236     cd - >/dev/null 2>&1
237 fi
238
239 # config file installation or diff
240 if [ -f "$etc_path/$brisk_conf" ]; then
241     echo "Config file $etc_path/$brisk_conf exists."
242     echo "=== Dump the diff. ==="
243     diff -u "$etc_path/$brisk_conf" "${web_path}__""/Obj/brisk.conf-templ.pho"
244     echo "===   End dump.    ==="
245 else
246     echo "Config file $etc_path/$brisk_conf not exists."
247     echo "Install a template."
248     cp  "${web_path}__""/Obj/brisk.conf-templ.pho" "$etc_path/$brisk_conf"
249 fi
250
251 mv ${web_path}__ ${web_path}
252 if [ -d ${web_path}.old ]; then
253     rm -rf ${web_path}.old
254 fi
255 if [ "$web_only" = "FALSE" ]; then
256     mv "$ftokk_path" "$ftok_path"
257 fi
258 exit 0