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