thunderbird-wrapper.sh 10.5 KB
Newer Older
1
#!/bin/bash
2
3
4
5
6
7
8
# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=sh textwidth=76
#
# File:
#   /usr/bin/thunderbird
#
# Purpose:
#   This is a wrapper script for starting the thunderbird binary with taking
9
#   care of the searching for an old user Icedove profile folder and adopting
10
11
12
13
14
#   the folder into the new place if possible.
#
# Environment:
#   The Icedove binary was using the profile folder '${HOME}/.icedove'. The
#   Mozilla default for the Thunderbird binary is '${HOME}/.thunderbird'.
15
16
#   The script will looking for the old profile folder and will symlink the
#   folder '${HOME}/.thunderbird' to the profile folder '${HOME}/.icedove'.
17
18
19
20
21
22
#
# Copyright:
#   Licensed under the terms of GPLv2+.

#set -x

23
TB_HELPER=${TB_HELPER:-"/usr/lib/thunderbird/thunderbird-wrapper-helper.sh"}
24
# sourcing external variables and helper functions
25
26
27
28
29
# hide the sourcing for http://www.shellcheck.net/
# shellcheck source=/dev/null
. "${TB_HELPER}"
if [ $? -ne 0 ]; then
    echo "sourcing ${TB_HELPER} failed - giving up."
30
31
    exit 1
fi
32
33
34

# some global variables
MOZ_APP_NAME=thunderbird
35
MOZ_APP_LAUNCHER=$(which "$0")
36
37
38
MOZ_LIBDIR=/usr/lib/${MOZ_APP_NAME}
ID_PROFILE_FOLDER=${HOME}/.icedove
TB_PROFILE_FOLDER=${HOME}/.thunderbird
39
TB_GDB_DEFAULT_OPTS=${TB_GDB_DEFAULT_OPTS:-"-ex \"handle SIG38 nostop\" -ex \"handle SIGPIPE nostop\""}
40

41
42
export TB_HELP=0
export TB_FAIL=0
43
export FORCE_MIMEAPPS_MIGRATE=0
44
export TB_VERBOSE=0
45
46
unset DEBUG
unset DEBUGGER
47

48
49
50
# set MOZ_APP_LAUNCHER for gnome-session
export MOZ_APP_LAUNCHER

51
52
53
# let Thunderbird detect the system dictionaries
export DICPATH=/usr/share/hunspell

54
declare -a TB_ARGS=()
55
56
57

for ARG in "$@"; do
    case "${ARG}" in
58
59
60
        --fixmime)
            FIXMIME=1
            FORCE_MIMEAPPS_MIGRATE=1
61
            ;;
62
        -g)
63
64
65
            DEBUGGER=1
            DEBUG=1
            ;;
66
#       -d)
67
68
#            USER_DEBUGGER=$2
#            DEBUG=1
69
#            shift
70
#            ;;
71
        --help)
72
            TB_HELP=1
73
74
            ;;
        --show-backup)
75
            SHOW_BACKUP=1
76
            ;;
77
78
        --verbose)
            output_info "[[ ... using verbose mode ... ]]"
79
            TB_VERBOSE=1
80
            ;;
81
82
83
84
        '?')
            usage >&2
            exit 1
            ;;
85
86
        # Every other argument is needed to get down to the TB starting call.
        *)
87
            TB_ARGS+=("${ARG}")
88
            ;;
89
    esac
90
    shift
91
92
93
94
done

# sanity check
if [ "$DEBUGGER" != "" ] && [ "$USER_DEBUGGER" != "" ]; then
95
    output_info "You can't use option '-g and '-d' at the same time!"
96
97
98
99
    usage
    exit 1
fi

100
101
# If '--help' was called show usage() and exit immediately without other
# helpers can be called.
102
if [ "${TB_HELP}" = "1" ]; then
103
104
105
106
    usage
    exit 0
fi

107
108
109
110
# The user is forcing to do the MIME fixing (again). If called a used
# profile(s) folder ~/.thunderbird will be also reworked. That's not the
# case if the user is starting this wrapper for a first time and only a
# folder ~/.thunderbird is existing!
111
112
113
114
115
116
if [ "${FIXMIME}" = "1" ]; then
    do_fix_mimetypes_rdf
    do_migrate_old_icedove_desktop
    do_collect_backup_files
    exit 0
fi
117

118
119
120
121
122
if [ "${SHOW_BACKUP}" = "1" ]; then
    do_collect_backup_files
    exit 0
fi

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
#############################################################################
#                  User Thunderbird Profile Adoptions                       #
#                                                                           #
# The users Icedove/Thunderbird profile(s) doesn't need to be modified in a #
# different and complicated way. We simply need to ensure that the          #
# Thunderbird binary is finding the existing profiles in the folder         #
# $(HOME)/.thunderbird folder or a valid symlink pointing to the profiles.  #
#                                                                           #
# To "migrate" an old existing Icedove profile we can simply do a symlink   #
# from $(HOME)/.thunderbird --> $(HOME)/.icedove .                          #
#                                                                           #
# Afterwards do some changes to the file mimeTypes.rdf within every         #
# profile. Also we can modify existing *icedove*.desktop entries in the     #
# files.                                                                    #
#                                                                           #
#     $(HOME)/.config/mimeapps.list                                         #
#     $(HOME)/.local/share/applications/mimeapps.list                       #
#                                                                           #
#############################################################################

# First try the default case for modification, there is only a folder
# ${ID_PROFILE_FOLDER} and we can symlink to this.
if { [ -d "${ID_PROFILE_FOLDER}" ] || [ -L "${ID_PROFILE_FOLDER}" ]; } && \
   { [ ! -d "${TB_PROFILE_FOLDER}" ] && [ ! -L "${TB_PROFILE_FOLDER}" ]; }; then
    output_debug "found folder '${ID_PROFILE_FOLDER}'"
    output_debug "not found folder or symlink '${TB_PROFILE_FOLDER}'"
    output_debug "Start Thunderbird profile adoptions, please be patient!"
150

151
    # open a pop-up window with a message about starting migration
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
    do_inform_migration_start

    # do the symlinking
    do_thunderbird2icedove_symlink

    # fixing mimeTypes.rdf which may have registered the iceweasel binary
    # as browser, instead of x-www-browser
    do_fix_mimetypes_rdf

    # Fix local mimeapp.list and *.desktop entries
    do_migrate_old_icedove_desktop

    # we are finished
    output_info "Thunderbird Profile adoptions done."
    do_collect_backup_files
fi

# We found both profile folder, and .thunderbird is a symlink,
# we need to check if .thunderbird is symlinked to .icedove
if { [ -d "${ID_PROFILE_FOLDER}" ] && [ -L "${TB_PROFILE_FOLDER}" ]; } && \
   [ "$(readlink -e "${TB_PROFILE_FOLDER}")" = "${ID_PROFILE_FOLDER}" ];then

    output_debug "Found folder ${ID_PROFILE_FOLDER}, found a symlink ${TB_PROFILE_FOLDER} pointing to ${ID_PROFILE_FOLDER}"

    # Check if we need to do some migration, the linking could be existing
    # before we switched back to Thunderbird.
    if [ ! -f "${TB_PROFILE_FOLDER}/.migrated" ]; then
        # Fixing mimeTypes.rdf which may have registered the iceweasel binary
        # as browser, instead of x-www-browser
        do_fix_mimetypes_rdf

        # Fix local mimeapp.list and *.desktop entries
        do_migrate_old_icedove_desktop
185
    fi
186

187
188
# ... or the opposite if .icedove is symlinked to .thunderbird
elif { [ -d "${TB_PROFILE_FOLDER}" ] && [ -L "${ID_PROFILE_FOLDER}" ]; } && \
189
     [ "$(readlink -e "${ID_PROFILE_FOLDER}")" = "${TB_PROFILE_FOLDER}" ];then
190
191
192
193
194
195

    output_debug "Found folder ${TB_PROFILE_FOLDER}, found a symlink ${ID_PROFILE_FOLDER} pointing to ${TB_PROFILE_FOLDER}"
    output_debug "You may want to remove the symlink ${ID_PROFILE_FOLDER}? It's probably not needed anymore."

    # Check if we need to do some migration ...
    if [ ! -f "${TB_PROFILE_FOLDER}/.migrated" ]; then
196
197
        # Fixing mimeTypes.rdf which may have registered the iceweasel binary
        # as browser, instead of x-www-browser
198
199
200
201
        do_fix_mimetypes_rdf

        # Fix local mimeapps.list and *.desktop entries
        do_migrate_old_icedove_desktop
202
    fi
203

204
205
206
207
# We found both profile folder, but they are not linked to each other! This
# is a state we can't solve on our own !!! The user needs to interact and
# has probably an old or otherwise used Thunderbird installation. Which one
# is the correct one to use?
208
209
elif { [ -d "${ID_PROFILE_FOLDER}" ] || [ -L "${ID_PROFILE_FOLDER}" ]; } && \
     { [ -d "${TB_PROFILE_FOLDER}" ] || [ -L "${TB_PROFILE_FOLDER}" ]; } && \
210
       [ "$(readlink -e "${TB_PROFILE_FOLDER}")" != "$(readlink -e "${ID_PROFILE_FOLDER}")" ]; then
211
212
213
214

    for CHECK in ${ID_PROFILE_FOLDER} ${TB_PROFILE_FOLDER}; do
        FILE_CHECK=$(readlink -e "${CHECK}")
        if [ "${FILE_CHECK}" != "" ] && [ -L "${CHECK}" ]; then
215
            output_debug "Found symlink '${CHECK}' pointing to '${FILE_CHECK}'."
216
        elif [ "${FILE_CHECK}" != "" ] && [ -d "${CHECK}" ]; then
217
            output_debug "Found folder '${FILE_CHECK}'."
218
219
220
221
222
        else
            output_debug "${CHECK} is probably a symlink pointing to a non existing target, at least not to ${ID_PROFILE_FOLDER}."
            logger -i -p warning -s "$0: [profile migration] ${CHECK} is probably a symlink pointing to a non existing target, at least not to ${ID_PROFILE_FOLDER}."
        fi
    done
223

224
225
226
227
228
    output_debug "There are already the folders or symlinks '${TB_PROFILE_FOLDER}' and '${ID_PROFILE_FOLDER}'"
    output_debug "which not pointing to each other, will do nothing as don't know which folder to use."
    output_debug "Please investigate by yourself! Maybe you will find additional information in '/usr/share/doc/thunderbird/README.Debian.gz'."
    logger -i -p warning -s "$0: [profile migration] Couldn't migrate Icedove into Thunderbird profile due existing or symlinked folder '${TB_PROFILE_FOLDER}'!"

229
    # display a graphical advice if possible
230
    do_thunderbird2icedove_error_out
231
232
233

fi

234
if [ "${TB_FAIL}" = 1 ]; then
235
236
    output_info "An error happened while trying to migrate the old Icedove profile folder '${ID_PROFILE_FOLDER}'."
    output_info "Please take a look into the syslog file!"
237
238
239
    exit 1
fi

240
# If we are here we going simply further by starting Thunderbird.
241
242

if [ "${DEBUG}" = "" ]; then
243
    TB_ARGS_LINE=$(echo "${TB_ARGS[@]}" | sed "s/'/\"/g")
244
245
    output_debug "call '${MOZ_LIBDIR}/${MOZ_APP_NAME} ${TB_ARGS_LINE}'"
    exec "${MOZ_LIBDIR}"/"${MOZ_APP_NAME}" "${TB_ARGS[@]}"
246
247
else
    # User has selected GDB?
248
    if [ "${DEBUGGER}" = "1" ]; then
249
250
        # checking for GDB
        if [ -f /usr/bin/gdb ]; then
251
            if dpkg-query -W -f='${Version}' thunderbird-dbgsym &>/dev/null ; then
252
                output_info "Starting Thunderbird with GDB ..."
253
                TB_ARGS_LINE="/usr/bin/gdb ${TB_GDB_DEFAULT_OPTS} -ex run ${MOZ_LIBDIR}/${MOZ_APP_NAME}$(printf ' %q' "${TB_ARGS[@]}")"
254
255
                output_info "LANG= ${TB_ARGS_LINE}"
                LANG='' eval "exec ${TB_ARGS_LINE}"
256
            else
257
                output_info "No package 'thunderbird-dbgsym' installed! Please install first and restart."
258
259
260
                output_info "More information how to adjust your sources.list to being able installing"
                output_info "dbgsym packages in generally can be found here:"
                output_info "https://wiki.debian.org/HowToGetABacktrace#Installing_the_debugging_symbols"
261
262
263
                exit 1
            fi
        else
264
            output_info "No package 'gdb' installed! Please install first and try again."
265
266
267
268
269
270
            exit 1
        fi
    fi
fi

exit 0