summaryrefslogtreecommitdiff
path: root/scripts/setup-email.sh
blob: 9cb23294b3b8f542f71810af231a93e2f52b2f1a (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
#!/usr/bin/env bash
# Craig Jennings <c@cjennings.net>

# Typically run on a fresh installation on a new machine.
# - Decrypts mail passwords from encrypted .gpg files to ~/.config/
# - Validates all email components of my Emacs email setup are in place
# - Validates local email directories exist; creates them if they don't exist
# - Performs initial email sync to local directories
# - Performs initial email indexing for both of my email accounts

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
EMACS_DIR="$(dirname "$SCRIPT_DIR")"
ENCRYPTED_PASSWORDS_DIR="$EMACS_DIR/assets/mail-passwords"
PASSWORD_DEST_DIR="$HOME/.config"

MBSYNC="$(command -v mbsync || true)"
MU="$(command -v mu || true)"
MU4EDIR="/usr/share/emacs/site-lisp/mu4e"
MSMTP="$(command -v msmtp || true)"

MBSYNCRC="$HOME/.mbsyncrc"
MSMTPRC="$HOME/.msmtprc"
MAILROOT="$HOME/.mail"
GMAILDIR="$MAILROOT/gmail"
CMAILDIR="$MAILROOT/cmail"

# Decrypt Mail Passwords
# Loop through all .gpg files in assets/mail-passwords/
# Skip if destination already exists, decrypt if missing
echo "→ checking mail passwords..."
if [[ -d "$ENCRYPTED_PASSWORDS_DIR" ]]; then
    for gpg_file in "$ENCRYPTED_PASSWORDS_DIR"/*.gpg; do
        [[ -f "$gpg_file" ]] || continue  # Skip if no .gpg files

        filename=$(basename "$gpg_file")
        dest_file="$PASSWORD_DEST_DIR/${filename%.gpg}"  # Strip .gpg extension

        if [[ -f "$dest_file" ]]; then
            echo "  ✓ $dest_file already exists, skipping"
        else
            echo "  → decrypting $filename..."
            if gpg -q -d "$gpg_file" > "$dest_file" 2>/dev/null; then
                chmod 600 "$dest_file"
                echo "  ✓ created $dest_file"
            else
                echo "  ✗ failed to decrypt $filename"
                rm -f "$dest_file"  # Clean up partial file
                exit 1
            fi
        fi
    done
else
    echo "  ⚠ encrypted passwords directory not found: $ENCRYPTED_PASSWORDS_DIR"
fi

# Check All Prerequisites
[[ -x "$MBSYNC"   ]] || { echo "ERROR: mbsync not found. Install 'isync'."; exit 1; }
[[ -x "$MU"       ]] || { echo "ERROR: mu not found. Install 'mu'."; exit 1; }
[[ -d "$MU4EDIR"  ]] || { echo "ERROR: mu4e elisp not found at $MU4EDIR. Install 'mu'."; exit 1; }
[[ -f "$MBSYNCRC" ]] || { echo "ERROR: '~/.mbsyncrc' missing."; exit 1; }
[[ -x "$MSMTP"    ]] || { echo "ERROR: msmtp not found. Install 'msmtp'."; exit 1; }
[[ -f "$MSMTPRC"  ]] || { echo "ERROR: '~/.msmtprc' missing."; exit 1; }

# Ensure Mail Dirs Exist
mkdir -p "$GMAILDIR" "$CMAILDIR"

# Initial Sync
echo "→ syncing all mail with mbsync ..."
"$MBSYNC" -aV

# Init MU and Index Email
echo "→ initializing mu ..."
"$MU" init --maildir="$MAILROOT" \
		  --my-address="craigmartinjennings@gmail.com" \
		  --my-address="c@cjennings.net"

echo "→ indexing mail ..."
"$MU" index

echo "✅ Mail setup complete."