#!/bin/sh

usage (){
	cat <<EOF
This program is intended to create DICTD database files
necessary for integrating plugin into DICTD.
It creates two special entries:
00-database-plugin
  Its "definition" is a filename for plugin (possibly .so)
  You should specify the filename as a parameter (see below).
  If the filename does not begin with '.' or '/', it is prepended with
  \$libexecdir. It is a compile time option. You can change this behaviour
  by editing Makefile or running ./configure --libexecdir=...
00-database-plugin-data
  Its "definition" is a data passed to plugin during
  initialization.
Take a note that -c5 input format is used. You cannot specify
another one.
usage:
dictfmt_plugin [OPTIONS] <path_to_plugin> [DICTFMT_OPTIONS] [datafiles...]
OPTIONS:
    --help displays this help
DICTFMT_OPTIONS:
    all they are from dictfmt
EOF
}

while test $# -ne 0; do
	case $1 in
		--help)
			usage
			exit 0;;
		-*)
			echo "invalid argument '$1'" 1>&2
			echo "run '$0 --help' for help" 1>&2
			exit 1;;
		*)
			break;;
	esac
	shift
done

filename=$1
shift

if test -z "$filename"; then
	usage
	exit 1
fi

{
	echo -en "\
_____\n\
\n\
00-database-plugin\n\
	$filename\n\
_____\n\
\n\
00-database-plugin-data\n\
"

	cat
} |
dictfmt -q --without-header --without-url -c5 "$@"
