#!/bin/sh

DICTFMT='../dictfmt --without-time --silent'
DICTUNFORMAT='../dictunformat'
DICT='../dict'
DICTD='../dictd'

DIFF='diff -u'

##################################################
##################################################
echo '                       DICTFMT!!!'

##################################################
echo 'Comparing .dict and .index files (except those created by dictfmt -h)'

rm -f _*

$DICTFMT -c5 _db.c5 < testdb.c5.txt || exit 1
$DICTFMT -h  _db.h  < testdb.h.txt  || exit 1
$DICTUNFORMAT _db.c5.index < _db.c5.dict > testdb.t.txt || exit 1

###

dictfmt_X_eq_Y (){
    printf "   $1 == $2 ..."
    $DICTFMT -$2  _db.$2  < testdb.$2.txt  || exit 1
    $DIFF _db.$1.index _db.$2.index || exit 1
    $DIFF _db.$1.dict  _db.$2.dict || exit 1
    printf 'passed\n'
}

dictfmt_X_eq_Y c5 e
dictfmt_X_eq_Y c5 f
dictfmt_X_eq_Y c5 j
dictfmt_X_eq_Y c5 p
dictfmt_X_eq_Y c5 t

###
printf "   c5 == expected ..."
$DIFF db.expect.index _db.c5.index || exit 1
$DIFF db.expect.dict  _db.c5.dict || exit 1
printf "passed\n"

##################################################
printf "Test for different dictfmt\'s options\n"

dictfmt_diff_opts (){
    num=$1
    shift

    dbname=$1
    shift

    printf "   test $num..."

    $DICTFMT "$@" _db.${dbname}_$num < testdb.${dbname}.txt || exit 1
    $DIFF db.expect.${dbname}_$num.dict  _db.${dbname}_$num.dict || exit 1
    $DIFF db.expect.${dbname}_$num.index _db.${dbname}_$num.index || exit 1

    printf 'passed\n'
}

# ASCII database
dictfmt_diff_opts 1 smiley -c5
dictfmt_diff_opts 2 smiley --allchars -u 'somewhere in Internet' -c5
dictfmt_diff_opts 3 smiley --allchars --headword-separator '-' -c5
dictfmt_diff_opts 4 smiley --allchars --without-headword \
   --without-header --without-url -c5
dictfmt_diff_opts 5 smiley --allchars --without-headword \
   --columns 15 --default-strategy lev --without-info -c5

# UTF-8 database
dictfmt_diff_opts 1 hello --utf8 -c5
dictfmt_diff_opts 2 hello --utf8 --allchars -u 'somewhere in Internet' -c5
dictfmt_diff_opts 3 hello --utf8 --allchars --headword-separator '-' -c5
dictfmt_diff_opts 4 hello --utf8 --allchars --without-headword \
   --without-header --without-url -c5
dictfmt_diff_opts 5 hello --utf8 --allchars --without-headword \
   --columns 15 --default-strategy lev --without-info -c5
