#!/bin/sh
# tests with rpm installations (--justdb is used)

. ./sh/lib/setup
. ./sh/lib/repo-setup
. ./sh/lib/solver-setup
. ./sh/lib/rpm-setup

ORIGREPO=
DESTINATION_REPO=
DEPENDENCY_SOLVER=3

setUp() {
    rpm_up
    # provide /bin/sh auto requirement
    rpm_build_installed sh -p /bin/sh
}

tearDown() {
    rpm_down
}

build() {
    build_package $REPO $@
}

build_installed() {
    rpm_build_installed $@
}

try_install() {
  rpm_install "$@"
}

function testRPMdbcache() {
    build foo

    local op=""
    is_verbose_mode && op="-v"

    msgn "create rpmdb cache with dirindex"
    $RAW_POLDEK -Oauto_directory_dependencies=y --root $DESTINATION_REPO $op --cmd reload >/dev/null
    local rpmdb_cachedirname="$(echo $DESTINATION_REPO | sed -e 's|^/||' -e 's|/|.|g').var.lib.rpm"
    local suffix="rpmdbcache$(echo $DESTINATION_REPO | sed 's|/|.|g').var.lib.rpm"

    # check presence of dbcache
    for i in zst md zst.md5; do
        local name="packages.$suffix.$i"
        [ -f "$CACHEDIR/$name" ] || fail "missing expected $name in $CACHEDIR"
    done

    # check presence of dbcache's dirindex
    for i in tndb tndb.md5; do
        local name="dirindex.$suffix.$i"
        [ -f "$CACHEDIR/$name" ] || fail "missing expected $name in $CACHEDIR"
    done

    local dirindex_path="$CACHEDIR/$rpmdb_cachedirname/dirindex.rpmdb.tndb"
    local md5=$(md5sum $dirindex_path | cut -f1 -d' ')


    # will die (we're in test mode) if dirindex is missing
    $RAW_POLDEK -Oauto_directory_dependencies=y --st dir -s $REPO --root $DESTINATION_REPO $op --cmd ls -u
    assertEquals "died" "$?" "0"
    msgn "install a"
    local md=$(md5sum $dirindex_path | cut -f1 -d' ')
    assertEquals "dirindex changed" "$md5" "$md"

    build a "1-1"
    sleep 0.8 # mtime based cache

    try_install a "a-1-1" ""

    msgn "update dirindex"
    # should load rpmdb and update dirindex
    $RAW_POLDEK -Oauto_directory_dependencies=y --st dir -s $REPO --root $DESTINATION_REPO $op --cmd ls -u

    local md=$(md5sum $dirindex_path | cut -f1 -d' ')
    if [ "$md5" = "$md" ]; then
        fail "$dirindex_path: dirindex not changed ($md5)"
    fi
}

# e.g. openssl multiple instances
testUpgradeMultipleInstalled() {
    msgn "Preparing repositories..."
    build_installed a 0.9.8c-1
    build_installed a 1.0.0o-1
    build_installed a 1.1.1j-1

    build a 1.1.1k-1
    try_install a "a-1.1.1k-1" "a-1.1.1j-1"
}

testUpgradeMultipleInstalledWithDeps() {
    msgn "Preparing repositories..."
    build_installed a 0.9.8c-1
    build_installed a 1.0.0o-1
    build_installed a 1.1.1j-1

    for i in b10 c10 d10; do
        build_installed $i 0.1-0.1 -r "'a = 1.0.0o'"
    done

# TODO
#    for i in b11 c11; do
#        build_installed $i 1.0-1 -r "'a = 1.1'"
#    done

    build a 1.1.1k-1
    try_install a "a-1.1.1k-1" "a-1.1.1j-1"
}

# not implemented yet
xtestUpgradeMultipleInstalledIndir() {
    msgn "Preparing repositories..."
    build_installed a 1.0-1
    build_installed a 1.1-1

    build a 1.2-1
    build b 1-1 -r "'a = 1.2'"

    try_install b-1-1 "a-1.2" "a-1.1-1"
}

. ./sh/lib/shunit2
