#!/usr/bin/bash

if [ "${1}" = "" ] || [ "${1}" = "--help" ]; then
	echo "Migrate Bogofilter Berkeley database into the current format."
	echo "Expects one argument, the file name to migrate."
	echo "Usage: bogomigrate-berkeley wordlist.db"
	exit 1;
fi

if [ -e "${1}" ]; then
	bogoutil-berkeley -d "${1}" > "${1}.txt.migrate" && \
	bogoutil -I "${1}.txt.migrate" -l "${1}.migrated" && \
	rm "${1}.txt.migrate" && \
	mv "${1}" "${1}.berkeley.bak" && \
	mv "${1}.migrated" "${1}" && \
	echo "Successfully migrated '${1}' with `bogoutil -d "${1}" | wc -l` entries." && \
	echo "Backup of the original file is stored as '${1}.berkeley.bak'."
else
	echo "File '${1}' does not exist" 1>&2
fi
