#!/usr/bin/env bash
. ./wvtest-bup.sh || exit $?
. ./dev/lib.sh || exit $?

set -o pipefail

top="$(WVPASS pwd)" || exit $?
tmpdir="$(WVPASS wvmktempdir)" || exit $?

export BUP_DIR="$tmpdir/bup"
export GIT_DIR="$tmpdir/bup"

bup() { "$top/bup" "$@"; }
compare-trees() { "$top/dev/compare-trees" "$@"; }

WVPASS bup init
WVPASS cd "$tmpdir"

WVPASS mkdir home
export HOME="$tmpdir/home"

WVSTART "index/save"
WVPASS mkdir src src/foo
WVPASS date > src/bar
WVPASS bup random 1k > src/baz
WVPASS bup on - index src
WVPASS bup on - save -ctn src src > get.log
WVPASSEQ $(WVPASS cat get.log | WVPASS wc -l) 2
tree_id=$(WVPASS awk 'FNR == 1' get.log) || exit $?
commit_id=$(WVPASS awk 'FNR == 2' get.log) || exit $?
WVPASS git ls-tree "$tree_id"
cat_id="$(WVPIPE git cat-file commit "$commit_id" | WVPASS head -1)" || exit $?
WVPASSEQ "tree $tree_id" "$cat_id"

WVSTART 'restore'
WVPASS bup restore -C restore "src/latest/$(pwd)/src/."
WVPASS compare-trees src/ restore/
WVPASS rm -r restore
WVPASS bup on - restore -C "$tmpdir/restore" "src/latest/$(pwd)/src/."
WVPASS compare-trees src/ restore/
WVPASS rm -r restore

WVSTART "split"
WVPASS bup on - split -ctn baz src/baz > get.log
tree_id=$(WVPASS awk 'FNR == 1' get.log) || exit $?
commit_id=$(WVPASS awk 'FNR == 2' get.log) || exit $?
WVPASS git ls-tree "$tree_id"
cat_id="$(WVPIPE git cat-file commit "$commit_id" | WVPASS head -1)" || exit $?
WVPASSEQ "tree $tree_id" "$cat_id"
WVPASS bup join baz > restore-baz
WVPASS cmp src/baz restore-baz

repo_id="id--$(WVPASS git config --file "$tmpdir"/bup/config bup.repo.id)"

WVSTART 'index-cache exists'
# there should be an index-cache now
for idx in "$tmpdir"/bup/objects/pack/*.idx ; do
    cachedidx="$HOME/.cache/bup/remote/$repo_id/$(basename "$idx")"
    WVPASS cmp "$idx" "$cachedidx"
done

# Legacy is based on hostname: by default via BUP_SERVER_REVERSE via
# on__server.py.
host="$(WVPASS "$top/dev/bup-python" -c 'import socket; print(socket.gethostname())')"
WVPASS "$top/dev/bup-python" -c '
import os, sys, bup.client
args = map(os.fsencode, sys.argv[1:])
print(os.fsdecode(bup.client._legacy_cache_id_for_host_path(*args)))' \
       \
       "$host" '' > legacy-id

legacy_id="$(< legacy-id)"

WVSTART 'legacy index-cache moves to ~/.cache'
WVPASS mkdir -p "$BUP_DIR/index-cache"
WVPASS mv "$HOME/.cache/bup/remote/$repo_id" "$BUP_DIR/index-cache/$legacy_id"
# Previously we'd forgotten to create the parents when moving; verify
# we do now
WVPASS rm -r "$HOME/.cache/bup"
WVPASS bup on - restore -C tmp-restore src/latest
# index cache should have been moved back to the new location
WVPASS test ! -e "$BUP_DIR/index-cache/$legacy_id"
for idx in "$tmpdir"/bup/objects/pack/*.idx ; do
    cachedidx="$HOME/.cache/bup/remote/$repo_id/$(basename "$idx")"
    WVPASS cmp "$idx" "$cachedidx"
done
WVPASS rm -rf tmp-restore

WVSTART 'index-cache respects XDG_CACHE_HOME'
WVPASS mkdir xdg-cache
export XDG_CACHE_HOME="$(pwd)/xdg-cache"
WVPASS bup on - split -ct src/baz
# Ensure the cache is a subset of objects/pack
for idx in xdg-cache/bup/remote/"$repo_id"/*.idx ; do
    repo_idx="bup/objects/pack/$(basename "$idx")"
    WVPASS cmp "$idx" "$repo_idx"
done

WVSTART "suggest packs"
# we need to write a large object here, otherwise the
# buffering in the local connection on the client will
# kill the test - if everything is buffered then we get
# the index suggestions only at the end when we finish,
# not inbetween, and then don't exercise the suspend
# pack writing code path ...
WVPASS seq 100000 > "$tmpdir/longfile"
WVPASS bup on - split -ctn large "$tmpdir/longfile"
WVPASS rm -rf "$XDG_CACHE_HOME"/bup/remote/
WVPASS bup --debug on - split -ctn large "$tmpdir/longfile"
idxs=("$XDG_CACHE_HOME"/bup/remote/*/*.idx)
WVPASS test -f "${idxs[0]}"

WVPASS cd "$top"
WVPASS rm -rf "$tmpdir"
