brisk 2.0 merge
[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/.brisk_install ]; then
15    . $HOME/.brisk_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 "    web_only:   \"$web_only\""
114
115 if [ ! -z "$outconf" ]; then
116   ( 
117     echo "#"
118     echo "#  Produced automatically by brisk::INSTALL.sh"
119     echo "#"
120     echo "n_players=$n_players"
121     echo "brisk_debug=\"$brisk_debug\""
122     echo "web_path=\"$web_path\""
123     echo "ftok_path=\"$ftok_path\""
124     echo "proxy_path=\"$proxy_path\""
125     echo "legal_path=\"$legal_path\""
126     echo "cookie_path=\"$cookie_path\""
127     echo "web_only=\"$web_only\""
128   ) > "$outconf"
129 fi
130 #
131 #  Pre-check
132 #
133 # check for etc path existence
134 dsta="`dirname "$web_path"`"
135 etc_path="`searchetc "$dsta" Etc`"
136 if [ $? -ne 0 ]; then
137     echo "Etc directory not found"
138     exit 1
139 fi
140
141 #
142 #  Installation
143 #
144 ftokk_path="${ftok_path}k"
145
146 if [ $n_players -ne 3 -a $n_players -ne 5 ]; then
147     echo "n_players ($n_players) out of range (3|5)"
148     exit 1
149 fi
150 if [ "$web_only" = "FALSE" ]; then
151     if [ ! -d "$ftok_path" -a ! -d "$ftokk_path" ]; then
152         echo "ftok_path (\"$ftok_path\") not exists"
153         exit 2
154     fi
155     if [ -d "$ftok_path" -a -d "$ftokk_path" ]; then
156         echo "ftok_path (\"$ftok_path\") and ftokk_path (\"$ftokk_path\") exist, cannot continue"
157         exit 4
158     fi
159     if [ -d "$ftok_path" ]; then
160         mv "$ftok_path" "$ftokk_path"
161     fi
162     touch $ftokk_path/spy.txt >/dev/null 2>&1
163     if [ $? -ne 0 ]; then
164         echo "ftokk_path (\"$ftokk_path\") write not allowed."
165         exit 3
166     fi
167     rm $ftokk_path/spy.txt
168     
169     # create the fs subtree to enable ftok-ing
170     touch ${ftokk_path}/main
171     chmod 666 ${ftokk_path}/main
172     for i in `seq 0 99`; do 
173         touch ${ftokk_path}/table$i
174         chmod 666 ${ftokk_path}/table$i
175     done
176 fi
177 install -d ${web_path}__
178 for i in `find web -type d | grep -v /CVS | sed 's/^....//g'`; do
179     install -d ${web_path}__/$i 
180 done
181
182 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
183     install -m 644 web/$i ${web_path}__/$i
184 done
185
186 cd web
187 find . -name '.htaccess' -exec install -m 644 {} ${web_path}__/{} \;
188 cd - >/dev/null 2>&1
189
190 if [ $n_players -eq 5 ]; then
191    send_time=250
192 else
193    send_time=10
194 fi
195
196 # .js substitutions
197 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]\+' {} \;`
198
199 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]\+' {} \;`
200
201 # .ph[pho] substitutions
202 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]\+ *)' {} \;`
203
204 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]\+ *)' {} \;`
205
206 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,[^)]*)' {} \;`
207
208 sed -i "s@define *( *BRISK_DEBUG,[^)]*)@define(BRISK_DEBUG, $brisk_debug)@g" ${web_path}__/Obj/brisk.phh
209
210 sed -i "s@define *( *LEGAL_PATH,[^)]*)@define(LEGAL_PATH, \"$legal_path\")@g" ${web_path}__/Obj/brisk.phh
211
212 sed -i "s@define *( *PROXY_PATH,[^)]*)@define(PROXY_PATH, \"$proxy_path\")@g" ${web_path}__/Obj/brisk.phh
213
214 sed -i "s@define *( *BRISK_CONF,[^)]*)@define(BRISK_CONF, \"$brisk_conf\")@g" ${web_path}__/Obj/brisk.phh
215
216 sed -i "s@var \+xhr_rd_cookiepath \+= \+\"[^\"]*\";@var xhr_rd_cookiepath = \"$cookie_path\";@g" ${web_path}__/xhr.js
217 sed -i "s@var \+cookiepath \+= \+\"[^\"]*\";@var cookiepath = \"$cookie_path\";@g" ${web_path}__/commons.js
218
219 if [ -d ${web_path} ]; then
220     mv ${web_path} ${web_path}.old
221 fi
222
223 if [ -d ../brisk-img ]; then
224     cd ../brisk-img
225     ./INSTALL.sh -w ${web_path}__
226     cd - >/dev/null 2>&1
227 fi
228
229 # config file installation or diff
230 if [ -f "$etc_path/$brisk_conf" ]; then
231     echo "Config file $etc_path/$brisk_conf exists."
232     echo "=== Dump the diff. ==="
233     diff -u "$etc_path/$brisk_conf" "${web_path}__""/Obj/brisk.conf-templ.pho"
234     echo "===   End dump.    ==="
235 else
236     echo "Config file $etc_path/$brisk_conf not exists."
237     echo "Install a template."
238     cp  "${web_path}__""/Obj/brisk.conf-templ.pho" "$etc_path/$brisk_conf"
239 fi
240
241 mv ${web_path}__ ${web_path}
242 if [ -d ${web_path}.old ]; then
243     rm -rf ${web_path}.old
244 fi
245 if [ "$web_only" = "FALSE" ]; then
246     mv "$ftokk_path" "$ftok_path"
247 fi
248 exit 0