#!/usr/local/bin/perl

##################################################################################################################
#
# Source  : $Source: /home/simran/cvs/bin/xbiff-checkmail.pl,v $
# Revision: $Revision: 1.2 $
# Date    : $Date: 2002/10/09 03:43:01 $
# Author  : $Author: simran $
#
# Description:  used by xbiff to determine if there is new mail... 
#
##################################################################################

use strict;

my $inbox_dir = "/home/simran/.evolution/mail/local/Inbox.sbd";
my $size      = 0;
my $orig_size = &readSize();

#
# Maildir format 
#
# foreach my $file (<$inbox_dir/mbox/cur/*>, <$inbox_dir/subfolders/*/mbox/cur/*>) {
#   next if (! $file);
#   $size += -s $file;
# }

#
# mbox format 
#
foreach my $file (<$inbox_dir/*>) {
  next if (! -f "$file.cmeta");
  $size += -s $file;
}

if ($size > $orig_size) {
  open(OUT, "> /tmp/gnome-checkmail.newmail");
  print OUT "1";
  close(OUT);
}
  
&writeOut($size);

if ($size > $orig_size) {
  exit(0);
}
elsif ($size < $orig_size) {
  exit(2);
}
else {
  exit(1);
}

sub writeOut {
  my $size = shift;
  open(OUT, "> /tmp/gnome-checkmail.size") || die "Could not write to file: $!";
  print OUT $size;
  close(OUT);
}

sub readSize {
  open(IN, "/tmp/gnome-checkmail.size") || return 0;
  my $size = <IN>;
  chomp($size);
  close(IN);
  return $size || 0;
}
