#!/bin/sh
# $Id$

# --mkidx tests
. ./sh/lib/setup
. ./sh/lib/repo-setup
. ./sh/lib/compr-setup
. ./sh/lib/rpm-setup

setUp() {
    #repo_up
    sudo_up
}

#  Index creation use cases:
Case_a="-s /foo                   =>  dir  -> default type"
Case_b="--st type -s /foo         =>  type -> default type"
#Case_c="--st type /foo --mt dtype =>  type -> dtype" # irrelevant, as pndir is only creatable format
#Case_d="-s /foo --mt dtype        =>  dir  -> dtype" # irrelevant, as pndir is only creatable format
#Case_e="-s /foo --mt dtype,dtype2 =>  dir  -> dtype" # irrelevant, as pndir is only creatable format
Case_f="-n foo                    =>  dir (or original type) -> foo's type"
Case_g="-s /foo --mkidx=<path>    =>  index written outside the repo"
Case_h="-n foo --mkidx=<path>     =>  the same, with an explicit index name"


testCreateDefaultType()
{
    msgn $Case_a
    $POLDEK_NOCONF -s $REPO --mkidx || fail "mkidx #1 failed"
    die_if_empty_index "$REPO/packages.ndir$COMPR_EXT"

    $POLDEK_NOCONF -s $REPO --mkidx=$TMPDIR || fail "mkidx #2 failed"
    die_if_empty_index "$TMPDIR/packages.ndir$COMPR_EXT"
    if ! diff -u $REPO/packages.ndir.md $TMPDIR/packages.ndir.md; then
        fail "differ md's!"
    fi
}

#  b) --st dir -s /foo          =>  dir -> default type
testCreateFromDir()
{
    $POLDEK_NOCONF --st dir -s $REPO --index || fail "mkidx failed"
    die_if_empty_index "$REPO/packages.ndir$COMPR_EXT"

    # move indexes to separate dir to make sure poldek will not faillback to load it as "dir" repo
    rm -rf $TMPDIR/tmprepo
    mkdir $TMPDIR/tmprepo
    mv $REPO/packages.ndir* $TMPDIR/tmprepo || fail "cp failed"

    check_repo_content $TMPDIR/tmprepo
}

# the descriptions (changelogs) are saved next to the index also when the path has a '%'
testCreateInDirWithPercent()
{
    local repo="$TMPDIR/re%20dpo"

    rm -rf $repo
    mkdir $repo
    cp $REPO/*.rpm $repo || fail "cp failed"

    $POLDEK_NOCONF --st dir -s $repo --index || fail "mkidx failed"
    die_if_empty_index "$repo/packages.ndir$COMPR_EXT"
    check_repo_content $repo
}

#  b) --st rpmdb -s /foo          =>  rpmdb -> default type
testCreateFromRpmdb() {
    skip_if_rpm_testing_off && return

    rm -rf $TMPDIR/tmproot
    mkdir $TMPDIR/tmproot
    $SUDO rpm --root=$TMPDIR/tmproot --initdb

    local rpmop=
    test is_verbose_mode && rpmop="v"

    msgn "prep: rpm install"
    $SUDO rpm --root=$TMPDIR/tmproot -U$rpmop $SOURCE_REPO/sh-*.rpm --noscripts
    for p in $(ls $SOURCE_REPO/a*.rpm); do
        $SUDO rpm --root=$TMPDIR/tmproot -U$rpmop $p --noscripts
    done

    msgn "test"
    $POLDEK_NOCONF --st rpmdb -s $TMPDIR/tmproot/var/lib/rpm --index=$TMPDIR/tmproot/packages.ndir$COMPR_EXT || fail "mkidx failed"
    die_if_empty_index "$TMPDIR/tmproot/packages.ndir$COMPR_EXT"
    check_repo_content $TMPDIR/tmproot
}

#  d) -s /foo --mt dtype        =>  dir  -> dtype
#  e) -s /foo --mt dtype,dtype2 =>  dir  -> dtype
xtestSelectType() # disabled as pdir indexes are gone since 0.4
{
    msgn "$Case_d"
    $POLDEK_NOCONF -s $REPO --mkidx --mt pdir || fail "mkidx failed"
    die_if_empty_index "$REPO/packages.dir$COMPR_EXT"

    rm -rf $REPO/packages.*
    $POLDEK_NOCONF -s $REPO --st dir --mkidx --mt pdir || fail "mkidx failed"
    die_if_empty_index "$REPO/packages.dir$COMPR_EXT"

    setUp

    msgn "$Case_e"
    $POLDEK_NOCONF -s $REPO --mkidx --mt pdir,pndir || fail "mkidx failed"
    die_if_empty_index "$REPO/packages.ndir$COMPR_EXT"
    die_if_empty_index "$REPO/packages.dir$COMPR_EXT"
}

poldek_n() {
    $POLDEK_NOCONF -Osource1="repo-pndir,type=pndir $REPO"  $@
}

#  f) -n foo                    =>  dir (or original type) -> foo's type
testCreateByName()
{
    msgn "$Case_f"
    poldek_n -n repo-pndir --mkidx || fail "mkidx failed"
    die_if_empty_index "$REPO/packages.ndir$COMPR_EXT"
}

#  g) -s /foo --mkidx=<path>
testNonDefaultName()
{
    msgn "$Case_g"
    poldek="$POLDEK_NOCONF"

    msgn "1"
    $poldek -s $REPO --mkidx=$TMPDIR || fail "mkidx #1 failed"
    die_if_empty_index "$TMPDIR/packages.ndir$COMPR_EXT"

    msgn "2"
    $poldek -s $REPO --mkidx=$TMPDIR/packages.foo.ndir$COMPR_EXT || fail "mkidx #2 failed"
    die_if_empty_index "$TMPDIR/packages.foo.ndir$COMPR_EXT"

    msgn "3"
    $poldek --st pndir -s $TMP/packages.foo.ndir$COMPR_EXT \
            --mkidx=$TMP/packages.foo2.ndir$COMPR_EXT --mt pndir || fail "mkidx #3 failed"

    die_if_empty_index "$TMP/packages.foo2.ndir$COMPR_EXT"

    setUp
    msgn "4"
    $poldek -s $REPO --mkidx --mt pndir || fail "mkidx #4 failed"

    msgn "5"
    poldek="$POLDEK_UP -Ovfile_retries=1"
    $poldek --st pndir -s $REPOURL --mkidx=$TMPDIR || fail "mkidx #5 failed"
    die_if_empty_index "$TMPDIR/packages.ndir$COMPR_EXT"

    msgn "6"
    $poldek --st pndir -s $REPOURL --mkidx=$TMPDIR/packages.foo.ndir$COMPR_EXT || fail "mkidx #6 failed"
    die_if_empty_index "$TMPDIR/packages.foo.ndir$COMPR_EXT"
}

#  h) -n foo --mkidx=<path>
testCreateFromRemoteRepos()
{
    msgn "$Case_h"
    poldek_n -n repo-pndir --mkidx=$TMPDIR/packages.foo.ndir$COMPR_EXT --mt pndir ||
        fail "mkidx #1 failed"
    die_if_empty_index "$TMPDIR/packages.foo.ndir$COMPR_EXT"

    poldek_n -n repo-pndir --mkidx=$TMPDIR/packages.foo2.ndir$COMPR_EXT --mt pndir ||
        fail "mkidx #2 failed"

    die_if_empty_index "$TMPDIR/packages.foo2.ndir$COMPR_EXT"
}

testCreateConfiguredDefaultIndexCompr()
{
    msgn "with 'default index compr' = zst"
    echo "
[global]
default index compr = zst
" >> $TMPDIR/poldek.conf

    if [ -n "$POLDEK_TESTING_INDEX_COMPR" ]; then # unset global
        force_msg " => compression is set globally, skipped"
        return;
    fi

    $POLDEK --conf $TMPDIR/poldek.conf -s $REPO --mkidx || fail "mkidx failed"
    die_if_empty_index "$REPO/packages.ndir.zst"
}

testCreateWithIndexComprOption()
{
    compr="$POLDEK_TESTING_INDEX_COMPR"
    if [ -n "$POLDEK_TESTING_INDEX_COMPR" ]; then # unset global
        unset POLDEK_TESTING_INDEX_COMPR
    fi

    op=""
    if [ "$compr" == "zst" ]; then
        op="zstd"
    elif [ "$compr" == "gz" ]; then
        op="gzip"
    elif [ "$compr" == "none" ]; then
        op="nocompress"
    else
        op="gzip"
    fi

    msgn "with --mo=$op"

    $POLDEK_NOCONF -s $REPO --mkidx --mo=$op || fail "mkidx failed"
    die_if_empty_index "$REPO/packages.ndir$COMPR_EXT"
}


. ./sh/lib/shunit2
