#!/bin/bash
# markallread by Andrew Oakley www.aoakley.com Public Domain 2009-01-11
# A script to mark all maildir mail as read, including folders
# Assumptions:
# * Your maildir folder is ~/.maildir
# * Folders use Dovecot IMAP . prefix eg. ~/.maildir/.foldername
# * Folder names do not contain spaces

# Loop through ~/.maildir/.foldername/new/ folders
# This also does the ~/.maildir Inbox since it matches ~/.maildir./new/
for i in `ls -1 ~/.maildir/.*/new/*`
do
  # Move from /new/ to /cur/
  # Also add status "seen" to message by appending :2,S to filename
  # as per http://cr.yp.to/proto/maildir.html
  mv $i `echo $i | sed -r "s/^(.*)\/new\/(.*)$/\1\/cur\/\2:2,S/"`
done

# Loop through ~/.maildir/.foldername/cur/ folders
# Required in case new mail has been moved to a cur dir without reading
# Note how these already have :2, at the end of the filename
for i in `ls -1 ~/.maildir/.*/cur/*:2, 2>/dev/null`
do
  # Add status "seen" to message by appending S to filename
  mv $i `echo $i | sed -r "s/^(.*)$/\1S/"`
done
