1 | #!/bin/bash |
---|
2 | # postinst script |
---|
3 | # |
---|
4 | # see: dh_installdeb(1) |
---|
5 | |
---|
6 | #set -e |
---|
7 | |
---|
8 | . /usr/share/debconf/confmodule |
---|
9 | |
---|
10 | # summary of how this script can be called: |
---|
11 | # * <postinst> `configure' <most-recently-configured-version> |
---|
12 | # * <old-postinst> `abort-upgrade' <new version> |
---|
13 | # * <conflictor's-postinst> `abort-remove' `in-favour' <package> |
---|
14 | # <new-version> |
---|
15 | # * <postinst> `abort-remove' |
---|
16 | # * <deconfigured's-postinst> `abort-deconfigure' `in-favour' |
---|
17 | # <failed-install-package> <version> `removing' |
---|
18 | # <conflicting-package> <version> |
---|
19 | # for details, see http://www.debian.org/doc/debian-policy/ or |
---|
20 | # the debian-policy package |
---|
21 | |
---|
22 | do_dialog(){ |
---|
23 | input=$(dialog \ |
---|
24 | --title "$1" \ |
---|
25 | --inputbox "$2" 0 0 \ |
---|
26 | 3>&1 1>&2 2>&3 3>&-) |
---|
27 | clear |
---|
28 | } |
---|
29 | ask_question(){ |
---|
30 | dialog --yesno "$1" 0 0 |
---|
31 | if [ $? -eq 0 ]; then |
---|
32 | input=yes |
---|
33 | else |
---|
34 | input=no |
---|
35 | fi |
---|
36 | clear |
---|
37 | } |
---|
38 | |
---|
39 | case "$1" in |
---|
40 | configure) |
---|
41 | |
---|
42 | ASK="no" |
---|
43 | |
---|
44 | #CHECK OLD CONFIG |
---|
45 | if [ ! -d "/etc/lliurex-analytics-server" ]; then |
---|
46 | ASK="yes" |
---|
47 | echo "creating config directory" |
---|
48 | mkdir /etc/lliurex-analytics-server |
---|
49 | else |
---|
50 | if [ -f "/etc/lliurex-analytics-server/config_db" ]; then |
---|
51 | USER=$(cat /etc/lliurex-analytics-server/config_db|perl -ne '/user=(.*)/i && print $1') |
---|
52 | PASS=$(cat /etc/lliurex-analytics-server/config_db|perl -ne '/pass=(.*)/i && print $1') |
---|
53 | if [ -n "${USER}" -a -n "${PASS}" ]; then |
---|
54 | RES=$(echo 'show tables;'|mysql -u${USER} -p${PASS} -s analytics 2>/dev/null) |
---|
55 | if [ -n "${RES}" ]; then |
---|
56 | user="${USER}" |
---|
57 | pass="${PASS}" |
---|
58 | ASK="no" |
---|
59 | echo "using user & pass from config_db file" |
---|
60 | else |
---|
61 | echo "wrong user & pass into config_db file" |
---|
62 | ASK="yes" |
---|
63 | fi |
---|
64 | else |
---|
65 | echo "user & pass not detected in config_db file" |
---|
66 | ASK="yes" |
---|
67 | fi |
---|
68 | else |
---|
69 | echo "config_db file not found" |
---|
70 | ASK="yes" |
---|
71 | fi |
---|
72 | fi |
---|
73 | |
---|
74 | # ASK QUESTIONS |
---|
75 | print_info="no" |
---|
76 | if [ "${ASK}" = "yes" ]; then |
---|
77 | ask_question 'Desea configurar la base de datos analytics?' |
---|
78 | if [ "x$input" == "xyes" ]; then |
---|
79 | create_db=$input |
---|
80 | ask_question 'Desea crear usuario para la tabla de la base de datos?' |
---|
81 | create_user=$input |
---|
82 | if [ "x$input" == "xyes" ]; then |
---|
83 | do_dialog 'Database configuration' 'Nombre del usuario para crear:' |
---|
84 | user=$input |
---|
85 | do_dialog 'Database configuration' 'Password del usuario para crear:' |
---|
86 | pass=$input |
---|
87 | else |
---|
88 | do_dialog 'Database configuration' 'Usuario con permiso en bd analytics?' |
---|
89 | user=$input |
---|
90 | do_dialog 'Database configuration' 'Password del usuario con permiso en bd analytics?' |
---|
91 | pass=$input |
---|
92 | fi |
---|
93 | do_dialog 'Database configuration' 'Password root de mysql?' |
---|
94 | rootpass=$input |
---|
95 | else |
---|
96 | print_info="yes" |
---|
97 | fi |
---|
98 | else |
---|
99 | #ASK=no |
---|
100 | create_db="no" |
---|
101 | create_user="no" |
---|
102 | ask_user="no" |
---|
103 | fi |
---|
104 | |
---|
105 | #INITIALIZE DB |
---|
106 | |
---|
107 | if [ "x$create_db" == "xyes" ]; then |
---|
108 | if [ "x$rootpass" == "x" ]; then |
---|
109 | mysqlroot="mysql -uroot" |
---|
110 | mysqluser="mysql -u$user -p$pass" |
---|
111 | else |
---|
112 | mysqlroot="mysql -uroot -p$rootpass" |
---|
113 | mysqluser="mysql -u$user -p$pass" |
---|
114 | fi |
---|
115 | if [ "x$create_user" == "xyes" ]; then |
---|
116 | echo "create user '$user'@'%' identified by '$pass';" | $mysqlroot |
---|
117 | echo "grant all privileges on analytics.* to '$user'@'%';" | $mysqlroot |
---|
118 | echo "flush privileges;" | $mysqlroot |
---|
119 | echo "user=$user" > /etc/lliurex-analytics-server/config_db |
---|
120 | echo "pass=$pass" >> /etc/lliurex-analytics-server/config_db |
---|
121 | chmod 750 /etc/lliurex-analytics-server/config_db |
---|
122 | fi |
---|
123 | $mysqluser < /usr/lib/analytics-server/analytics.sql |
---|
124 | else |
---|
125 | if [ "x$create_user" != "xyes" -o "${ask_user}" != "no" ]; then |
---|
126 | do_dialog 'Analytics configuration' 'Usuario con permiso en bd analytics?' |
---|
127 | user=$input |
---|
128 | do_dialog 'Analytics configuration' 'Password del usuario con permiso en bd analytics?' |
---|
129 | pass=$input |
---|
130 | echo "user=$user" > /etc/lliurex-analytics-server/config_db |
---|
131 | echo "pass=$pass" >> /etc/lliurex-analytics-server/config_db |
---|
132 | chmod 640 /etc/lliurex-analytics-server/config_db |
---|
133 | fi |
---|
134 | fi |
---|
135 | |
---|
136 | RES1=$(grep -o '@phpuser@' /usr/lib/analytics-server/analytics/config.php) |
---|
137 | RES2=$(grep -o '@phppass@' /usr/lib/analytics-server/analytics/config.php) |
---|
138 | |
---|
139 | if [ -f "/usr/lib/analytics-server/analytics/config.php" -a -n "${RES1}" -a -n "${RES2}" ]; then |
---|
140 | sed -i.old -e "s/@phpuser@/$user/g" -e "s/@phppass@/$pass/g" /usr/lib/analytics-server/analytics/config.php |
---|
141 | rm /usr/lib/analytics-server/analytics/config.php.old |
---|
142 | chmod 640 /usr/lib/analytics-server/analytics/config.php |
---|
143 | apachectl_cmd=$(which apachectl) |
---|
144 | apache_group=$(${apachectl_cmd} -t -D DUMP_RUN_CFG 2>/dev/null|perl -ne '/group:[ ]+name=\"(.*)\"/i && print $1') |
---|
145 | if [ -z "${apache_group}" ]; then |
---|
146 | echo "Warning: apache group not detected, falling back to www-data" |
---|
147 | apache_group="www-data" |
---|
148 | fi |
---|
149 | chgrp ${apache_group} /usr/lib/analytics-server/analytics/config.php |
---|
150 | else |
---|
151 | echo "BUG !! WARNING: replace tags @phpuser@ & @phppass@ not found in /usr/lib/analytics-server/analytics/config.php" |
---|
152 | echo "Installation may not work succesfully, report bug to developers" |
---|
153 | fi |
---|
154 | |
---|
155 | |
---|
156 | # CONFIG APACHE |
---|
157 | |
---|
158 | a2enmod rewrite |
---|
159 | a2ensite analytics |
---|
160 | service apache2 restart |
---|
161 | |
---|
162 | if [ "${print_info}" = "yes" ]; then |
---|
163 | echo |
---|
164 | echo "To import database schema use:" |
---|
165 | echo "" |
---|
166 | echo "echo \"create user '<user>'@'%' identified by '<password>';\" | mysql -u root -p<root_password>" |
---|
167 | echo "echo \"grant all privileges on analytics.* to '<user>'@'%';\" | mysql -u root -p<root_password>" |
---|
168 | echo "echo \"flush privileges; \" | mysql -u root -p<root_password>" |
---|
169 | echo "mysql -u <user> -p<root_password> < /usr/lib/analytics-server/analytics.sql" |
---|
170 | echo "After that, edit /usr/lib/analytics-server/analytics/config.php" |
---|
171 | echo |
---|
172 | echo |
---|
173 | fi |
---|
174 | ;; |
---|
175 | |
---|
176 | abort-upgrade|abort-remove|abort-deconfigure) |
---|
177 | ;; |
---|
178 | |
---|
179 | *) |
---|
180 | echo "postinst called with unknown argument \`$1'" >&2 |
---|
181 | exit 1 |
---|
182 | ;; |
---|
183 | esac |
---|
184 | |
---|
185 | # dh_installdeb will replace this with shell code automatically |
---|
186 | # generated by other debhelper scripts. |
---|
187 | |
---|
188 | #DEBHELPER# |
---|
189 | |
---|
190 | exit 0 |
---|
191 | |
---|
192 | |
---|