set WEB_URL and DBG_LEVEL from INSTALL file
[curl-de-sac.git] / INSTALL.sh
1 #!/bin/bash
2 #set -x
3 #
4 # Defaults
5 #
6 CONFIG_FILE="$HOME/.curl-de-sac_install"
7
8 apache_conf="/etc/apache2/sites-available/default"
9 # brisk_debug="0xffff"
10 web_path="/home/nastasi/web/curl-de-sacccc"
11 web_url="http://localhost/curl-de-sac"
12 dbg_level=998
13 # ftok_path="/home/nastasi/brisk-priv/ftok/brisk"
14 # proxy_path="/home/nastasi/brisk-priv/proxy/brisk"
15 # sys_user="www-data"
16 # legal_path="/home/nastasi/brisk-priv/brisk"
17 # prefix_path="/brisk/"
18 # brisk_conf="brisk_spu.conf.pho"
19 web_only="FALSE"
20 test_add="FALSE"
21 #
22 # functions
23 function usage () {
24     echo
25     echo "$1 -h"
26
27     echo "$1 chk                          - run lintian on all ph* files."
28 #    echo "$1 pkg                          - build brisk packages."
29     echo "$1 [-w <web_dir>] [-f <conffile>] [-p <outconf>]" # [-W] [-n 3|5] [-t <(n>=4)>] [-T <auth_tab>] [-G <cert_tab>] [-A <apache-conf>] [-a <auth_file_name>] [-U <usock_path>] [-u <sys_user>] [-d <TRUE|FALSE>] [-k <ftok_dir>] [-l <legal_path>] [-y <proxy_path>] [-P <prefix_path>] [-x]"
30 #    echo "$1 [-W] [-n 3|5] [-t <(n>=4)>] [-T <auth_tab>] [-G <cert_tab>] [-A <apache-conf>] [-a <auth_file_name>] [-f <conffile>] [-p <outconf>] [-U <usock_path>] [-u <sys_user>] [-d <TRUE|FALSE>] [-w <web_dir>] [-k <ftok_dir>] [-l <legal_path>] [-y <proxy_path>] [-P <prefix_path>] [-x]"
31     echo "$1 [-w <web_dir>]"
32     echo "  -h this help"
33     echo "  -f use this config file"
34     echo "  -p save preferences in the file"
35     # echo "  -W web files only"
36     # echo "  -A apache_conf                  - def. $apache_conf"
37     # echo "  -n number of players            - def. $players_n"
38     # echo "  -t number of tables             - def. $tables_n"
39     # echo "  -T number of auth-only tables   - def. $tables_auth_n"
40     # echo "  -G number of cert-only tables   - def. $tables_cert_n"
41     # echo "  -a authorization file name      - def. \"$brisk_auth_conf\""
42     echo "  -d set debug level                - def. $dbg_level"
43     echo "  -w dir where place the web tree   - def. \"$web_path\""
44     echo "  -U web url to retrieve test pages - def. \"$web_url\""
45     # echo "  -k dir where place ftok files   - def. \"$ftok_path\""
46     # echo "  -l dir where save logs          - def. \"$legal_path\""
47     # echo "  -y dir where place proxy files  - def. \"$proxy_path\""
48     # echo "  -P prefix path                  - def. \"$prefix_path\""
49     # echo "  -C config filename              - def. \"$brisk_conf\""
50     # echo "  -U unix socket path             - def. \"$usock_path\""
51     # echo "  -u system user to run brisk dae - def. \"$sys_user\""
52     echo "  -x copy tests as normal php       - def. \"$test_add\""
53     echo
54 }
55
56 function get_param () {
57     echo "X$2" | grep -q "^X$1\$"
58     if [ $? -eq 0 ]; then
59         # echo "DECHE" >&2
60         echo "$3"
61         return 2
62     else
63         # echo "DELA" >&2
64         echo "$2" | cut -c 3-
65         return 1
66     fi
67     return 0
68 }
69
70 function searchetc() {
71     local dstart dname pp
72     dstart="$1"
73     dname="$2"
74
75     pp="$dstart"
76     while [ "$pp" != "/" ]; do
77         if [ -d "$pp/$dname" ]; then
78             echo "$pp/$dname"
79             return 0
80         fi
81         pp="$(dirname "$pp")"
82     done
83     
84     return 1
85 }
86
87 #
88 #  MAIN
89 #
90 if [ "$1" = "chk" ]; then
91     set -e
92     oldifs="$IFS"
93     IFS='
94 '
95     for i in $(find -name '*.pho' -o -name '*.phh' -o -name '*.php'); do
96         php5 -l $i
97     done
98
99     taggit="$(git describe --tags | sed 's/^v//g')"
100     tagphp="$(grep "^\$G_curl_de_sac_version = " web/Obj/curl-de-sac.phh | sed 's/^[^"]\+"//g;s/".*//g')"
101     if [ "$taggit" != "$tagphp" ]; then
102         echo
103         echo "WARNING: taggit: [$taggit] tagphp: [$tagphp]"
104         echo
105     fi
106     exit 0
107 fi
108
109 # before all check errors on the sources
110 $0 chk || exit 3
111
112 if [ "$1" = "pkg" ]; then
113     if [ "$2" != "" ]; then
114         tag="$2"
115     else
116         tag="$(git describe)"
117     fi
118     nam1="curl-de-sac_${tag}.tgz"
119     echo "Build packages ${nam1}."
120     read -p "Proceed [y/n]: " a
121     if [ "$a" != "y" -a  "$a" != "Y" ]; then
122         exit 1
123     fi
124     git archive --format=tar --prefix=brisk-${tag}/curl-de-sac/ $tag | gzip > ../$nam1
125     cd -
126     exit 0
127 fi
128
129 if [ -f "$CONFIG_FILE" ]; then
130    source "$CONFIG_FILE"
131 fi
132
133 if [ "x$prefix_path" = "x" ]; then
134    prefix_path="$web_path"
135 fi
136
137 action=""
138 while [ $# -gt 0 ]; do
139     # echo aa $1 xx $2 bb
140     conffile=""
141     case $1 in
142 #        -A*) apache_conf="$(get_param "-A" "$1" "$2")"; sh=$?;;
143         -f*) conffile="$(get_param "-f" "$1" "$2")"; sh=$?;;
144         -p*) outconf="$(get_param "-p" "$1" "$2")"; sh=$?;;
145 #        -n*) players_n="$(get_param "-n" "$1" "$2")"; sh=$?;;
146 #        -t*) tables_n="$(get_param "-t" "$1" "$2")"; sh=$?;;
147 #        -T*) tables_auth_n="$(get_param "-T" "$1" "$2")"; sh=$?;;
148 #        -G*) tables_cert_n="$(get_param "-G" "$1" "$2")"; sh=$?;;
149 #        -a*) brisk_auth_conf="$(get_param "-a" "$1" "$2")"; sh=$?;;
150         -d*) dbg_level="$(get_param "-d" "$1" "$2")"; sh=$?;;
151         -w*) web_path="$(get_param "-w" "$1" "$2")"; sh=$?;;
152         -U*) web_url="$(get_param "-U" "$1" "$2")" ; sh=$?;;
153 #        -k*) ftok_path="$(get_param "-k" "$1" "$2")"; sh=$?;;
154 #        -y*) proxy_path="$(get_param "-y" "$1" "$2")"; sh=$?;;
155 #        -P*) prefix_path="$(get_param "-P" "$1" "$2")"; sh=$?;;
156 #        -C*) brisk_conf="$(get_param "-C" "$1" "$2")"; sh=$?;;
157 #        -l*) legal_path="$(get_param "-l" "$1" "$2")"; sh=$?;;
158 #        -U*) usock_path="$(get_param "-U" "$1" "$2")"; sh=$?;;
159 #        -u*) sys_user="$(get_param "-u" "$1" "$2")"; sh=$?;;
160 #        system) action=system;;
161 #        -W) web_only="TRUE";;
162         -x) test_add="TRUE";;
163         -h) usage $0; exit 0;;
164         *) usage $0; exit 1;;
165     esac
166     if [ ! -z "$conffile" ]; then
167         if [ ! -f "$conffile" ]; then
168             echo "config file [$conffile] not found"
169             exit 1
170         fi
171         . "$conffile"
172     fi
173     shift $sh
174 done
175
176 #
177 #  Show parameters
178 #
179 echo "    outconf:    \"$outconf\""
180 # echo "    apache_conf:\"$apache_conf\""
181 # echo "    players_n:   $players_n"
182 # echo "    tables_n:    $tables_n"
183 # echo "    tables_auth_n: $tables_auth_n"
184 # echo "    tables_cert_n: $tables_cert_n"
185 # echo "    brisk_auth_conf: \"$brisk_auth_conf\""
186 echo "    dbg_level:  $dbg_level"
187 echo "    web_path:   \"$web_path\""
188 echo "    web_url:    \"$web_url\""
189 # echo "    ftok_path:  \"$ftok_path\""
190 # echo "    legal_path: \"$legal_path\""
191 # echo "    proxy_path: \"$proxy_path\""
192 # echo "    prefix_path:\"$prefix_path\""
193 # echo "    brisk_conf: \"$brisk_conf\""
194 # echo "    usock_path: \"$usock_path\""
195 # echo "    sys_user:   \"$sys_user\""
196 # echo "    web_only:   \"$web_only\""
197 # echo "    test_add:   \"$test_add\""
198
199 if [ ! -z "$outconf" ]; then
200   ( 
201     echo "#"
202     echo "#  Produced automatically by curl-de-sac::INSTALL.sh"
203     echo "#"
204     # echo "apache_conf=$apache_conf"
205     # echo "players_n=$players_n"
206     # echo "tables_n=$tables_n"
207     # echo "tables_auth_n=$tables_auth_n"
208     # echo "tables_cert_n=$tables_cert_n"
209     # echo "brisk_auth_conf=\"$brisk_auth_conf\""
210     echo "dbg_level=$dbg_level"
211     echo "web_path=\"$web_path\""
212     echo "web_url=\"$web_url\""
213     # echo "ftok_path=\"$ftok_path\""
214     # echo "proxy_path=\"$proxy_path\""
215     # echo "legal_path=\"$legal_path\""
216     # echo "prefix_path=\"$prefix_path\""
217     # echo "brisk_conf=\"$brisk_conf\""
218     # echo "usock_path=\"$usock_path\""
219     # echo "sys_user=\"$sys_user\""
220     # echo "web_only=\"$web_only\""
221     # echo "test_add=\"$test_add\""
222   ) > "$outconf"
223 fi
224
225 if [ 1 -eq 0 -a "$action" = "system" ]; then
226     # no used
227     scrname="$(echo "$prefix_path" | sed 's@^/@@g;s@/$@@g;s@/@_@g;')"
228     echo
229     echo "script name:  [$scrname]"
230     echo "brisk path:   [$web_path]"
231     echo "private path: [$legal_path]"
232     echo "system user:  [$sys_user]"
233     echo
234     read -p "press enter to continue" sure
235     cp bin/brisk-init.sh brisk-init.sh.wrk
236     sed -i "s@^BPATH=.*@BPATH=\"${web_path}\"@g;s@^PPATH=.*@PPATH=\"${legal_path}\"@g;s@^SSUFF=.*@SSUFF=\"${scrname}\"@g;s@^BUSER=.*@BUSER=\"${sys_user}\"@g" brisk-init.sh.wrk
237
238     su -c "cp brisk-init.sh.wrk /etc/init.d/${scrname}"
239
240     rm brisk-init.sh.wrk
241     echo
242     echo "... DONE."
243     echo "DON'T FORGET: after the first installation you MUST configure your run-levels accordingly"
244     echo
245     echo "Example: su -c 'update-rc.d $scrname defaults'"
246     echo
247     exit 0
248 fi
249
250 #  Pre-check
251 #
252 # check for etc path existence
253 dsta="$(dirname "$web_path")"
254 etc_path="$(searchetc "$dsta" Etc)"
255 if [ $? -ne 0 ]; then
256     echo "Etc directory not found"
257     exit 1
258 fi
259
260 IFS='
261 '
262 #
263 #  Installation
264 #
265 # ftokk_path="${ftok_path}k"
266
267
268 if [ "$web_only" != "TRUE" ]; then
269    # here code una tantum
270    :
271 fi
272 install -d ${web_path}
273 for i in $(find web -type d | grep '/' | sed 's/^....//g'); do
274     install -d ${web_path}/$i 
275 done
276
277 for i in $(find web -name '.htaccess' -o -name '*.php' -o -name '*.phh' -o -name '*.pho' -o -name '*.css' -o -name '*.js' -o -name '*.mp3' -o -name '*.swf' -o -name 'terms-of-service*' | sed 's/^....//g'); do
278     install -m 644 "web/$i" "${web_path}/$i"
279 done
280 if [ "$test_add" = "TRUE" ]; then
281     for i in $(find webtest -name '.htaccess' -o -name '*.phh' -o -name '*.pho' -o -name '*.css' -o -name '*.js' -o -name '*.mp3' -o -name '*.swf' -o -name 'terms-of-service*' | sed 's/^........//g'); do
282         install -m 644 "webtest/$i" "${web_path}/$i"
283     done
284     for i in $(find webtest -name '*.php' | sed 's/^........//g'); do
285         install -m 755 "webtest/$i" "${web_path}/$i"
286     done
287 fi
288
289 # # .js substitutions
290 # 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]\+' {} \;)
291
292 # 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]\+' {} \;)
293
294 # # .ph[pho] substitutions
295 sed -i "s@^define *( *'WEB_URL', *'[^']\+' *)@define('WEB_URL', '$web_url')@g;s@define *( *'DBG_LEVEL', *[0-9]\+ *)@define('DBG_LEVEL', $dbg_level)@g" $(find ${web_path} -type f -name '*.ph*')
296
297 # sed -i "s/define *( *'BIN5_PLAYERS_N', *[0-9]\+ *)/define('BIN5_PLAYERS_N', $players_n)/g" $(find ${web_path} -type f -name '*.ph*' -exec grep -l "define *( *'BIN5_PLAYERS_N', *[0-9]\+ *)" {} \;)
298
299 # 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',[^)]*)" {} \;)
300
301 # sed -i "s@define *( *'SITE_PREFIX',[^)]*)@define('SITE_PREFIX', \"$prefix_path\")@g;
302 # s@define *( *'SITE_PREFIX_LEN',[^)]*)@define('SITE_PREFIX_LEN', $prefix_path_len)@g" ${web_path}/Obj/sac-a-push.phh
303
304 # sed -i "s@define *( *'USOCK_PATH',[^)]*)@define('USOCK_PATH', \"$usock_path\")@g" ${web_path}/spush/brisk-spush.phh
305
306 # sed -i "s@define *( *'TABLES_N',[^)]*)@define('TABLES_N', $tables_n)@g;
307 # s@define *( *'TABLES_AUTH_N',[^)]*)@define('TABLES_AUTH_N', $tables_auth_n)@g;
308 # s@define *( *'TABLES_CERT_N',[^)]*)@define('TABLES_CERT_N', $tables_cert_n)@g;
309 # s@define *( *'BRISK_DEBUG',[^)]*)@define('BRISK_DEBUG', $brisk_debug)@g;
310 # s@define *( *'LEGAL_PATH',[^)]*)@define('LEGAL_PATH', \"$legal_path\")@g;
311 # s@define *( *'PROXY_PATH',[^)]*)@define('PROXY_PATH', \"$proxy_path\")@g;
312 # s@define *( *'BRISK_CONF',[^)]*)@define('BRISK_CONF', \"$brisk_conf\")@g;" ${web_path}/Obj/brisk.phh
313
314 # sed -i "s@define *( *'BRISK_AUTH_CONF',[^)]*)@define('BRISK_AUTH_CONF', \"$brisk_auth_conf\")@g" ${web_path}/Obj/auth.phh
315
316 # sed -i "s@var \+cookiepath \+= \+\"[^\"]*\";@var cookiepath = \"$prefix_path\";@g" ${web_path}/commons.js
317
318 # sed -i "s@\( \+cookiepath *: *\)\"[^\"]*\" *,@\1 \"$prefix_path\",@g" ${web_path}/xynt-streaming.js
319
320 # document_root="$(grep DocumentRoot "${apache_conf}"  | grep -v '^[    ]*#' | awk '{ print $2 }')"
321 # sed -i "s@^\(\$DOCUMENT_ROOT *= *[\"']\)[^\"']*\([\"']\)@\1$document_root\2@g" ${web_path}/spush/*.ph*
322
323 # # config file installation or diff
324 # if [ -f "$etc_path/$brisk_conf" ]; then
325 #     echo "Config file $etc_path/$brisk_conf exists."
326 #     echo "=== Dump the diff. ==="
327 #     # diff -u "$etc_path/$brisk_conf" "${web_path}""/Obj/brisk.conf-templ.pho"
328 #     diff -u <(cat "$etc_path/$brisk_conf" | egrep -v '^//|^#' | grep '\$[a-zA-Z_ ]\+=' | sed 's/ \+= .*/ = /g' | sort | uniq) <(cat "${web_path}""/Obj/brisk.conf-templ.pho" | egrep -v '^//|^#' | grep '\$[a-zA-Z_ ]\+=' | sed 's/ \+= .*/ = /g' | sort | uniq )
329 #     echo "===   End dump.    ==="
330 # else
331 #     echo "Config file $etc_path/$brisk_conf not exists."
332 #     echo "Install a template."
333 #     cp  "${web_path}""/Obj/brisk.conf-templ.pho" "$etc_path/$brisk_conf"
334 # fi
335
336 if [ -f WARNING.txt ]; then
337     echo ; echo "    ==== WARNING ===="
338     echo
339     cat WARNING.txt
340     echo
341 fi
342 exit 0