#!/bin/sh

set -e

DICTFMT='../dictfmt --without-time --silent --without-ver'
DICTUNFORMAT='../dictunformat'
DICT='../dict'
DICTD="../dictd -L `pwd`/_dictd.log"
DICTFMT_I2W='../dictfmt_index2word --silent'
DICTFMT_I2S='../dictfmt_index2suffix'
DIFF="${DIFF_PROG-diff -u}"

##################################################
PATH=$(dirname $(pwd)):$PATH
export PATH

##################################################
cmp_2_files (){
    if test $# -eq 4; then
    	fn1="$3"
    	fn2="$4"
    else
    	fn1="$1"
    	fn2="$2"
    fi

    if $DIFF "$1" "$2" > log.txt; then
    	true
    else
	cat 1>&2 <<EOF
$fn1 and $fn2 are different,
see test/log.txt file a difference.
EOF
	exit 1
    fi
}

cmp_index_files (){
    LC_ALL=C sort "$1" > _tmpcmp1.txt
    LC_ALL=C sort "$2" > _tmpcmp2.txt
    cmp_2_files _tmpcmp1.txt _tmpcmp2.txt "$1" "$2"
}

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

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

rm -f _*

$DICTFMT -c5 _db.c5 < @srcdir@/testdb.c5.txt
$DICTFMT -h  _db.h  < @srcdir@/testdb.h.txt
$DICTUNFORMAT _db.c5.index < _db.c5.dict > _testdb.t.txt

###
printf "   -c5 == -t ..."
$DICTFMT -t _db.t < _testdb.t.txt
cmp_index_files _db.c5.index _db.t.index
cmp_2_files _db.c5.dict  _db.t.dict
printf "passed\n"

###
printf "   -c5.utf8 ..."
$DICTFMT --utf8 -c5 _testdb_utf8 < @srcdir@/testdb.c5.txt
cmp_2_files _testdb_utf8.dict  @srcdir@/db.expect.testdb_utf8.dict
cmp_index_files _testdb_utf8.index @srcdir@/db.expect.testdb_utf8.index
printf "passed\n"

###
printf "   -c5 == -X ..."
dictfmt_X_eq_Y (){
    printf "   $1 == $2 ..."
    $DICTFMT -$2  _db.$2  < @srcdir@/testdb.$2.txt
    cmp_index_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
printf "passed\n"

###
printf "   c5 == expected ..."
cmp_index_files @srcdir@/db.expect.index _db.c5.index
cmp_2_files @srcdir@/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 "%s" "   test $dbname $num..."

    $DICTFMT "$@" _db.${dbname}_$num < @srcdir@/testdb.${dbname}.txt
    cmp_2_files @srcdir@/db.expect.${dbname}_$num.dict  _db.${dbname}_$num.dict
    cmp_index_files @srcdir@/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

# case sensitive flag
dictfmt_diff_opts '1' mixedcase --utf8 -c5 -s 'mixed case words' \
   -u 'URL here' --without-headword --case-sensitive

# --index-data-separator '|' --index-keep-orig
dictfmt_diff_opts '1' indexdata \
   --index-data-separator '|' --index-keep-orig --headword-separator '/' \
   -c5 -s 'index/data test' \
   -u 'URL here' --without-headword

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

absolute_path(){
    if printf '%s\n' "$1" | grep -q ^/; then
	printf '%s\n' "$1"
    else
	printf '%s\n' `pwd`/"$1"
    fi
}

generate_commands (){
    test $# -eq 2

    conf_file="$1"
    shift

    strats="$1"
    shift

    @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_base (){
    test $# -eq 5

    etadir=`absolute_path $1` # etalon directory
    shift

    confname="$1"
    shift

    dbname="$1"
    shift

    noorder="$1"
    shift

    strats="$1"
    shift

    #
    conf_file=_testdb.$confname.conf
    m4 -DCURR_DIR=$etadir @srcdir@/testdb.$confname.conf >$conf_file

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

    in_file=_dictd_${dbname}_in.txt

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

    #
    $DICTD -c $conf_file -i --stdin2stdout < "$in_file" |
    @AWK@ '/^2[25]0 / { print $1 "\r"; next; } {print}' \
	| $noorder > _dictd_${dbname}_res.txt

    #
    cmp_2_files \
	$etadir/dictd_${dbname}_res.expected.txt \
	_dictd_${dbname}_res.txt
}

dictd_test_inetd (){
    test $# -eq 4

    etadir="$1"
    shift

    confname="$1"
    shift

    txtname="$1"
    shift

    strats="$1"
    shift

    dictd_test_inetd_base "$etadir" "$confname" "$txtname" "cat $@" "$strats"
}

dictd_test_inetd_noorder (){
    test $# -eq 4

    etadir="$1"
    shift

    confname="$1"
    shift

    txtname="$1"
    shift

    strats="$1"
    shift

    dictd_test_inetd_base "$etadir" "$confname" "$txtname" 'env LC_ALL=C sort' "$strats"
}

dictd_test_inetd2_base (){
    test $# -eq 4

    etadir="$1"
    shift

    confname="$1"
    shift

    txtname="$1"
    shift

    noorder="$1"
    shift

    #
    conf_file=_testdb.$confname.conf
    m4 -DCURR_DIR=$etadir @srcdir@/testdb.$confname.conf >$conf_file

    #
    in_file=$etadir/input.${txtname}.txt

    #
    $DICTD -c $conf_file -i --stdin2stdout < "$in_file" |
    @AWK@ '/^2[25]0 / { print $1 "\r"; next; } {print}' \
	| $noorder > _dictd_${txtname}_res.txt

    #
    cmp_2_files \
	$etadir/dictd_${txtname}_res.expected.txt \
	_dictd_${txtname}_res.txt
}

dictd_test_inetd2 (){
    test $# -eq 3

    etadir="$1"
    shift

    confname="$1"
    shift

    txtname="$1"
    shift

    dictd_test_inetd2_base \
	"$etadir" "$confname" "$txtname" \
	cat "$@"
}

dictd_test_inetd2_noorder (){
    test $# -eq 3

    etadir="$1"
    shift

    confname="$1"
    shift

    txtname="$1"
    shift

    dictd_test_inetd2_base \
	$etadir $confname $txtname \
	'env LC_ALL=C sort' "$@"
}

dictd_test_daemon (){
    test $# -eq 3

    etadir=`absolute_path $1` # etalon directory
    shift

    dbname="$1"
    shift

    expect_name="$1"
    shift

    conf_name=`pwd`/"_testdb.${dbname}.conf"

    $DICTD -c $conf_name --pid-file _dictd.pid 2>&1
    sleep 1
    dictd_pid=`cat _dictd.pid`

    trap "kill -TERM $dictd_pid" 0

    $DICT -h localhost -p 26281 -DS > _dict_nodetach_test.out 2>&1

    cmp_2_files "$etadir/$expect_name" _dict_nodetach_test.out
}

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

dictd_test_inetd @srcdir@ hello_1 hello_1 exact
printf 'hello_1 passed\n'
dictd_test_inetd @srcdir@ hello_2 hello_2 exact
printf 'hello_2 passed\n'
dictd_test_inetd @srcdir@ hello_3 hello_3 exact
printf 'hello_3 passed\n'
dictd_test_inetd @srcdir@ hello_4 hello_4 exact
printf 'hello_4 passed\n'
dictd_test_inetd @srcdir@ hello_5 hello_5 exact
printf 'hello_5 passed\n'

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

dictd_test_daemon @srcdir@ cyrillic_1 cyrillic_nodetach_expect.out
printf 'cyrillic_1 nodetach nofork passed\n'

#LC_ALL=C sort dictd_cyrillic_1_res.expected.txt \
#            > dictd_cyrillic_2_res.expected.txt
#$DICTFMT_I2W --utf8 --silent db.expect.cyrillic_1.index \
#   > db.expect.cyrillic_1.index_word
#$DICTFMT_I2S --utf8 --silent db.expect.cyrillic_1.index \
#   > db.expect.cyrillic_1.index_suffix
#dictd_test_inetd_noorder cyrillic_2 cyrillic_2 exact word lev suffix prefix substring
#printf 'cyrillic_2 passed\n'

dictd_test_inetd2 @srcdir@ cyrillic_1 cyrillic_3
printf 'cyrillic_3 passed\n'

LC_ALL=C sort @srcdir@/dictd_cyrillic_3_res.expected.txt \
            > dictd_cyrillic_4_res.expected.txt
cp @srcdir@/input.cyrillic_3.txt input.cyrillic_4.txt
if test @srcdir@ != .; then
   cp @srcdir@/db.expect.cyrillic_1.index .
fi
if test @srcdir@ != .; then
   cp @srcdir@/db.expect.cyrillic_1.dict .
fi
$DICTFMT_I2S --utf8 --silent @srcdir@/db.expect.cyrillic_1.index \
   > db.expect.cyrillic_1.index_suffix
dictd_test_inetd2_noorder `pwd` cyrillic_4 cyrillic_4
printf 'cyrillic_4 passed\n'

dictd_test_inetd2 @srcdir@ mixedcase_1 mixedcase_1
printf 'mixedcase_1 passed\n'

dictd_test_inetd2 @srcdir@ indexdata_1 indexdata_1
printf 'indexdata_1 passed\n'

dictd_test_inetd2 @srcdir@ limits_1 limits_1
printf 'limits_1 passed\n'

dictd_test_inetd2 @srcdir@ defstrat_1 defstrat_1
printf 'defstrat_1 passed\n'
dictd_test_inetd2 @srcdir@ defstrat_2 defstrat_2
printf 'defstrat_2 passed\n'
