aggiunta gestione flash e audio mp3
[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/.brisk_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>] [-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 "  -c cookie path                  - def. \"$cookie_path\""
30     
31     echo
32 }
33
34 function get_param () {
35     echo "X$2" | grep -q "^X$1\$"
36     if [ $? -eq 0 ]; then
37         # echo "DECHE" >&2
38         echo "$3"
39         return 2
40     else
41         # echo "DELA" >&2
42         echo "$2" | cut -c 3-
43         return 1
44     fi
45     return 0
46 }
47
48 #
49 #  MAIN
50 #
51 while [ $# -gt 0 ]; do
52     # echo aa $1 xx $2 bb
53     conffile=""
54     case $1 in
55         -f*) conffile="`get_param "-f" "$1" "$2"`"; sh=$?;;
56         -p*) outconf="`get_param "-p" "$1" "$2"`"; sh=$?;;
57         -n*) n_players="`get_param "-n" "$1" "$2"`"; sh=$?;;
58         -d*) brisk_debug="`get_param "-d" "$1" "$2"`"; sh=$?;;
59         -w*) web_path="`get_param "-w" "$1" "$2"`"; sh=$?;;
60         -k*) ftok_path="`get_param "-k" "$1" "$2"`"; sh=$?;;
61         -c*) cookie_path="`get_param "-c" "$1" "$2"`"; sh=$?;;
62         -W) web_only="TRUE";;
63         -h) usage $0; exit 0;;
64         *) usage $0; exit 1;;
65     esac
66     if [ ! -z "$conffile" ]; then
67         if [ ! -f "$conffile" ]; then
68             echo "config file [$conffile] not found"
69             exit 1
70         fi
71         . "$conffile"
72     fi
73     shift $sh
74 done
75
76 #
77 #  Show parameters
78 #
79 echo "    outconf:    \"$outconf\""
80 echo "    n_players:   $n_players"
81 echo "    brisk_debug:\"$brisk_debug\""
82 echo "    web_path:   \"$web_path\""
83 echo "    ftok_path:  \"$ftok_path\""
84 echo "    cookie_path:\"$cookie_path\""
85 echo "    web_only:   \"$web_only\""
86
87 if [ ! -z "$outconf" ]; then
88   ( 
89     echo "#"
90     echo "#  Produced automatically by brisk::INSTALL.sh"
91     echo "#"
92     echo "n_players=$n_players"
93     echo "brisk_debug=\"$brisk_debug\""
94     echo "web_path=\"$web_path\""
95     echo "ftok_path=\"$ftok_path\""
96     echo "cookie_path=\"$cookie_path\""
97     echo "web_only=\"$web_only\""
98   ) > "$outconf"
99 fi
100 #
101 #  Installation
102 #
103 if [ $n_players -ne 3 -a $n_players -ne 5 ]; then
104     echo "n_players ($n_players) out of range (3|5)"
105     exit 1
106 fi
107 if [ "$web_only" = "FALSE" ]; then
108     if [ ! -d $ftok_path ]; then
109         echo "ftok_path (\"$ftok_path\") not exists"
110         exit 2
111     fi
112     touch $ftok_path/spy.txt >/dev/null 2>&1
113     if [ $? -ne 0 ]; then
114         echo "ftok_path (\"$ftok_path\") write not allow."
115         exit 3
116     fi
117     rm $ftok_path/spy.txt
118     
119     # create the fs subtree to enable ftok-ing
120     touch ${ftok_path}/main
121     chmod 666 ${ftok_path}/main
122 fi
123 install -d $web_path
124 install -m 644 web/*.{php,phh,css,js,mp3,swf} ${web_path}
125 cd web
126 find . -name '.htaccess' -exec install -m 644 {} ${web_path}/{} \;
127 cd -
128
129 if [ $n_players -eq 5 ]; then
130    send_time=250
131 else
132    send_time=10
133 fi
134
135 # .js substitutions
136 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]\+' {} \;`
137
138 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]\+' {} \;`
139
140 # .ph[ph] substitutions
141 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]\+ *)' {} \;`
142
143 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,[^)]*)' {} \;`
144
145 sed -i "s@define *( *BRISK_DEBUG,[^)]*)@define(BRISK_DEBUG, $brisk_debug)@g" ${web_path}/brisk.phh
146
147 sed -i "s@var \+xhr_rd_cookiepath \+= \+\"[^\"]*\";@var xhr_rd_cookiepath = \"$cookie_path\";@g" ${web_path}/xhr.js
148
149 exit 0