#!/usr/bin/perl -w
#
# Bcheck -- check for new mail in "bulk" maildrops
#
# Called by xlbiff(1).  Not intended for general consumption.
#
# $Id: Bcheck,v 1.3 2003/11/09 00:17:51 esm Exp $
#
(our $ME = $0) =~ s,.*/,,;

our $State = ".$ME.results";

use strict;
use Digest::MD5;

my $context = Digest::MD5->new;

# All operations take place under our Mail dir.  Theoretically we should
# read this (Mail) from the Path: component of ~/.mh_profile.  But I'm lazy.
chdir "$ENV{HOME}/Mail"
  or die "$ME: cd: $!\n";

my $seen = 0;					# Number of unseen messages

# Exmh keeps this nice cache for us.  If you don't use exmh, you could
# instead run 'folders -fast -recurse'.
open FOLDERS, ".folders"
  or die "$ME: open(.folders): $!\n";
while (defined (my $f = <FOLDERS>)) {
  chomp $f;

  open SEQ, "$f/.mh_sequences"
    or next;
  while (<SEQ>) {
    if (/^unseen:\s+(.*)/) {
#      printf STDERR "$f: $1\n";
      $seen++;
      $context->add($f, $1);
    }
  }
  close SEQ;
}
close FOLDERS;

print "0\n";	# xlbiff expects this

# If no new messages exist, pop down the window
if ($seen == 0) {
  exit 2;			# 0 = change, 1 = no change, 2 = zero'ed
}

# Read results from the last time we ran.
my $last_results = "";
open LAST, $State and do {
  chop ($last_results = <LAST>);
  close LAST;
};

# Compare our results now against those from last time.  If no change, exit now
my $new = $context->hexdigest;
#printf STDERR "old: %s\nnew: %s\n", $last_results, $new;
if ($new eq $last_results) {
  exit 1;			# 1 = no change, 0 = change, 2 = zero'ed
}


# Something changed. Write out our new results, and exit with status 0 (change)
my $tmp = "$State.tmp.$$";
open OUT, ">", $tmp
  or die "$ME: Error creating $tmp: $!\n";
print OUT $new,"\n";
close OUT;

chmod 0600, $tmp;
rename $tmp => $State
  or die "$ME: rename ($tmp => $State): $!\n";

exit 0;
