#!/bin/sh

#	db_compact

set -e

if [ -z "$1" ] ; then
    echo usage: db_compact source_dir [wordlist_name]
    exit
fi

ORIG="$1"
if [ -z "$2" ] ; then
    FILE="wordlist.db"
else
    FILE="$2"
fi

TEMP="bf_compact.$$"

if [ -d $TEMP ] ; then
    echo Directory $TEMP already exists.
    echo exiting.
    exit
else
    mkdir $TEMP
fi

cp -p "$ORIG"/DB_CONFIG $TEMP || true
bogoutil -d "$ORIG"/"$FILE" | bogoutil -l $TEMP/"$FILE"

#checkpoint and remove unneeded log files
db_checkpoint -1h $TEMP
db_archive    -dh $TEMP

mv "$ORIG" "$ORIG.old"
mv  $TEMP  "$ORIG"
