#! /bin/sh
# xbiffpop --- query pop server for xbiff new-mail notification

# Copyright (C) 1996, 1999 Noah S. Friedman

# Author: Noah Friedman <friedman@splode.com>
# Created: 1996-10-18

# $Id: xbiffpop,v 1.4 1999/05/25 04:49:50 friedman Exp $

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

# Commentary:

# To use this program, invoke in the background; it will launch xbiff for you.
# Requires my `from' program, which knows how to query pop servers.

# Code:

# If set, running under xbiff.  Just check for mail.
case "${XBIFFPOPSTATE+set}" in
  set )
    oldsize=`${XRDB-xrdb} -screen -query \
               | sed -n \
                     -e "/^$XBIFFPOPSTATE.mailcount:/!d" \
                     -e 's/^.*:[ 	]*//' \
                     -e p`

    newsize=`${FROM-from} -C`

    case "$XBIFFPOPDATE" in
      date ) stamp=`date '+%s'` ;;
      perl ) stamp=`${PERL-perl} -e 'print time'` ;;
    esac

    {
      echo "$XBIFFPOPSTATE.mailcount: $newsize"
      echo "$XBIFFPOPSTATE.timestamp: $stamp"
    } | ${XRDB-xrdb} -screen -merge -nocpp

    # exit status:
    #   0 = there is new mail
    #   1 = no change in size
    #   2 = mail has been cleared
    case "$newsize" in
      '' )         exit 1 ;;
      0  )         exit 2 ;;
      "$oldsize" ) exit 1 ;;
      *  )         exit 0 ;;
    esac
   ;;
esac


# Launching a new xbiff process.

# Name by which this script was invoked.
progname=`echo "$0" | sed -e 's/[^\/]*\///g'`

XBIFFPOPSTATE="xbiffpop.state.$$"
export XBIFFPOPSTATE

# Use `date' if possible because that's faster.
XBIFFPOPDATE=
stamp=`(date '+%s' | sed -e 's/^[0-9]*$/t/') 2> /dev/null`
case "$stamp" in
  t ) XBIFFPOPDATE=date ;;
  * )
    # Tack on a newline after the timestamp, because some versions of
    # sed will throw away the last line of input if it has no newline.
    # We don't need this above because the output of perl is assigned
    # to a variable first.
    stamp=`(${PERL-perl} -e 'print time . "\n"' \
             | sed -e 's/^[0-9]*$/t/') 2> /dev/null`
    case "$stamp" in
      t ) XBIFFPOPDATE=perl ;;
    esac
   ;;
esac
export XBIFFPOPDATE

# Garbage collect old xbiffpop resources which haven't been updated for
# at least an hour.
if (${PERL-perl} -v) > /dev/null 2>&1 ; then
  ${XRDB-xrdb} -screen -query \
    | ${PERL-perl} -e '
        while (<>) {
          if ($_ !~ /^xbiffpop.state.[0-9]*/o) { print $_; next; }
          chop $_;
          ($key, $val) = split (/:[ \t]/o, $_, 2);
          $ent{$key} = $val;
          if ($key =~ /\.timestamp$/o && (time - $val > 3600)) {
            $key =~ s/\.timestamp$//o;
            $ign{"${key}.mailcount"} = $ign{"${key}.timestamp"} = 1;
          }
        }
        foreach $key (keys (%ign)) { delete $ent{$key}; }
        foreach $key (keys (%ent)) { print "$key: $ent{$key}\n"; }
      ' \
    | ${XRDB-xrdb} -screen -load -nocpp
fi

exec ${XBIFF-xbiff} -xrm "xbiff*checkCommand: $progname" ${1+"$@"}

# eof
