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