#!/usr/bin/env bash
. wvtest.sh
. wvtest-bup.sh
. dev/lib.sh

set -o pipefail

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

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

bup() { "$top/bup" "$@"; }
sha1sum() { "$top/dev/checksum" -t sha1 "$@"; }

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
WVPASS bup index $D
WVPASS bup save -t $D


WVSTART "bloom"
WVPASS bup bloom -c $(ls -1 "$BUP_DIR"/objects/pack/*.idx|head -n1)
WVPASS rm "$BUP_DIR"/objects/pack/bup.bloom
WVPASS bup bloom -k 4
WVPASS bup bloom -c $(ls -1 "$BUP_DIR"/objects/pack/*.idx|head -n1)
WVPASS bup bloom -d "$BUP_DIR"/objects/pack --ruin --force
WVFAIL bup bloom -c $(ls -1 "$BUP_DIR"/objects/pack/*.idx|head -n1)
WVPASS bup bloom --force -k 5
WVPASS bup bloom -c $(ls -1 "$BUP_DIR"/objects/pack/*.idx|head -n1)


WVSTART "memtest"
WVPASS bup memtest -c1 -n100
WVPASS bup memtest -c1 -n100 --existing


WVSTART "save/git-fsck"
(
    WVPASS cd "$BUP_DIR"
    #git repack -Ad
    #git prune
    WVPASS bup random 4k | WVPASS bup split -b
    (WVPASS cd "$top/test/sampledata" && WVPASS bup save -vvn main /) || exit $?
    result="$(LC_ALL=C git fsck --full --strict 2>&1)" || exit $?
    n=$(echo "$result" |
        WVFAIL egrep -v 'dangling (commit|tree|blob)' |
        WVPASS tee -a /dev/stderr |
        WVPASS wc -l) || exit $?
    WVPASS [ "$n" -eq 0 ]
) || exit $?


WVSTART "pack name and idx same as git"
(
    # reuse packs from previous test
    WVPASS cd "$BUP_DIR"/objects/pack/
    WVPASS ls *.pack
    for pack in *.pack ; do
       bup_idx_sha=$(sha1sum $(basename $pack .pack).idx) || exit $?
       gitname=$(git index-pack $pack) || exit $?
       # make sure we named it correctly (like git)
       WVPASSEQ pack-$gitname.pack $pack
       # make sure we wrote the index correctly
       git_idx_sha=$(sha1sum $(basename $pack .pack).idx) || exit $?
       WVPASSEQ "$bup_idx_sha" "$git_idx_sha"
    done
) || exit $?


WVSTART "ftp"
WVPASS bup ftp "cat /main/latest/$tmpdir/$D/b" >$D/b.new
WVPASS bup ftp "cat /main/latest/$tmpdir/$D/f" >$D/f.new
WVPASS bup ftp "cat /main/latest/$tmpdir/$D/f"{,} >$D/f2.new
WVPASS bup ftp "cat /main/latest/$tmpdir/$D/a" >$D/a.new
WVPASSEQ "$(sha1sum <$D/b)" "$(sha1sum <$D/b.new)"
WVPASSEQ "$(sha1sum <$D/f)" "$(sha1sum <$D/f.new)"
WVPASSEQ "$(cat $D/f.new{,} | sha1sum)" "$(sha1sum <$D/f2.new)"
WVPASSEQ "$(sha1sum <$D/a)" "$(sha1sum <$D/a.new)"


WVSTART "tag"
WVFAIL bup tag -d v0.n 2>/dev/null
WVFAIL bup tag v0.n non-existant 2>/dev/null
WVPASSEQ "$(bup tag)" ""
WVPASS bup tag v0.1 main
WVPASSEQ "$(bup tag)" "v0.1"
WVFAIL bup tag v0.1 main
WVPASS bup tag -f v0.1 main
WVPASS bup tag -d v0.1
WVPASS bup tag -f -d v0.1
WVFAIL bup tag -d v0.1


WVSTART "indexfile"
D=indexfile.tmp
INDEXFILE=tmpindexfile.tmp
WVPASS rm -f $INDEXFILE
WVPASS force-delete $D
WVPASS mkdir $D
export BUP_DIR="$D/.bup"
WVPASS bup init
WVPASS touch $D/a
WVPASS touch $D/b
WVPASS mkdir $D/c
WVPASS bup index -ux $D
WVPASS bup save --strip -n bupdir $D
WVPASSEQ "$(bup ls -F bupdir/latest/)" "a
b
c/"
WVPASS bup index -f $INDEXFILE --exclude=$D/c -ux $D
WVPASS bup save --strip -n indexfile -f $INDEXFILE $D
WVPASSEQ "$(bup ls indexfile/latest/)" "a
b"


WVSTART "import-rsnapshot"
D=rsnapshot.tmp
export BUP_DIR="$tmpdir/$D/.bup"
WVPASS force-delete $D
WVPASS mkdir $D
WVPASS bup init
WVPASS mkdir -p $D/hourly.0/buptest/a
WVPASS touch $D/hourly.0/buptest/a/b
WVPASS mkdir -p $D/hourly.0/buptest/c/d
WVPASS touch $D/hourly.0/buptest/c/d/e
WVPASS true
WVPASS bup import-rsnapshot $D/
WVPASSEQ "$(bup ls -F buptest/latest/)" "a/
c/"


WVSTART features
expect_py_ver=$(LC_CTYPE=C "$top/dev/python" \
                        -c 'import platform; print(platform.python_version())') \
    || exit $?
actual_py_ver=$(bup features | grep Python: | sed -Ee 's/ +Python: //') || exit $?
WVPASSEQ "$expect_py_ver" "$actual_py_ver"


WVSTART id-other-than
result=$("$top/dev/id-other-than" --user 0) ||  exit $?
WVPASS echo "$result" | WVPASS grep -qE '.*:[0-9]+$'
result=$("$top/dev/id-other-than" --group 0) ||  exit $?
WVPASS echo "$result" | WVPASS grep -qE '.*:[0-9]+$'


WVPASS rm -rf "$tmpdir"
