source: zero-lliurex-dni/trunk/fuentes/zero-lliurex-dni.zlaunchers/zero-lliurex-dni.zmd @ 507

Last change on this file since 507 was 507, checked in by jrpelegrina, 4 years ago

First release to xenial

File size: 9.1 KB
Line 
1#!/bin/bash
2
3#set -x
4
5## Global Variables
6###################
7
8export TEXTDOMAIN="zero-lliurex-dni"
9
10USER_NAME=$USER # User name
11USER_HOME="" # Home user folder
12FF_PROFILE_PATH="" # Absolute path to user firefox profile
13TH_PROFILE_PATH="" # Absolute path to user firefox profile
14LIBRARY="/usr/lib/libpkcs11-dnie.so"
15OLD_LIBRARY="/usr/lib/opensc-pkcs11.so /usr/lib/libpkcs11-dnie.so"
16ERROR=0
17
18
19
20# MSGS
21MSG_LIBRARY_NOT_FOUND="$(gettext 'Library not found:')"
22MSG_SOME_PROBLEM_FIREFOX="$(gettext 'Some problem with firefox profile')"
23MSG_ONCE_FIREFOX="$(gettext 'Start firefox once at least')"
24MSG_SOME_PROBLEM_THUNDER="$(gettext 'Some problem with Thunderbird profile')"
25MSG_ONCE_THUNDER="$(gettext 'Start Thunderbird once at least')"
26MSG_RUNNING_FIREFOX="$(gettext 'Firefox is running. Stop it!')"
27MSG_RUNNING_THUNDER="$(gettext 'Thunderbird is running. Stop it!')"
28MSG_NOT_FIREFOX="$(gettext 'Firefox is not installed')"
29MSG_NOT_THUNDER="$(gettext 'Thunderbird is not installed')"
30MSG_INSTALLING_PROGRESS="$(gettext 'Installing DNIe in your computer...')"
31MSG_ZENITY_FIREFOX_1="$(gettext 'Installing DNIe modules in your system')"
32MSG_ZENITY_FIREFOX_2="$(gettext 'DNIe modules in Firefox')"
33MSG_ZENITY_THUNDER_1="$(gettext 'DNIe modules in Thunderbird')"
34MSG_FINAL_ERROR="$(gettext 'Impossible to install DNIe module, correct the problems and execute this program again please.')"
35MSG_FINAL_OK="$(gettext 'DNIe modules are installed with success in Firefox and Thunderbird profile, for others users you have to execute this action again on each session.')"
36MSG_CREATING_THUNDER="$(gettext 'We have to create your Thunderbird profile, now it will be launched, then please close Thunderbird and installation continues')"
37
38#instalo paquetes necesarios de los repositorios
39
40zero-installer install libpkcs11-dnie
41# Si falla la instalacion del paquete se aborat el resto y da error
42if [ $? != 0 ] ; then
43        zenity --error --text="Impossible to install libpkcs11-dnie problems with repos. $MSG_FINAL_ERROR" 
44        exit 1
45fi
46
47
48#inicializo variable de error en un archivo de tmp ya que se produce una subshell en el zenity
49
50echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
51
52user_home(){
53   
54   USER_HOME=$(getent passwd "$USER_NAME" | cut -d: -f 6)
55}
56
57test_libpkcs11_dnie(){
58
59   if [ ! -f $LIBRARY ]; then
60      zenity --info --text="$MSG_LIBRARY_NOT_FOUND $LIBRARY"
61      exit 1
62      ERROR=1
63      echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
64   fi
65}
66
67test_running_firefox(){
68
69   is_running=$(ps -fe | grep /usr/lib/firefox/firefox | grep -v grep | wc -l)
70   if [ $is_running -gt 0 ]; then
71        return 1
72   else
73        return 0
74   fi
75}
76
77test_running_thunderbird(){
78
79   is_running=$(ps -fe | grep thunderbird | grep -v grep | wc -l)
80        if [ $is_running -gt 0 ]; then
81                while [ $is_running -gt 0 ] ; do
82                        zenity --info --text="$MSG_RUNNING_THUNDER"
83                        is_running=$(ps -fe | grep thunderbird | grep -v grep | wc -l)
84                done
85        fi
86        return 0
87       
88}
89
90test_installed_package(){
91
92   dpkg -s $1
93   iRESULT=$?
94   if [ $iRESULT -eq 1 ]; then
95        return 0
96   else
97        return 1
98   fi
99}
100
101## Firefox functions
102####################
103
104get_firefox_default_profile(){
105   
106   # Getting Mozilla products default profile
107   # http://kb.mozillazine.org/Profiles.ini_file
108   #
109   # Get default profile info
110   # This script must be capable of dealing with multiple profiles
111   # and relative or absolute paths, for now ... buff!!
112   #
113   # This script get Default path profile if Default=1 or
114   # get first profile in other case
115 
116   TMP_FILE=$(mktemp)
117   cat $FF_PATH/profiles.ini | awk -F ' *= *' '{ if ($1 ~ /^\[/) section=$1; else if ($1 !~ /^$/) print $1 section "=" "\"" $2 "\"" }' > $TMP_FILE
118   DEFAULT_EXISTS=$(grep ^Default $TMP_FILE)
119   if [ -n $DEFAULT_EXISTS ]; then
120        PROFILE_ID=$(cat $TMP_FILE | grep -m 1 Profile[0-9] | cut -d"[" -f 2 | cut -d"]" -f 1 )
121   else
122        PROFILE_ID=$(echo $DEFAULT_EXISTS | grep -m 1 Profile[0-9] | cut -d"[" -f 2 | cut -d"]" -f 1 )
123   fi
124   IS_RELATIVE=$(cat $TMP_FILE | grep $PROFILE_ID | grep ^IsRelative | cut -d"=" -f 2 | sed -e 's/\"//g')
125   TMP_PATH=$(cat $TMP_FILE | grep $PROFILE_ID | grep ^Path | cut -d"=" -f 2 | sed -e 's/\"//g')
126   if [ $IS_RELATIVE = "0" ]; then
127      FF_PROFILE_PATH=$TMP_PATH
128   else
129      FF_PROFILE_PATH=$FF_PATH/$TMP_PATH
130   fi
131   rm -f $TMP_FILE
132}
133
134get_thunderbird_default_profile(){
135   
136   # Getting Mozilla products default profile
137   # http://kb.mozillazine.org/Profiles.ini_file
138   #
139   # Get default profile info
140   # This script must be capable of dealing with multiple profiles
141   # and relative or absolute paths, for now ... buff!!
142   #
143   # This script get Default path profile if Default=1 or
144   # get first profile in other case
145 
146   TMP_FILE=$(mktemp)
147   cat $TH_PATH/profiles.ini | awk -F ' *= *' '{ if ($1 ~ /^\[/) section=$1; else if ($1 !~ /^$/) print $1 section "=" "\"" $2 "\"" }' > $TMP_FILE
148   DEFAULT_EXISTS=$(grep ^Default $TMP_FILE)
149   if [ -n $DEFAULT_EXISTS ]; then
150        PROFILE_ID=$(cat $TMP_FILE | grep -m 1 Profile[0-9] | cut -d"[" -f 2 | cut -d"]" -f 1 )
151   else
152        PROFILE_ID=$(echo $DEFAULT_EXISTS | grep -m 1 Profile[0-9] | cut -d"[" -f 2 | cut -d"]" -f 1 )
153   fi
154   IS_RELATIVE=$(cat $TMP_FILE | grep $PROFILE_ID | grep ^IsRelative | cut -d"=" -f 2 | sed -e 's/\"//g')
155   TMP_PATH=$(cat $TMP_FILE | grep $PROFILE_ID | grep ^Path | cut -d"=" -f 2 | sed -e 's/\"//g')
156   if [ $IS_RELATIVE = "0" ]; then
157      TH_PROFILE_PATH=$TMP_PATH
158   else
159      TH_PROFILE_PATH=$TH_PATH/$TMP_PATH
160   fi
161   rm -f $TMP_FILE
162}
163
164test_firefox_user_profile() {
165
166   if [ -d $FF_PATH ]; then
167        get_firefox_default_profile
168        if [ ! -d $FF_PROFILE_PATH ]; then
169                zenity --info --text="$MSG_SOME_PROBLEM_FIREFOX"
170                ERROR=1
171                echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
172                return 0
173               
174        fi
175        return 1
176   else
177        zenity --info --text="$MSG_ONCE_FIREFOX"
178        ERROR=1
179        echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
180        exit 3
181   fi
182       
183}
184
185test_thunderbird_user_profile() {
186
187   if [ -d $TH_PATH ]; then
188        get_thunderbird_default_profile
189        if [ ! -d $TH_PROFILE_PATH ]; then
190                zenity --info --text="$MSG_SOME_PROBLEM_THUNDER" 
191                ERROR=1
192                echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
193                return 0
194        fi
195        return 1
196   else
197        zenity --info --text="$MSG_CREATING_THUNDER"
198        su $USER -c "thunderbird"
199        test_running_thunderbird
200        get_thunderbird_default_profile
201   fi
202       
203}
204
205test_firefox_dni_module(){
206
207   if [ -z $FF_MODULE_EXISTS ]; then
208        return 1
209   fi
210   return 0
211}
212
213test_thunderbird_dni_module(){
214
215   if [ -z $TH_MODULE_EXISTS ]; then
216        return 1
217   fi
218   return 0
219}
220
221remove_dni_module_firefox(){
222
223   su $USER -c "modutil -disable $FF_MODULE_NAME -force -dbdir $FF_PROFILE_PATH 2>&1"
224   su $USER -c "modutil -delete $FF_MODULE_NAME -force -dbdir $FF_PROFILE_PATH 2>&1"
225}
226
227remove_dni_module_thunderbird(){
228
229   su $USER -c "modutil -disable $TH_MODULE_NAME -force -dbdir $TH_PROFILE_PATH 2>&1"
230   su $USER -c "modutil -delete $TH_MODULE_NAME -force -dbdir $TH_PROFILE_PATH 2>&1"
231}
232
233enable_dni_module_firefox(){
234
235   su $USER -c "modutil -add $FF_MODULE_NAME -force -libfile $LIBRARY -dbdir $FF_PROFILE_PATH 2>&1"
236   su $USER -c "modutil -enable $FF_MODULE_NAME -force -dbdir $FF_PROFILE_PATH 2>&1"
237}
238
239enable_dni_module_thunderbird(){
240
241   su $USER -c "modutil -add $TH_MODULE_NAME -force -libfile $LIBRARY -dbdir $TH_PROFILE_PATH 2>&1"
242   su $USER -c "modutil -enable $TH_MODULE_NAME -force -dbdir $TH_PROFILE_PATH 2>&1"
243}
244
245do_firefox(){
246
247   test_installed_package "firefox"
248   if [ $? ]; then
249        test_running_firefox
250        if [ $? -gt 0 ]; then
251                ERROR=1
252                echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
253                zenity --info --text="$MSG_RUNNING_FIREFOX"
254                exit 2
255        else
256                test_firefox_user_profile
257                test_ff_up=$?
258                echo "--->"$test_ff_up"<---"
259                if [ $? ]; then
260                        remove_dni_module_firefox
261                        enable_dni_module_firefox
262                fi
263        fi
264   else
265      ERROR=1
266      echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
267      zenity --info --text="$MSG_NOT_FIREFOX"
268      exit 1
269   fi
270}
271
272do_thunderbird(){
273
274   test_installed_package "thunderbird"
275   if [ $? ]; then
276        test_running_thunderbird # and waits to close
277        test_thunderbird_user_profile # and create if doesnt exist
278        test_th_up=$?
279        echo "--->"$test_th_up"<---"
280        if [ $? ]; then
281                remove_dni_module_thunderbird
282                enable_dni_module_thunderbird
283        fi
284   else
285      zenity --info --text="$MSG_NOT_THUNDER"
286      ERROR=1
287      echo "$ERROR" > /tmp/zero-lliurex-dni.tmp
288      exit 1
289   fi
290}
291
292## Get user home folder
293#######################
294
295
296## Test if dni library exists
297#############################
298
299
300# firefox
301(
302test_libpkcs11_dnie
303user_home
304echo "# $MSG_ZENITY_FIREFOX_1" ; sleep 1
305echo "20"
306echo "# $MSG_ZENITY_FIREFOX_2" ; FF_MODULE_NAME="DNI_E"
307echo "40"
308echo "# $MSG_ZENITY_FIREFOX_2"; FF_PATH="$USER_HOME/.mozilla/firefox"
309do_firefox
310echo "60"
311echo "# $MSG_ZENITY_THUNDER_1";TH_MODULE_NAME="DNI_E"
312echo "80"
313echo "# $MSG_ZENITY_THUNDER_1";TH_PATH="$USER_HOME/.thunderbird"
314do_thunderbird
315echo "100"
316) |
317zenity --progress \
318  --title="ZERO-LLIUREX-DNIe" \
319  --text="$MSG_INSTALLING_PROGRESS" \
320  --percentage=0 \
321  --auto-close \
322   --width=500 \
323  --auto-kill
324 
325#capturo el error a ver si todo fue bien o fallo algo
326ERROR=$(cat /tmp/zero-lliurex-dni.tmp)
327
328if [ $ERROR != 0 ] ; then 
329        zenity --error --text="$MSG_FINAL_ERROR" 
330        exit 1
331  else
332        zenity --info --text="$MSG_FINAL_OK"
333fi
334exit 0
Note: See TracBrowser for help on using the repository browser.