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