File: thunderbird-wrapper-helper.sh

package info (click to toggle)
thunderbird 1%3A52.9.1-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,709,148 kB
  • sloc: cpp: 5,081,792; ansic: 2,051,951; python: 458,733; java: 241,615; xml: 193,600; asm: 178,649; sh: 81,881; makefile: 24,702; perl: 16,874; objc: 4,389; yacc: 1,816; ada: 1,697; lex: 1,257; pascal: 1,251; cs: 879; exp: 499; php: 436; lisp: 258; awk: 152; sed: 51; ruby: 47; csh: 27
file content (429 lines) | stat: -rw-r--r-- 14,545 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=sh textwidth=76
#
# File:
#   /usr/lib/thunderbird/thunderbird-wrapper-helper.sh
#
# Purpose:
#   This shell script has helper functions and variables that are used or
#   called from the main wrapper-script /usr/bin/thunderbird.
#
# Copyright:
#   Licensed under the terms of GPLv2+.


# trying to get the DE
if [ "${XDG_CURRENT_DESKTOP}" = "" ]; then
    DESKTOP=$(echo "${XDG_DATA_DIRS}" | sed 's/.*\(gnome\|kde\|mate\|xfce\).*/\1/')
else
    DESKTOP=${XDG_CURRENT_DESKTOP}
fi

# timestamp like '2017-02-26-113855'
DATE=$(date +%F-%H%M%S)

# convert to lower case shell safe
DESKTOP=$(echo "$DESKTOP" | tr '[:upper:]' '[:lower:]')

#########################################
# message templates for the X11 dialogs #
#########################################

DEFAULT_X11_MSG="\
If you see this message something went wrong while
migrating your Icedove profile(s) into the Thunderbird
profile folder.

The following error occured:"

DOT_THUNDERBIRD_EXISTS="\
${DEFAULT_X11_MSG}

An existing profile folder (or symlink) '.thunderbird' and a folder
(or symlink) '.icedove' was found in your home directory '${HOME}/'
while trying to migrate the Icedove profile(s) folder.

This can be caused by an old, currently unused profile folder or you might
be using Thunderbird as provided by upstream Mozilla.
If you don't need this old profile folder, you can remove or backup
it and start Thunderbird again.

Sorry, but please investigate the situation yourself.

The Debian wiki has extra information about the migration from
Icedove to Thunderbird.

  https://wiki.debian.org/Thunderbird

Please also read the information in section 'Profile Migration' in

  /usr/share/doc/thunderbird/README.Debian.gz
"

THUNDERBIRD_PROFILE_LINKING_ERROR="\
${DEFAULT_X11_MSG}

A needed symlink for the Thunderbird profile(s) folder '.thunderbird'
to the old existing Icedove profile '.icedove' couldn't created.

Sorry, but please investigate the situation by yourself.

Please mind also the information in section 'Profile Migration'
given in the file

  /usr/share/doc/thunderbird/README.Debian.gz
"

START_MIGRATION="\
You see this window because you're starting Thunderbird for the
first time and have profile(s) for Icedove.
The Debian Icedove package is de-branded back to Thunderbird.

The Icedove profile(s) will now be migrated to the Thunderbird folder.
This will take a short time!

Please be patient, the Thunderbird program will be started right after
the changes.

If you need more information on the de-branding and migration please
read

  /usr/share/doc/thunderbird/README.Debian.gz

The Debian wiki is also holding extra information about the migration of
Icedove to Thunderbird.

  https://wiki.debian.org/Thunderbird
"

TITLE="Icedove to Thunderbird Profile migration"

###################
# local functions #
###################

# Simple search all files where we made a backup from
do_collect_backup_files () {
output_debug "Collecting all files we've made a backup of."
BACKUP_FILES=$(find -L "${TB_PROFILE_FOLDER}/" -type f -name "*backup_thunderbird_migration*")
if [ "${BACKUP_FILES}" != "" ]; then
    output_info "The following backups related to the Icedove to Thunderbird transition exist:"
    output_info ""
    cat << EOF
${BACKUP_FILES}
EOF
    output_info ""
else
    output_info "No backups related to the Icedove to Thunderbird transition found."
fi
}

# Create the file .thunderbird/.migrated with some content
do_create_migrated_mark_file (){
cat <<EOF > "${TB_PROFILE_FOLDER}/.migrated"
This file is automatically created by /usr/bin/thunderbird, it will be
created on every start of Thunderbird if does not exist.
Remove that file only if you know the propose of this file.

/usr/share/doc/thunderbird/README.Debian.gz has some information about this
dot file.
EOF
}

# Fix the file(s) ${TB_PROFILE_FOLDER}/${TB_PROFILE}/mimeTypes.rdf
# Search for pattern of '/usr/bin/iceweasel' and 'icedove' in the file and
# replace them with '/usr/bin/x-www-browser' and 'thunderbird'.
do_fix_mimetypes_rdf (){
for MIME_TYPES_RDF_FILE in $(find -L "${TB_PROFILE_FOLDER}/" -name mimeTypes.rdf); do
    RDF_SEARCH_PATTERN=$(grep '/usr/bin/iceweasel\|icedove' "${MIME_TYPES_RDF_FILE}")
    if [ "${RDF_SEARCH_PATTERN}" != "" ]; then
        output_debug "Backup ${MIME_TYPES_RDF_FILE} to ${MIME_TYPES_RDF_FILE}.backup_thunderbird_migration-${DATE}"
        cp "${MIME_TYPES_RDF_FILE}" "${MIME_TYPES_RDF_FILE}.backup_thunderbird_migration-${DATE}"

        output_debug "Fixing possible broken 'mimeTypes.rdf'."
        sed -i "s|/usr/bin/iceweasel|/usr/bin/x-www-browser|g;s|icedove|thunderbird|g" "${MIME_TYPES_RDF_FILE}"
    else
        output_info "No fix up for ${MIME_TYPES_RDF_FILE} needed."
    fi
done
}

# Inform the user we will starting the migration
do_inform_migration_start () {
# A system admin may avoid the dialog ...
if [ ! -f /etc/thunderbird/no_migration_popup ]; then
    case "${DESKTOP}" in
        gnome|mate|xfce)
            local_zenity --info --no-wrap --title "${TITLE}" --text "${START_MIGRATION}"
            if [ $? -ne 0 ]; then
                local_xmessage -center "${START_MIGRATION}"
            fi
            ;;

        kde)
            local_kdialog --title "${TITLE}" --msgbox "${START_MIGRATION}"
            if [ $? -ne 0 ]; then
                local_xmessage -center "${START_MIGRATION}"
            fi
            ;;

        *)
            xmessage -center "${START_MIGRATION}"
            ;;
    esac
fi
}

# Function that will do the fixing of mimeapps.list files
do_migrate_old_icedove_desktop() {
# Fix mimeapps.list files in the following folders which may still have
# icedove.desktop associations
#
#   ~/.config/
#   ~/.local/share/applications/
#
# icedove.desktop files are now deprecated, but still commonly around.
# We normally could remove them, but for safety only modify the files.
# These mimeapps.list files configures default applications for MIME types.

# Only jump in loop if we haven't already done a migration before or the
# user is forcing this by the option '--fixmime'.
if [ ! -f "${TB_PROFILE_FOLDER}/.migrated" ] || [ "${FORCE_MIMEAPPS_MIGRATE}" = "1" ]; then
    if [ ! -f "${TB_PROFILE_FOLDER}/.migrated" ]; then
        output_debug "No migration mark '${TB_PROFILE_FOLDER}/.migrated' found, checking mimeapps.list files for possible migration."
    elif [ "${FORCE_MIMEAPPS_MIGRATE}" = "1" ]; then
        output_debug "Migration enforced by user. Checking mimeapps.list files once again for possible migration."
    fi
    for MIMEAPPS_LIST in ${HOME}/.config/mimeapps.list ${HOME}/.local/share/applications/mimeapps.list; do
        # Check if file exists and has old icedove entry
        if [ -e "${MIMEAPPS_LIST}" ] && \
              grep -iq "\(userapp-\)*icedove\(-.*\)*\.desktop" "${MIMEAPPS_LIST}"; then

            output_debug "Fixing broken '${MIMEAPPS_LIST}'."
            MIMEAPPS_LIST_COPY="${MIMEAPPS_LIST}.backup_thunderbird_migration-${DATE}"

            # Fix mimeapps.list and create a backup, but it's really unlikely we
            # have an existing backup so no further checking here!
            # (requires GNU sed 3.02 or ssed for case-insensitive "I")
            sed -i.backup_thunderbird_migration-"${DATE}" "s|\(userapp-\)*icedove\(-.*\)*\.desktop|thunderbird.desktop|gI" "${MIMEAPPS_LIST}"
            if [ $? -ne 0 ]; then
                output_info "The configuration file for default applications for some MIME types"
                output_info "'${MIMEAPPS_LIST}' couldn't be fixed."
                output_info "Please check for potential problems like low disk space or wrong access rights!"
                logger -i -p warning -s "$0: [profile migration] Couldn't fix '${MIMEAPPS_LIST}'!"
                exit 1
            else
                output_debug "A copy of the configuration file of default applications for some MIME types"
                output_debug "was saved to '${MIMEAPPS_LIST_COPY}'."
            fi
        else
            output_info "No fix up for ${MIMEAPPS_LIST} needed."
        fi
    done
    output_debug "Setting migration mark '${TB_PROFILE_FOLDER}/.migrated'."
    do_create_migrated_mark_file
fi

# Migrate old user specific *.desktop entries
# Users may have created custom desktop shortcuts for Icedove in
# the past. These associations (files named like 'userapp-Icedove-*.desktop')
# are done in the folder $(HOME)/.local/share/applications/.

# Remove such old icedove.desktop files, superseeded by system-wide
# /usr/share/applications/thunderbird.desktop. The old ones in $HOME don't
# receive updates and might have missing/outdated fields.
# *.desktop files and their reverse mimeinfo cache provide information
# about available applications.

for ICEDOVE_DESKTOP in $(find "${HOME}/.local/share/applications/" -iname "*icedove*.desktop"); do
    output_debug "Backup ${ICEDOVE_DESKTOP} to ${ICEDOVE_DESKTOP}.backup_thunderbird_migration-${DATE}"
    ICEDOVE_DESKTOP_COPY=${ICEDOVE_DESKTOP}.backup_thunderbird_migration-${DATE}
    mv "${ICEDOVE_DESKTOP}" "${ICEDOVE_DESKTOP_COPY}"
    # Update the mimeinfo cache.
    # Not existing *.desktop files in there should simply be ignored by the system anyway.
    if [ -x "$(which update-desktop-database)" ]; then
        output_debug "Call 'update-desktop-database' to update the mimeinfo cache."
        update-desktop-database "${HOME}/.local/share/applications/"
    fi
done
}

# Print out an error message about failed migration
do_thunderbird2icedove_error_out (){
case "${DESKTOP}" in
    gnome|mate|xfce)
        local_zenity --info --no-wrap --title "${TITLE}" --text "${DOT_THUNDERBIRD_EXISTS}"
        if [ $? -ne 0 ]; then
            local_xmessage -center "${DOT_THUNDERBIRD_EXISTS}"
        fi
        FAIL=1
        ;;
    kde)
        local_kdialog --title "${TITLE}" --msgbox "${DOT_THUNDERBIRD_EXISTS}"
        if [ $? -ne 0 ]; then
            local_xmessage -center "${DOT_THUNDERBIRD_EXISTS}"
        fi
        FAIL=1
        ;;
    *)
        xmessage -center "${DOT_THUNDERBIRD_EXISTS}"
        FAIL=1
        ;;
esac
}

# Symlink .thunderbird to .icedove
do_thunderbird2icedove_symlink () {
output_debug "Trying to symlink '${TB_PROFILE_FOLDER}' to '${ID_PROFILE_FOLDER}'"
if ln -s "${ID_PROFILE_FOLDER}" "${TB_PROFILE_FOLDER}"; then
    output_debug "success."
    return 0
else
    case "${DESKTOP}" in
        gnome|mate|xfce)
            local_zenity --info --no-wrap --title "${TITLE}" --text "${THUNDERBIRD_PROFILE_LINKING_ERROR}"
            if [ $? -ne 0 ]; then
                local_xmessage -center "${THUNDERBIRD_PROFILE_LINKING_ERROR}"
            fi
            FAIL=1
            ;;
        kde)
            local_kdialog --title "${TITLE}" --msgbox "${THUNDERBIRD_PROFILE_LINKING_ERROR}"
            if [ $? -ne 0 ]; then
                local_xmessage -center "${THUNDERBIRD_PROFILE_LINKING_ERROR}"
            fi
            FAIL=1
            ;;
        *)
            xmessage -center "${THUNDERBIRD_PROFILE_LINKING_ERROR}"
            FAIL=1
            ;;
    esac
    output_debug "Ohh, that wasn't working, sorry! Do you have access rights to create a symlink?"
    return 1
fi
}

# Wrapping /usr/bin/kdialog calls
local_kdialog () {
if [ -f /usr/bin/kdialog ]; then
    /usr/bin/kdialog "$@"
    return 0
else
    return 1
fi
}

# Wrapping /usr/bin/xmessage calls
local_xmessage () {
if [ -f /usr/bin/xmessage ]; then
    /usr/bin/xmessage "$@"
else
    # this should never be reached as thunderbird has a dependency on x11-utils!
    output_info "xmessage not found"
fi
}

# Wrapping /usr/bin/zenity calls
local_zenity () {
if [ -f /usr/bin/zenity ]; then
    /usr/bin/zenity "$@"
    return 0
else
    return 1
fi
}

# Simple info output function
output_info () {
echo "INFO  -> $1"
}

# Simple debugging output function
output_debug () {
if [ "${VERBOSE}" = "1" ]; then
    echo "DEBUG -> $1"
fi
}

# How this script can be called
usage () {
cat << EOF

Usage: ${0##*/} [--help|-? ] | [--verbose ] [ -g ] [args-passed-to-thunderbird...]

This script parses its command line options and passes everything else on to
Thunderbird.  Note that some Thunderbird options need an additional argument
that can't be naturally mixed with other options!

  -g          Starts Thunderbird within gdb (needs package thunderbird-dbg)

  --help or ? Display this help and exit

  --verbose   Verbose mode, increase the output messages to stdout
              (Logging to /var/log/syslog isn't touched or increased by this
              option!)

Additional options:

  --fixmime      Only fixes MIME associations in
                 ~/.thunderbird/$profile/mimeTypes.rdf and exits. Can be
                 combined with '--verbose'.

  --show-backup  Collect the backup files which where made and print them to
                 stdout and exits.
EOF
cat << EOF

Examples:

 ${0##*/} --help

    Writes this help messages on stdout. If any other option is given it
    will be ignored. Note that Thunderbird has a '-h' option which needs
    to be used if you want the help output for Thunderbird.

 ${0##*/} --verbose

    Enable more debug messages on stdout. Only useful while developing the
    thunderbird packages or during profile migration.

 ${0##*/} -g

    Starts Thunderbird in a GDB session if packages gdb and thunderbird-dbg
    are installed.
EOF
# other debuggers will be added later, we need maybe a separate valgrind
# package! Note MDN site for valgrind
#    https://developer.mozilla.org/en-US/docs/Mozilla/Testing/Valgrind
# ${0##*/} -d gdb
#    The same as above, only manually specified the GDB debugging tool as
#    argument. Note that you probably will need additional parameter to
#    enable e.g. writing to a logfile.
#    It's also possible to specify valgrind, that will need to add additional
#    quoted arguments in any case!
#    The thunderbird binary must be compiled with valgrind support if you
#    want to use valgrind here!
#
#      ${0##*/} -d 'valgrind --arg1 --arg2' -thunderbird-arg1
cat << EOF

 ${0##*/} [args-passed-to-thunderbird...]

    Some example for invoking Thunderbird from the ommand line:
    Run in safe-mode with the JS Error console:

      ${0##*/} --safe-mode --jsconsole

    Call Thunderbird directly to compose a message with a specific
    attachement.

      ${0##*/} -compose "to='recipient@tld.org','attachment=/path/attachment'"

    See the all possible arguments for Thunderbird:

      ${0##*/} -h

EOF
}

# end local functions