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

set -o pipefail

top="$(WVPASS pwd)" || exit $?
tmpdir="$(WVPASS wvmktempdir)" || exit $?
export BUP_DIR="$tmpdir/bup"

bup() { "$top/bup" "$@"; }

validate-local-and-remote-restore()
{
    local src="$1" dest="$2" cmp_src="$3" cmp_dest="$4"
    force-delete "$dest"
    WVPASS bup restore -C "$dest" "$src"
    WVPASS "$top/dev/compare-trees" "$cmp_src" "$cmp_dest"
    force-delete "$dest"
    WVPASS bup restore -r "-:$BUP_DIR" -C "$dest" "$src"
    WVPASS "$top/dev/compare-trees" "$cmp_src" "$cmp_dest"
}


WVPASS cd "$tmpdir"

WVSTART "init"
WVPASS bup init
# Be independent of git defaults or a configured defaultBranch
git --git-dir "$BUP_DIR" symbolic-ref HEAD refs/heads/main
D=bupdata.tmp
WVPASS force-delete $D
WVPASS mkdir $D
WVPASS touch $D/a
WVPASS bup random 128k >$D/b
WVPASS mkdir $D/d $D/d/e
WVPASS bup random 512 >$D/f
WVPASS touch $D/d/z
WVPASS touch $D/d/z
(cd "$D/d" && WVPASS ln -s z z-link)
WVPASS bup index -vv $D
WVPASS bup save -t $D


WVSTART 'save (argument validation)'
WVFAIL err-to log bup save
WVPASS grep -E '^error: use one or more of -t, -c, -n' log
WVFAIL err-to log bup save -n main
WVPASS grep -E '^error: no filenames given' log
WVFAIL err-to log bup save -n "x y" src
WVPASS grep -E "^error: 'x y' is not a valid branch name" log
WVFAIL err-to log bup save -n main --date never src
WVPASS grep -E '^error: --date never is not a float' log
WVFAIL err-to log bup save -n main --smaller nope src
WVPASS grep -E "^error: invalid --smaller \(can't parse 'nope' as a number\)" log
WVFAIL err-to log bup save -n main --bwlimit nope src
WVPASS grep -E "^error: invalid --bwlimit \(can't parse 'nope' as a number\)" log
WVFAIL err-to log bup save -n main --strip-path x --strip src
WVPASS grep -E '^error: --strip is incompatible with --strip-path' log
WVFAIL err-to log bup save -n main --graft x=y --strip src
WVPASS grep -E '^error: --strip is incompatible with --graft' log
WVFAIL err-to log bup save -n main --graft x=y --strip-path x src
WVPASS grep -E '^error: --strip-path is incompatible with --graft' log
WVFAIL err-to log bup save -n main --graft x/y src
WVPASS grep -E '^error: a graft point must be of the form old_path=new_path' log

WVPASS git --git-dir bup config bup.split.trees 42
WVFAIL err-to log bup save -n main /dev/null
WVPASS grep -E '^error: got invalid bool value 42 from git config' log
WVPASS git --git-dir bup config --unset bup.split.trees


WVSTART 'restore (argument validation)'
WVFAIL err-to log bup restore -C /dev/null
WVPASS grep -E '^error: must specify at least one path to restore' log


WVSTART "restore"
WVPASS force-delete buprestore.tmp
WVFAIL bup restore boink
WVPASS touch "$tmpdir/$D/$D"
WVPASS bup index -u "$tmpdir/$D"
WVPASS bup save -n main /
WVPASS bup restore -C buprestore.tmp "/main/latest/$tmpdir/$D"
WVPASSEQ "$(ls buprestore.tmp)" "bupdata.tmp"
WVPASS force-delete buprestore.tmp
WVPASS bup restore -vv -C buprestore.tmp "/main/latest/$tmpdir/$D/"
WVPASS touch $D/non-existent-file buprestore.tmp/non-existent-file # else diff fails
WVPASS diff -ur $D/ buprestore.tmp/
WVPASS force-delete buprestore.tmp
WVPASS echo -n "" | WVPASS bup split -n split_empty_string.tmp
WVPASS bup restore -C buprestore.tmp split_empty_string.tmp/latest/
WVPASSEQ "$(cat buprestore.tmp/data)" ""


(
    tmp=testrestore.tmp
    WVPASS force-delete $tmp
    WVPASS mkdir $tmp
    export BUP_DIR="$(pwd)/$tmp/bup"
    WVPASS WVPASS bup init
    WVPASS mkdir -p $tmp/src/x/y/z
    WVPASS bup random 8k > $tmp/src/x/y/random-1
    WVPASS bup random 8k > $tmp/src/x/y/z/random-2
    WVPASS bup index -u $tmp/src
    WVPASS bup save --strip -n foo $tmp/src

    WVSTART "restore /foo/latest"
    validate-local-and-remote-restore \
        /foo/latest  "$tmp/restore" \
        "$tmp/src/" "$tmp/restore/latest/"

    WVSTART "restore /foo/latest/."
    WVPASS force-delete "$tmp/restore"
    validate-local-and-remote-restore \
        /foo/latest/.  "$tmp"/restore \
        "$tmp"/src/ "$tmp"/restore

    WVSTART "restore /foo/latest/x"
    WVPASS force-delete "$tmp/restore"
    validate-local-and-remote-restore \
        /foo/latest/x  "$tmp"/restore \
        "$tmp"/src/x/ "$tmp"/restore/x/

    WVSTART "restore /foo/latest/x/"
    WVPASS force-delete "$tmp/restore"  
    WVPASS bup restore -C "$tmp"/restore /foo/latest/x/
    for x in "$tmp"/src/x/*; do
        WVPASS "$top/dev/compare-trees" "$x/" "$tmp/restore/$(basename $x)"
    done
    WVPASS force-delete "$tmp/restore"  
    WVPASS bup restore -r "-:$BUP_DIR" -C "$tmp"/restore /foo/latest/x/
    for x in "$tmp"/src/x/*; do
        WVPASS "$top/dev/compare-trees" "$x/" "$tmp/restore/$(basename $x)"
    done

    WVSTART "restore /foo/latest/x/."
    WVPASS force-delete "$tmp/restore"
    validate-local-and-remote-restore \
        /foo/latest/x/.  "$tmp"/restore \
        "$tmp"/src/x/ "$tmp"/restore/
) || exit $?


WVSTART "save (no index)"
(
    tmp=save-no-index.tmp
    WVPASS force-delete $tmp
    WVPASS mkdir $tmp
    export BUP_DIR="$(WVPASS pwd)/$tmp/bup" || exit $?
    WVPASS bup init
    WVFAIL bup save -n nothing /
    WVPASS rm -r "$tmp"
) || exit $?


WVSTART "save disjoint top-level directories"
(
    # Resolve any symlinks involving the top top-level dirs.
    real_pwd="$(WVPASS resolve-parent .)" || exit $?
    real_tmp="$(WVPASS resolve-parent /tmp/.)" || exit $?
    pwd_top="$(echo $real_pwd | WVPASS awk -F "/" '{print $2}')" || exit $?
    tmp_top="$(echo $real_tmp | WVPASS awk -F "/" '{print $2}')" || exit $?

    if [ "$pwd_top" = "$tmp_top" ]; then
        echo "(running from within /$tmp_top; skipping test)" 1>&2
        exit 0 # FIXME: allow intermixed WVSKIPs
    fi
    D=bupdata.tmp
    WVPASS force-delete $D
    WVPASS mkdir -p $D/x
    WVPASS date > $D/x/1
    tmpdir2="$(WVPASS mktemp -d $real_tmp/bup-test-XXXXXXX)" || exit $?
    cleanup() { WVPASS rm -r "$tmpdir2"; }
    WVPASS trap cleanup EXIT
    WVPASS date > "$tmpdir2/2"

    export BUP_DIR="$tmpdir/bup"
    WVPASS test -d bup
    WVPASS rm -rf bup
    WVPASS bup init
    WVPASS bup index -vu $(pwd)/$D/x "$tmpdir2"
    WVPASS bup save -t -n src $(pwd)/$D/x "$tmpdir2"

    # For now, assume that "ls -a" and "sort" use the same order.
    actual="$(WVPASS bup ls -AF src/latest)" || exit $?
    expected="$(echo -e "$pwd_top/\n$tmp_top/" | LC_ALL=C sort)" || exit $?
    WVPASSEQ "$actual" "$expected"
) || exit $?


if ! test "$BUP_TEST_OTHER_BUP"; then
    WVSKIP 'no BUP_TEST_OTHER_BUP'
else
    WVSTART "save/restore compatibility between ./bup and $BUP_TEST_OTHER_BUP"
    WVPASSEQ '' "$BUP_TEST_SSH_BUP_PATH"
    force-delete src repo restore
    export BUP_DIR="$(pwd)/repo"
    WVPASS bup init
    WVPASS bup index "$top/test/sampledata"
    export BUP_TEST_SSH_BUP_PATH="$BUP_TEST_OTHER_BUP"
    WVPASS bup save -n save --strip "$top/test/sampledata"
    WVPASS bup restore -r "-:$BUP_DIR" -C restore save/latest
    unset BUP_TEST_SSH_BUP_PATH
    WVPASS "$top/dev/compare-trees" "$top/test/sampledata/" restore/latest
    force-delete src repo restore
fi


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