#!/bin/sh
set -e

if [ $# -eq 0 ] || { [ $# -eq 1 ] && [ "$1" = - ]; }; then
	echo "Usage: ${0##*/} SKK-JISYO [SKK-JISYO-CDB]" >&2
	exit 1
fi

if [ "$1" != - ]; then
	# Open input file while we still have a chance to catch errors
	# (the shell ignores the exit status of everything except the
	# last command in a pipe).
	exec < "$1"
fi

LC_ALL=C awk '
    /^[^;]/ {
	s = substr($0, index($0, " ") + 1)
	print "+" length($1) "," length(s) ":" $1 "->" s
    }
    END {
	print ""
    }
' | cdb -c "${2-$1.cdb}"
