~elementary-os/contractor/thunderbird-contracts

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
#!/bin/bash

# This is a wrapper script to attach several files to an email in Thunderbird.
# Written by Sergey "Shnatsel" Davidoff <sergey@elementaryos.org>
# License: 3-clause BSD

# Thunderbird is very picky about the file paths and will just hang in case it 
# doesn't like something.
# If it does, I'm probably doing something slightly wrong here.

#TODO: define a Gettext domain

# Disable history substitution on "!" symbols so we can have them in strings
set +H

if [ -z "$1" ] || [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
    echo $"Usage: $0 /path/to/file [/path/to/another/file...]
Relative paths are also supported, but files with comma in the name are NOT."
    exit 1 # so that calling without parameters is counted as a failure
fi

while [ -n "$1" ]; do
    # Check if the file exists; if not, complain and exclude the file.
    # Thunderbird will hang if given a nonexistent file.
    if [ -e "$1" ]; then
        if ! ( echo "$1" | grep -q ',' ); then
            FILES="$FILES,$1"
        else
            comma_files="$comma_files
$1"
        fi
    else
        nonexistent_files="$nonexistent_files
$1"
    fi
    shift
done

# Remove the commas in the beginning and end of file list,
# otherwise Thunderbird will hang.
FILES=${FILES%,}
FILES=${FILES#,}

# Error reporting
if [ -n "$comma_files" ]; then
    ERRORS="$ERRORS
"$"Thunderbird cannot handle files with commas in the name. 
These files were not attached: $comma_files"
fi
if [ -n "$nonexistent_files" ]; then
    ERRORS="$ERRORS
"$"These files do not seem to exist and were ignored: $nonexistent_files"
fi
ERRORS=${ERRORS#'
'} # Remove the \n in the beginning of the error list, it's prettier that way

if [ -n "$FILES" ]; then
    if [ -n "$ERRORS" ]; then
        notify-send --icon=thunderbird $"Some files were not attached" "$ERRORS"
    fi
    # Finally execute Thunderbird and exit with its exit code
    thunderbird -compose "attachment='${FILES}'"
else
    if [ -n "$ERRORS" ]; then
        notify-send --icon=thunderbird $"No files were attached!" "$ERRORS"
    fi
    exit 1
fi