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