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