aboutsummaryrefslogtreecommitdiffstats
path: root/bin/mbsync2mutt_mailboxes
blob: ccf9505a8e17fb128fbfb03bf67897c22bcddf05 (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
#!/usr/bin/env bash
#
# This script writes account specific output of mbsync -l to a configuration
# file, that can be used by mutt, to define its mailboxes.
# It requires isync (provides mbsync) to be installed.
#
# Creates an output file (defined by the first arguments to this script), which
# adds a 'mailboxes' entry (see `man 5 muttrc`) of all mailboxes in a specific
# account, that mbsync is setup to sync (see `man 1 mbsync`).

set -eou pipefail
IFS=$'\n'

declare -A mailboxes
output_file=""

get_mailboxes_by_account() {
  local account=""
  for entry in $(mbsync -al); do
    if [[ "$entry" == *':' ]]; then
      account="${entry/:/}"
      mailboxes["$account"]=""
    else
      mailboxes["$account"]="${mailboxes[$account]} '+${account}/${entry}'"
    fi
  done
}

output_mailboxes_to_file() {
  local tmpfile counter
  tmpfile="$(mktemp)"
  counter=0
  for account in "${!mailboxes[@]}"; do
    echo -e "mailboxes ${mailboxes[$account]}\n" >> "$tmpfile"
    counter=$(( "$counter" + 1 ))
  done
  if (( "$counter" > 0 )); then
    mv "$tmpfile" "$output_file"
  fi
}

if [ $# -ne 1 ]; then
  echo "Requires an output file name as first argument."
  exit 1
else
  output_file="$1"
fi

get_mailboxes_by_account
output_mailboxes_to_file