#! /bin/sh

# This file dumps a bogofilter to standard output in POSIX-tar format.
#
# Requires: pax
#
# (C) 2004 by Matthias Andree
# GNU GPL v2.

# $Id: bf_tar,v 1.13 2005/03/14 01:29:47 m-a Exp $

set -e

REMOVEBEF=0
REMOVEAFT=0
while [ "$1" ] ; do
    case "$1" in
	-r) REMOVEAFT=1 ;;
	-R) REMOVEBEF=1 ;;
	--) shift ; break ;;
	-*) echo >&2 "`basename $0`: unknown option $1" ; exit 1 ;;
	*) break;
    esac
    shift
done

if [ $# -ne 1 ] ; then
    echo >&2 "Usage: `basename $0` [options] bogodir > outfile.tar"
    echo >&2 "   or: `basename $0` [options] bogodir | gzip -c >outfile.tar.gz"
    echo >&2 'Options are:'
    echo >&2 ' -r - remove inactive log files after archiving'
    echo >&2 ' -R - remove inactive log files before archiving (use with caution)'
    exit 1
fi

BOGOHOME="$1"

if [ ! -d $BOGOHOME ] ; then
    echo "$BOGOHOME" must be a directory, not a file
    exit 1
fi

nukelogs() {
    bogoutil --db-prune="$BOGOHOME"
}

# remove if requested
if [ $REMOVEBEF -eq 1 ] ; then
    nukelogs
else
    bogoutil --db-checkpoint "$BOGOHOME"
fi

# database first, if it's newer than the log, it's not recoverable!
# pax options: -w: write archive, -v: verbosely, -x ustar: choose tar
# format.
( db_archive -a -s -h "$BOGOHOME" ; db_archive -a -l -h "$BOGOHOME" ) \
| pax -w -v -x ustar

# remove if requested
if [ $REMOVEAFT -eq 1 ] ; then nukelogs ; fi
