aggiunto parametro -d per attivare/disattivare debug
[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=0
10
11 if [ -f $HOME/.brisk_install ]; then
12    . $HOME/.brisk_install
13 fi
14
15 function usage () {
16     echo
17     echo "$1 [-d TRUE|FALSE] [-n 3|5] [-w web_dir] [-k <ftok_dir>] [-W]"
18     echo "  -h this help"
19     echo "  -n number of players - def. $n_players"
20     echo "  -d activate dabug    - def. $brisk_debug"
21     echo "  -w dir where place the web tree - def. \"$web_path\""
22     echo "  -k dir where place ftok files   - def. \"$ftok_path\""
23     echo "  -W install web files only"
24     echo
25 }
26
27 function get_param () {
28     echo "X$2" | grep -q "^X$1\$"
29     if [ $? -eq 0 ]; then
30         # echo "DECHE" >&2
31         echo "$3"
32         return 2
33     else
34         # echo "DELA" >&2
35         echo "$2" | cut -c 3-
36         return 1
37     fi
38     return 0
39 }
40
41 #
42 #  MAIN
43 #
44 while [ $# -gt 0 ]; do
45     # echo aa $1 xx $2 bb
46     case $1 in
47         -n*) n_players="`get_param "-n" "$1" "$2"`"; sh=$?;;
48         -d*) brisk_debug="`get_param "-d" "$1" "$2"`"; sh=$?;;
49         -w*) web_path="`get_param "-w" "$1" "$2"`"; sh=$?;;
50         -k*) ftok_path="`get_param "-k" "$1" "$2"`"; sh=$?;;
51         -W) web_only=1;;
52         -h) usage $0; exit 0;;
53         *) usage $0; exit 1;;
54     esac
55     # echo "SH $sh"
56     shift $sh
57 done
58
59 #
60 #  Show parameters
61 #
62 echo "    web_path:  \"$web_path\""
63 echo "    ftok_path: \"$ftok_path\""
64 echo "    n_players:   $n_players"
65
66 #
67 #  Installation
68 #
69 if [ $web_only -eq 0 ]; then
70     if [ $n_players -ne 3 -a $n_players -ne 5 ]; then
71         echo "n_players ($n_players) out of range (3|5)"
72         exit 1
73     elif [ ! -d $ftok_path ]; then
74         echo "ftok_path (\"$ftok_path\") not exists"
75         exit 2
76     fi
77     touch $ftok_path/spy.txt >/dev/null 2>&1
78     if [ $? -ne 0 ]; then
79         echo "ftok_path (\"$ftok_path\") write not allow."
80         exit 3
81     fi
82     rm $ftok_path/spy.txt
83     
84     # create the fs subtree to enable ftok-ing
85     touch ${ftok_path}/main
86     chmod 666 ${ftok_path}/main
87 fi
88 install -d $web_path
89 install -m 644 web/*.{php,phh,css,js} ${web_path}
90 cd web
91 find . -name '.htaccess' -exec install -m 644 {} ${web_path}/{} \;
92 cd -
93
94 if [ $n_players -eq 5 ]; then
95    send_time=250
96 else
97    send_time=10
98 fi
99
100 # .js substitutions
101 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]\+' {} \;`
102
103 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]\+' {} \;`
104
105 # .ph[ph] substitutions
106 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]\+ *)' {} \;`
107
108 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,[^)]*)' {} \;`
109
110 sed -i "s@define *( *BRISK_DEBUG,[^)]*)@define(BRISK_DEBUG, $brisk_debug)@g" ${web_path}/brisk.phh
111
112 # install -d ${web_path}/img
113 # install -m 644 `ls img/*.{jpg,png} | grep -v 'img/src_'` ${web_path}/img
114
115 exit 0