#! /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.5 2004/12/03 09:41:44 m-a Exp $

set -e

removebef=0
removeaft=0
while [ "$1" ] ; do
    case "$1" in
	-r) removeaft=1 ;;
	-R) removebef=1 ;;
	-*) 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 log files after archiving'
    echo >&2 ' -R - remove log files before archiving (unsafe!)'
    exit 1
fi

bogohome="$1"

nukelogs() {
    rm -f `db_archive -a -h "$bogohome"`
}

# run db_checkpoint to reduce number of logs
db_checkpoint -1 -h "$bogohome"

# remove if requested
if [ $removebef -eq 1 ] ; then nukelogs ; 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
