summaryrefslogtreecommitdiffstats
path: root/debian/thunderbird-wrapper.sh
blob: b3f7c2e7b73e1e7248965ddd9a5e16341e91ff3d (plain)
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
#!/bin/bash
# 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
#   care of the searching for an old user Icedove profile folder and adopting
#   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'.
#   The script will looking for the old profile folder and will symlink the
#   folder '${HOME}/.thunderbird' to the profile folder '${HOME}/.icedove'.
#
# Copyright:
#   Licensed under the terms of GPLv2+.

#set -x

TB_HELPER=${TB_HELPER:-"/usr/lib/thunderbird/thunderbird-wrapper-helper.sh"}
# sourcing external variables and helper functions
# 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."
    exit 1
fi

# some global variables
MOZ_APP_NAME=thunderbird
MOZ_APP_LAUNCHER=$(command -v "$0")
MOZ_LIBDIR=/usr/lib/${MOZ_APP_NAME}
ID_PROFILE_FOLDER=${HOME}/.icedove
TB_PROFILE_FOLDER=${HOME}/.thunderbird
TB_GDB_DEFAULT_OPTS=${TB_GDB_DEFAULT_OPTS:-"-ex \"handle SIG38 nostop\" -ex \"handle SIGPIPE nostop\""}

export TB_HELP=0
export TB_FAIL=0
export FORCE_MIMEAPPS_MIGRATE=0
export TB_VERBOSE=0
unset DEBUG
unset DEBUGGER

# set MOZ_APP_LAUNCHER for gnome-session
export MOZ_APP_LAUNCHER

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

declare -a TB_ARGS=()

for ARG in "$@"; do
    case "${ARG}" in
        --fixmime)
            FIXMIME=1
            FORCE_MIMEAPPS_MIGRATE=1
            ;;
        -g)
            DEBUGGER=1
            DEBUG=1
            ;;
#       -d)
#            USER_DEBUGGER=$2
#            DEBUG=1
#            shift
#            ;;
        --help)
            TB_HELP=1
            ;;
        --show-backup)
            SHOW_BACKUP=1
            ;;
        --verbose)
            output_info "[[ ... using verbose mode ... ]]"
            TB_VERBOSE=1
            ;;
        '?')
            usage >&2
            exit 1
            ;;
        # Every other argument is needed to get down to the TB starting call.
        *)
            TB_ARGS+=("${ARG}")
            ;;
    esac
    shift
done

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

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

# 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!
if [ "${FIXMIME}" = "1" ]; then
    do_fix_mimetypes_rdf
    do_migrate_old_icedove_desktop
    do_collect_backup_files
    exit 0
fi

if [ "${SHOW_BACKUP}" = "1" ]; then
    do_collect_backup_files
    exit 0
fi

#############################################################################
#                  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!"

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

# ... or the opposite if .icedove is symlinked to .thunderbird
elif { [ -d "${TB_PROFILE_FOLDER}" ] && [ -L "${ID_PROFILE_FOLDER}" ]; } && \
     [ "$(readlink -e "${ID_PROFILE_FOLDER}")" = "${TB_PROFILE_FOLDER}" ];then

    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
        # Fixing mimeTypes.rdf which may have registered the iceweasel binary
        # as browser, instead of x-www-browser
        do_fix_mimetypes_rdf

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

# 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?
elif { [ -d "${ID_PROFILE_FOLDER}" ] || [ -L "${ID_PROFILE_FOLDER}" ]; } && \
     { [ -d "${TB_PROFILE_FOLDER}" ] || [ -L "${TB_PROFILE_FOLDER}" ]; } && \
       [ "$(readlink -e "${TB_PROFILE_FOLDER}")" != "$(readlink -e "${ID_PROFILE_FOLDER}")" ]; then

    for CHECK in ${ID_PROFILE_FOLDER} ${TB_PROFILE_FOLDER}; do
        FILE_CHECK=$(readlink -e "${CHECK}")
        if [ "${FILE_CHECK}" != "" ] && [ -L "${CHECK}" ]; then
            output_debug "Found symlink '${CHECK}' pointing to '${FILE_CHECK}'."
        elif [ "${FILE_CHECK}" != "" ] && [ -d "${CHECK}" ]; then
            output_debug "Found folder '${FILE_CHECK}'."
        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

    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}'!"

    # display a graphical advice if possible
    do_thunderbird2icedove_error_out

fi

if [ "${TB_FAIL}" = 1 ]; then
    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!"
    exit 1
fi

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

if [ "${DEBUG}" = "" ]; then
    TB_ARGS_LINE=$(echo "${TB_ARGS[@]}" | sed "s/'/\"/g")
    output_debug "call '${MOZ_LIBDIR}/${MOZ_APP_NAME} ${TB_ARGS_LINE}'"
    exec "${MOZ_LIBDIR}"/"${MOZ_APP_NAME}" "${TB_ARGS[@]}"
else
    # User has selected GDB?
    if [ "${DEBUGGER}" = "1" ]; then
        # checking for GDB
        if [ -f /usr/bin/gdb ]; then
            if dpkg-query -W -f='${Version}' thunderbird-dbgsym &>/dev/null ; then
                output_info "Starting Thunderbird with GDB ..."
                TB_ARGS_LINE="/usr/bin/gdb ${TB_GDB_DEFAULT_OPTS} -ex run ${MOZ_LIBDIR}/${MOZ_APP_NAME}$(printf ' %q' "${TB_ARGS[@]}")"
                output_info "LANG= ${TB_ARGS_LINE}"
                LANG='' eval "exec ${TB_ARGS_LINE}"
            else
                output_info "No package 'thunderbird-dbgsym' installed! Please install first and restart."
                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"
                exit 1
            fi
        else
            output_info "No package 'gdb' installed! Please install first and try again."
            exit 1
        fi
    fi
fi

exit 0