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