updated version
[brisk.git] / INSTALL.sh
1 #!/bin/bash
2 #
3 # Defaults
4 #
5 n_players=3
6 brisk_debug="TRUE"
7 ftok_path="/var/lib/brisk"
8 web_path="$HOME/brisk"
9 web_only="FALSE"
10
11 if [ -f $HOME/.brisk_install ]; then
12    . $HOME/.spawn_install
13 fi
14 if [ "x$cookie_path" = "x" ]; then
15    cookie_path=$web_path
16 fi
17 function usage () {
18     echo
19     echo "$1 -h"
20     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>]"
21     echo "  -h this help"
22     echo "  -f use this config file"
23     echo "  -p save preferences in the file"
24     echo "  -W web files only"
25     echo "  -n number of players            - def. $n_players"
26     echo "  -d activate dabug               - def. $brisk_debug"
27     echo "  -w dir where place the web tree - def. \"$web_path\""
28     echo "  -k dir where place ftok files   - def. \"$ftok_path\""
29     echo "  -l dir where save logs          - def. \"$legal_path\""
30     echo "  -y dir where place proxy files  - def. \"$proxy_path\""
31     echo "  -c cookie path                  - def. \"$cookie_path\""
32     
33     echo
34 }
35
36 function get_param () {
37     echo "X$2" | grep -q "^X$1\$"
38     if [ $? -eq 0 ]; then
39         # echo "DECHE" >&2
40         echo "$3"
41         return 2
42     else
43         # echo "DELA" >&2
44         echo "$2" | cut -c 3-
45         return 1
46     fi
47     return 0
48 }
49
50 #
51 #  MAIN
52 #
53 while [ $# -gt 0 ]; do
54     # echo aa $1 xx $2 bb
55     conffile=""
56     case $1 in
57         -f*) conffile="`get_param "-f" "$1" "$2"`"; sh=$?;;
58         -p*) outconf="`get_param "-p" "$1" "$2"`"; sh=$?;;
59         -n*) n_players="`get_param "-n" "$1" "$2"`"; sh=$?;;
60         -d*) brisk_debug="`get_param "-d" "$1" "$2"`"; sh=$?;;
61         -w*) web_path="`get_param "-w" "$1" "$2"`"; sh=$?;;
62         -k*) ftok_path="`get_param "-k" "$1" "$2"`"; sh=$?;;
63         -y*) proxy_path="`get_param "-y" "$1" "$2"`"; sh=$?;;
64         -c*) cookie_path="`get_param "-c" "$1" "$2"`"; sh=$?;;
65         -l*) legal_path="`get_param "-l" "$1" "$2"`"; sh=$?;;
66         -W) web_only="TRUE";;
67         -h) usage $0; exit 0;;
68         *) usage $0; exit 1;;
69     esac
70     if [ ! -z "$conffile" ]; then
71         if [ ! -f "$conffile" ]; then
72             echo "config file [$conffile] not found"
73             exit 1
74         fi
75         . "$conffile"
76     fi
77     shift $sh
78 done
79
80 #
81 #  Show parameters
82 #
83 echo "    outconf:    \"$outconf\""
84 echo "    n_players:   $n_players"
85 echo "    brisk_debug:\"$brisk_debug\""
86 echo "    web_path:   \"$web_path\""
87 echo "    ftok_path:  \"$ftok_path\""
88 echo "    legal_path: \"$legal_path\""
89 echo "    proxy_path: \"$proxy_path\""
90 echo "    cookie_path:\"$cookie_path\""
91 echo "    web_only:   \"$web_only\""
92
93 if [ ! -z "$outconf" ]; then
94   ( 
95     echo "#"
96     echo "#  Produced automatically by brisk::INSTALL.sh"
97     echo "#"
98     echo "n_players=$n_players"
99     echo "brisk_debug=\"$brisk_debug\""
100     echo "web_path=\"$web_path\""
101     echo "ftok_path=\"$ftok_path\""
102     echo "proxy_path=\"$proxy_path\""
103     echo "legal_path=\"$legal_path\""
104     echo "cookie_path=\"$cookie_path\""
105     echo "web_only=\"$web_only\""
106   ) > "$outconf"
107 fi
108 #
109 #  Installation
110 #
111 if [ $n_players -ne 3 -a $n_players -ne 5 ]; then
112     echo "n_players ($n_players) out of range (3|5)"
113     exit 1
114 fi
115 if [ "$web_only" = "FALSE" ]; then
116     if [ ! -d $ftok_path ]; then
117         echo "ftok_path (\"$ftok_path\") not exists"
118         exit 2
119     fi
120     touch $ftok_path/spy.txt >/dev/null 2>&1
121     if [ $? -ne 0 ]; then
122         echo "ftok_path (\"$ftok_path\") write not allow."
123         exit 3
124     fi
125     rm $ftok_path/spy.txt
126     
127     # create the fs subtree to enable ftok-ing
128     touch ${ftok_path}/main
129     chmod 666 ${ftok_path}/main
130     for i in `seq 0 99`; do 
131         touch ${ftok_path}/table$i
132         chmod 666 ${ftok_path}/table$i
133     done
134 fi
135 install -d ${web_path}__
136 for i in `find web -type d | grep -v /CVS | sed 's/^....//g'`; do
137     install -d ${web_path}__/$i 
138 done
139
140 for i in `find web -name '*.php' -o -name '*.phh' -o -name '*.css' -o -name '*.js' -o -name '*.mp3' -o -name '*.swf' | grep -v /CVS | sed 's/^....//g'`; do
141     install -m 644 web/$i ${web_path}__/$i
142 done
143
144 cd web
145 find . -name '.htaccess' -exec install -m 644 {} ${web_path}__/{} \;
146 cd -
147
148 if [ $n_players -eq 5 ]; then
149    send_time=250
150 else
151    send_time=10
152 fi
153
154 # .js substitutions
155 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]\+' {} \;`
156
157 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]\+' {} \;`
158
159 # .ph[ph] substitutions
160 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]\+ *)' {} \;`
161
162 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]\+ *)' {} \;`
163
164 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,[^)]*)' {} \;`
165
166 sed -i "s@define *( *BRISK_DEBUG,[^)]*)@define(BRISK_DEBUG, $brisk_debug)@g" ${web_path}__/Obj/brisk.phh
167
168 sed -i "s@define *( *LEGAL_PATH,[^)]*)@define(LEGAL_PATH, \"$legal_path\")@g" ${web_path}__/Obj/brisk.phh
169
170 sed -i "s@define *( *PROXY_PATH,[^)]*)@define(PROXY_PATH, \"$proxy_path\")@g" ${web_path}__/Obj/brisk.phh
171
172 sed -i "s@var \+xhr_rd_cookiepath \+= \+\"[^\"]*\";@var xhr_rd_cookiepath = \"$cookie_path\";@g" ${web_path}__/xhr.js
173 sed -i "s@var \+cookiepath \+= \+\"[^\"]*\";@var cookiepath = \"$cookie_path\";@g" ${web_path}__/commons.js
174
175 if [ -d ${web_path} ]; then
176     mv ${web_path} ${web_path}.old
177 fi
178
179 if [ -d ../brisk-img ]; then
180     cd ../brisk-img
181     ./INSTALL.sh -w ${web_path}__
182     cd -
183 fi
184
185 mv ${web_path}__ ${web_path}
186 if [ -d ${web_path}.old ]; then
187     rm -rf ${web_path}.old
188 fi
189
190 exit 0