Pasttle

application/x-sh multi-mutt.sh, created on 2020-12-08 18:09:22.058177

Edit as new paste

Get the raw version

#!/bin/bash

session="${1:-mutt}"

echo "reading all configured accounts"
declare -A accounts
for af in ~/.mutt/account-*.rc; do
  if [[ -f "$(realpath "$af")" ]]; then
    account=$(basename "$af" .rc | sed -e "s/account-//")
    accounts["$account"]="$af"
  fi
done
if [[ ${#accounts[*]} -gt 0 ]]; then
  declare -p accounts
else
  echo "no accounts configured?"
  exit
fi

if tmux has-session -t "$session"; then
  echo "attaching to existing multi-mutt session"
  tmux switch-client -t "$session"
else
  echo "creating new multi-mutt session"
  tmux new-session -d -s "$session" sleep 5
fi

for account in "${!accounts[@]}"; do
  if tmux list-windows -F '#{window_name}' -t "$session" | grep -q "^${account}$"; then
    echo "account $account already opened"
  else
    echo "opening account $account"
    tmux new-window -P -n "$account" "mutt -F ${accounts["$account"]}"
  fi
done

tmux kill-window -t "${session}:bash"

tmux attach-session -d -t "$session"