#!/bin/sh
# $Id$

. ./sh/lib/setup
. ./sh/lib/repo-setup
. ./sh/lib/compr-setup

mkidx() {
    time1s_barrier
    touch $REPO
    time1s_stamp
    $POLDEK_NOCONF "$@" -s $REPO --mkidx --mt pndir || fail "mkidx failed"
}

ls_expect() {
    local expected=$1
    is_verbose_mode && $POLDEK_RAW_UP -q -Oautoupa=n -Osource="test,type=pndir $REPOURL" -n test --cmd ls
    n=$($POLDEK_RAW_UP -q -Oautoupa=n -Osource="test,type=pndir $REPOURL" -n test --cmd ls | grep -P '^\w+-\d+-\d+\.\w+$' | wc -l)

    assertEquals "ls: invalid number of packages found (expected $expected, got $n)" "$n" "$expected"
    [ "$n" == "$expected" ]
}

up() {
    $POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" --up || fail "up failed"
    if [ $# -eq 1 ]; then
        local expected_packages_after=$1
        [ -n "$expected_packages_after" ] && ls_expect $expected_packages_after
    fi
}

up_debug() {
    echo $POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" --up || fail "up failed"
}

testUpdate()
{
    local out rc

    POLDEK_TESTING_DENIED_FILES="packages.ndir.diff.toc"

    rm -rf $REPO/*.*
    # Create repo and generate some diffs
    msg "\n## Creating empty index"
    indexfile="$REPO/packages.ndir$COMPR_EXT"
    mkidx --st dir
    die_if_invalid_index $indexfile 0

    msg "\n## up #0"
    up

    POLDEK_TESTING_DENIED_FILES="packages.ndir$COMPR_EXT"
    msg "\n## Changing repo #1"
    add_package_to_repo
    mkidx
    die_if_invalid_index $indexfile 1
    die_if_invalid_index "$REPO/packages.i/packages.ndir.diff.toc$COMPR_EXT" 1
    msg "\n## up #1"
    up 1

    msg "\n## Changing repo #2"
    remove_package_from_repo
    mkidx
    die_if_invalid_index $indexfile 0
    die_if_invalid_index "$REPO/packages.i/packages.ndir.diff.toc$COMPR_EXT" 2

    msg "\n## up #2"
    up 0

    msg "\n## Changing repo #3"
    add_package_to_repo
    add_package_to_repo
    mkidx
    die_if_invalid_index $indexfile 2
    die_if_invalid_index "$REPO/packages.i/packages.ndir.diff.toc$COMPR_EXT" 3

    msg "\n## up #3"
    up 2

    # <https://bugs.pld-linux.org/show_bug.cgi?id=28>
    msg "\n## Resetting repo"
    rm -rf $REPO/packages.*
    add_package_to_repo
    mkidx
    msg "\n## up (reset)"

    POLDEK_TESTING_DENIED_FILES="packages.ndir.diff.toc"
    #$POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" --up >/dev/null
    #[ $? -eq 0 ] && fail "up ok" # should fail

    $POLDEK_UP -Oautoupa=y -Osource="test,type=pndir $REPOURL" --up || fail "up(a) failed"
    POLDEK_TESTING_DENIED_FILES=""

    # upa
    msg "\n## Changing repo #4"
    add_package_to_repo
    mkidx
    msg "\n## up #4"

    POLDEK_TESTING_DENIED_FILES="packages.ndir$COMPR_EXT"
    $POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" --up || fail "up failed"

    POLDEK_TESTING_DENIED_FILES="$COMPR_EXT" # should be up to date
    $POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" --up || fail "up failed"
    $POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" --upa || fail "up failed"
    ls_expect 4

    # autoupa on missing diff
    msg "\n## Changing repo #5 (missing diff file and autoupa)"
    add_package_to_repo
    mkidx
    msg "\n## up #5"

    POLDEK_TESTING_DENIED_FILES=""
    # the index files, not their dscr siblings and not the list: those are
    # written after the index and are newer
    lastdiff=$(ls -tr $REPO/packages.i/packages.ndir.[0-9]* | tail -1)
    [ -z "$lastdiff" ] && fail "no last diff?"
    msg "lastdiff $lastdiff"
    rm -f "$lastdiff"
    out=$($POLDEK_UP -Oautoupa=y -Osource="test,type=pndir $REPOURL" --up 2>&1)
    rc=$?
    # the output is captured to be checked, and printed, so the error the
    # message below is about stays in the log
    echo "$out"
    assertEquals "up failed: $out" 0 "$rc"
    echo "$out" | grep -q 'no such file' || fail "no error for the missing diff: $out"
    ls_expect 5

    msg "\n## stubindex"
    index=$(find $CACHEDIR -name packages.ndir$COMPR_EXT)
    REPO_CACHE_DIR=$(dirname $index)
    stubindex="$REPO_CACHE_DIR/stubindex.pndir.zst"
    if [ ! -f "$stubindex" ]; then
        fail "no stubindex $stubindex!"
    else
        assertEquals $(zstdcat "$stubindex" | grep -vc "^%") "5"
        rm -f $stubindex
        if ls_expect 5; then
            assertEquals $(zstdcat "$stubindex" | grep -vc "^%") "5" # recreated at load
        fi

    fi

}

# a diff the list names that is gone behind one the client has applied: half
# way through, like with no matching patch at all, only the whole index gets
# the client back in sync (autoupa)
testUpMissingDiffAfterPatch()
{
    local out rc diff

    rm -rf $REPO/*.*
    cachedir_clean
    mkidx
    $POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" --up || fail "up failed"

    # two diffs to apply: the client applies the first, the second is denied
    add_package_to_repo
    mkidx
    add_package_to_repo
    mkidx

    # the index files, not their dscr siblings and not the list
    diff=$(basename $(ls -tr $REPO/packages.i/packages.ndir.[0-9]* | tail -1))
    [ -n "$diff" ] || fail "no diff to deny"

    POLDEK_TESTING_DENIED_FILES="$diff"
    out=$($POLDEK_UP -Oautoupa=y -Osource="test,type=pndir $REPOURL" --up 2>&1)
    rc=$?
    POLDEK_TESTING_DENIED_FILES=""
    assertEquals "up failed: $out" 0 "$rc"
    echo "$out" | grep -q 'desynchronized index, trying to update whole index' || \
        fail "no whole index update: $out"
    ls_expect 2
}

# a list of diffs that is not there (a lost file) is no incremental way to
# the index: the client takes the whole one instead of failing
testUpNoDiffList()
{
    local out rc toc="$REPO/packages.i/packages.ndir.diff.toc$COMPR_EXT"

    rm -rf $REPO/*.*
    cachedir_clean
    mkidx
    $POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" --up || fail "up failed"

    add_package_to_repo
    mkidx
    [ -f $toc ] || fail "no list of diffs"
    rm -f $toc

    out=$($POLDEK_UP -Oautoupa=y -Osource="test,type=pndir $REPOURL" --up 2>&1)
    rc=$?
    assertEquals "up failed: $out" 0 "$rc"
    echo "$out" | grep -q 'trying to update whole index' || \
        fail "no whole index update: $out"
    ls_expect 1
}

# --upa of a changed repository fetches the whole index again
testUpaChangedRepoIsNotUpToDate()
{
    local out n

    rm -rf $REPO/*.*
    cachedir_clean
    mkidx
    $POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" --upa || fail "upa failed"

    sleep 1
    add_package_to_repo
    mkidx
    out=$($POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" --upa 2>&1) || fail "upa failed"
    n=$(echo "$out" | grep -c 'is up to date')
    assertEquals "--upa said a changed repository is up to date: $out" "0" "$n"
    ls_expect 1
}

# "up NAME" selects sources like -n NAME does: a noautoup source is
# updated, a wildcard matches, and an unknown name is an error
testUpByName()
{
    local out rc

    rm -rf $REPO/*.*
    cachedir_clean
    mkidx

    out=$($POLDEK_UP -Oautoupa=n -Osource="test,type=pndir,noautoup $REPOURL" up test 2>&1)
    rc=$?
    assertEquals "up test failed: $out" 0 "$rc"
    echo "$out" | grep -Eq 'Retrieving|is up to date|updating' || \
        fail "up test did not update the noautoup source: $out"

    out=$($POLDEK_UP -Oautoupa=n -Osource="test,type=pndir,noautoup $REPOURL" up 'te*' 2>&1)
    rc=$?
    assertEquals "up te* failed: $out" 0 "$rc"
    echo "$out" | grep -Eq 'Retrieving|is up to date|updating' || \
        fail "up te* did not match the source: $out"

    out=$($POLDEK_UP -Oautoupa=n -Osource="test,type=pndir $REPOURL" up nosuch 2>&1)
    rc=$?
    [ "$rc" -ne 0 ] || fail "up nosuch succeeded: $out"
    echo "$out" | grep -q 'no such source' || \
        fail "no error for an unknown source: $out"
}

. ./sh/lib/shunit2
