#! /bin/bash -posix
# -*-sh-mode-*-
#     Copyright (C) 1998  Thomas Roessler <roessler@guug.de>
#                   1999  Roland Rosenfeld <roland@spinnaker.de>
# 
#     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 of the License, 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, write to the Free Software
#     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# $Id: lbdb-fetchaddr.sh.in,v 1.11 1999/09/15 09:43:12 roland Exp $

LBDB_VERSION=0.18.5

umask 077

prefix=/usr
exec_prefix=${prefix}
dotlock=/usr/bin/mutt_dotlock
fetchaddr=${prefix}/lib/lbdb/fetchaddr
db=$HOME/.lbdb/m_inmail.list

datefmt=${*:-'%Y-%m-%d %H:%M'}

case "$1" in
    -v) echo "lbdb-fetchaddr version $LBDB_VERSION"
	exit 0
	break;;
    -h) echo "Usage: $0 ['dateformat']  dig addresses with formatted timestamp"
	echo "       $0 -v              print version of lbdbq"
	echo "       $0 -h              this short help"
	exit 0
	break;;
esac

if [ ! -f $db ]; then
  dbdir=`dirname $db`
  if [ ! -d $dbdir ]; then
    mkdir -p $dbdir
    if [ ! -d $dbdir ]; then
      echo "Can't create $dbdir" >&2
      exit 1
    fi
  fi
  touch $db
  if [ ! -f $db ]; then
    echo "Can't create $db" >&2
    exit 1
  fi
fi

if $dotlock -r 10 -f $db
then
  : # okay, do nothing
else
  echo "Can't lock $db [dotlock returned $rv]." >&2
  exit 1
fi

if $fetchaddr "$datefmt" >> $db ; then
  touch $db.dirty
fi

$dotlock -u $db
