#!/usr/bin/perl

# mutt-mailboxes: prints a list of mailboxes as set in .muttrc
# $Id: mutt-mailboxes,v 1.1 2003/11/06 22:30:16 grant Exp $

# picks the 'mailboxes' command out of .muttrc
# handles backslash-escaped newlines
# prints the list of mailboxes, separated by spaces
# does not include mail spool ($MAIL)

# for use with bash's $MAILPATH environment variable
# (prints a new mail notification when a mailbox changes)
#
# MAILPATH=$MAIL
# for i in `mutt-mailboxes`
# do
#     MAILPATH=$MAILPATH:$i'?new ${_##*/} mail'
# done
# export MAILPATH

use strict;
use warnings;

my $home = $ENV{HOME};
my $maildir = "$home/mail/";
my $muttrc = "$home/.muttrc";

# read mailboxes line from .muttrc
open( my $rc, $muttrc ) or die "failed open of $muttrc ($!)";
my $line;
while ( <$rc> ) {
    chomp;
    $line .= $_ if /^mailboxes/ .. /[^\\]$/;
}
close( $rc ) or die "failed close of $muttrc ($!)";

# strip out mailboxes command
$line =~ s/^mailboxes //;

# remove backslashes
$line =~ s/\\//g;              

# a list of all mailboxes except the inbox ('!')
my @mailboxes = grep { !/^!$/ } split( /\s+/, $line );

# convert mutt's '+' prefix to maildir
s/^\+/$maildir/e for @mailboxes; 

print join( " ", @mailboxes ), "\n";