#!/bin/sh

set -e

SDCV="$1"
TEST_DIR="$2"

unset SDCV_PAGER

# Work on a copy of the dictionary in a temporary directory so the
# source tree stays clean and cache files land in a known place.
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT INT QUIT TERM
DICT_DIR="$TMP_DIR/stardict-test_synonyms-2.4.2"
mkdir -p "$DICT_DIR"
for f in test.ifo test.idx test.dict.dz test.syn; do
    cp "$TEST_DIR/stardict-test_synonyms-2.4.2/$f" "$DICT_DIR"
done
XDG_CACHE_HOME="$TMP_DIR/cache"
export XDG_CACHE_HOME

test_word() {
    WORD=$1
    RES=$($SDCV -n --data-dir "$TMP_DIR" -u "Test synonyms" $WORD | grep result)
    if [ "result of test" != "$RES" ]; then
	echo "synonym for $WORD should be 'result of test' but was '$RES'"
	exit 1
    fi
}

# The offset table is read straight out of the cache mapping, so the header
# has to keep it 4-byte aligned: 30 bytes of magic string, 2 of padding, then
# the 4-byte magic word, ending at byte 36.
CACHE_HEADER_SIZE=36

check_cache_layout() {
    FILE=$1
    GOT=$(head -c 30 "$FILE")
    if [ "StarDict's Cache, Version: 0.3" != "$GOT" ]; then
	echo "$FILE should start with the 0.3 cache magic but was '$GOT'"
	exit 1
    fi
    SIZE=$(wc -c < "$FILE" | tr -d ' ')
    if [ "$(( (SIZE - CACHE_HEADER_SIZE) % 4 ))" -ne 0 ]; then
	echo "$FILE holds $SIZE bytes, so its offset table is not 4-byte aligned"
	exit 1
    fi
}

SYN_CACHE="$DICT_DIR/test.syn.oft"

# First lookup scans the .syn file and saves an offset cache next to it
# (or in the user cache dir), like the .idx.oft cache.
test_word foo
if [ ! -f "$SYN_CACHE" ] && [ ! -f "$XDG_CACHE_HOME/sdcv/test.syn.oft" ]; then
    echo "no .syn.oft cache was created after a lookup"
    exit 1
fi
[ -f "$SYN_CACHE" ] || SYN_CACHE="$XDG_CACHE_HOME/sdcv/test.syn.oft"

IDX_CACHE="$DICT_DIR/test.idx.oft"
[ -f "$IDX_CACHE" ] || IDX_CACHE="$XDG_CACHE_HOME/sdcv/test.idx.oft"

# Both caches must be written in the aligned 0.3 layout.
check_cache_layout "$SYN_CACHE"
check_cache_layout "$IDX_CACHE"

# Later lookups load the cache instead of scanning; results must not change.
test_word foo
test_word bar
test_word test

# A cache with bogus content must be ignored (and rebuilt), not trusted.
printf 'garbage' > "$SYN_CACHE"
touch "$SYN_CACHE"
test_word foo

# A cache with a valid magic but truncated offset table must be ignored too.
head -c "$(( CACHE_HEADER_SIZE + 4 ))" "$SYN_CACHE" > "$SYN_CACHE.tmp"
mv "$SYN_CACHE.tmp" "$SYN_CACHE"
touch "$SYN_CACHE"
test_word bar

# So must one that holds more offsets than the .syn has entries.
cat "$SYN_CACHE" > "$SYN_CACHE.tmp"
printf '\0\0\0\0' >> "$SYN_CACHE.tmp"
mv "$SYN_CACHE.tmp" "$SYN_CACHE"
touch "$SYN_CACHE"
test_word bar

# A cache in the older 0.2 layout - same magic word and table, but without the
# padding that aligns the table - must be rejected and rewritten as 0.3.
test_word foo
printf "StarDict's Cache, Version: 0.2" > "$SYN_CACHE.tmp"
# everything from the magic word on, in whatever byte order this host wrote it
tail -c "+$(( CACHE_HEADER_SIZE - 4 + 1 ))" "$SYN_CACHE" >> "$SYN_CACHE.tmp"
mv "$SYN_CACHE.tmp" "$SYN_CACHE"
touch "$SYN_CACHE"
test_word bar
check_cache_layout "$SYN_CACHE"

# A cache older than the .syn file is stale and must be rebuilt. Keep the old
# file open to verify that rebuilding replaces it instead of truncating it.
exec 3< "$SYN_CACHE"
OLD_INODE=$(ls -i "$SYN_CACHE" | awk '{print $1}')
touch -t 200001010000 "$SYN_CACHE"
test_word test
NEW_INODE=$(ls -i "$SYN_CACHE" | awk '{print $1}')
exec 3<&-
if [ "$OLD_INODE" = "$NEW_INODE" ]; then
    echo "stale cache was overwritten in place instead of replaced"
    exit 1
fi

exit 0
