#!/bin/sh

DICTFMT='../dictfmt --without-time --silent'
DICTUNFORMAT='../dictunformat'
DICT='../dict'
DICTD="../dictd -L `pwd`/_dictd.log"

DIFF='diff -u'

##################################################
cmp_2_files (){
    if ! $DIFF "$1" "$2" > log.txt; then
	cat 1>&2 <<EOF
$1 and $2 are different,
see test/log.txt file a difference.
EOF
	exit 1
    fi
}

##################################################
##################################################
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
    cmp_2_files _db.$1.index _db.$2.index
    cmp_2_files _db.$1.dict  _db.$2.dict
    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 ..."
cmp_2_files db.expect.index _db.c5.index
cmp_2_files db.expect.dict  _db.c5.dict
printf "passed\n"

##################################################
printf "Tests 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
    cmp_2_files db.expect.${dbname}_$num.dict  _db.${dbname}_$num.dict
    cmp_2_files db.expect.${dbname}_$num.index _db.${dbname}_$num.index

    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

# UTF-8 database (mostly cyrillic)
dictfmt_diff_opts '1' cyrillic --utf8 -c5 -s 'rus words' \
   -u 'URL here' --default-strategy word

##################################################
##################################################
echo '                       DICTD!!!'

generate_commands (){
    conf_file=$1
    shift

    strats=`echo 'show strat' |
	$DICTD -i -c $conf_file |
	awk '!/^[0-9.]/ {print $1}' | LC_ALL=C sort`
    if test $? -ne 0; then
	exit 2
    fi

#    echo "strats=$strats" 1>&2
#    strats="exact prefix suffix substring"
    if test $# = 0; then
	strats=exact
    else
	strats="$@"
    fi

    awk -v strats="$strats" '
    BEGIN {
	cnt=split(strats, strats_arr)
    }
    {
	printf "define \ttestdb\t \"%s\"\n", $0
	printf "define testdb \"%s\"\n", $0

	for (i=1; i <= cnt; ++i){
	    printf "match\ttestdb %s\t\"%s\"\n", strats_arr [i], $0
	}
    }'
}

dictd_test_inetd (){
    dbtype=$1
    shift

    conf_file=_testdb.$dbtype.conf

    m4 -DCURR_DIR=`pwd` testdb.$dbtype.conf >$conf_file || exit 1

    index_file=`sed -n 's,^.*/\(.*[.]index\).*$,\1,p' $conf_file`

    in_file=_dictd_${dbtype}_in.txt

    cut -f 1 "$index_file" |
    generate_commands $conf_file "$@" > "$in_file"

    $DICTD -c $conf_file -i --stdin2stdout < "$in_file" |

    awk '/^2[25]0 / { print $1; next; } {print}' \
	> _dictd_${dbtype}_res.txt || exit 1
    cmp_2_files dictd_${dbtype}_res.expected.txt _dictd_${dbtype}_res.txt
}

#dictd_test_inetd smiley_1
#printf 'smiley_1 passed\n'
dictd_test_inetd smiley_2
printf 'smiley_2 passed\n'
#dictd_test_inetd smiley_3
#printf 'smiley_3 passed\n'
dictd_test_inetd smiley_4
printf 'smiley_4 passed\n'
dictd_test_inetd smiley_5
printf 'smiley_5 passed\n'

dictd_test_inetd hello_1
printf 'hello_1 passed\n'
dictd_test_inetd hello_2
printf 'hello_2 passed\n'
dictd_test_inetd hello_3
printf 'hello_3 passed\n'
dictd_test_inetd hello_4
printf 'hello_4 passed\n'
dictd_test_inetd hello_5
printf 'hello_5 passed\n'

dictd_test_inetd cyrillic_1 exact word lev suffix prefix substring
printf 'cyrillic_1 passed\n'
