#!/bin/sh
# $Id$

# --mkidx tests

#  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"
Case_d="-s /foo --mt dtype        =>  dir  -> dtype"
Case_e="-s /foo --mt dtype,dtype2 =>  dir  -> dtype"
Case_f="-n foo                    =>  dir (or original type) -> foo's type"
Case_g="-s /foo --mkidx=/tmp/bar.gz"
Case_h="-n foo --mkidx=/tmp/bar.gz"

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

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 type -s /foo         =>  type -> default type
#  c) --st type /foo --mt type  =>  type -> dtype
XXXtestConvertRepos()           # disabled as pdir indexes are gone since 0.4
{
    $POLDEK_NOCONF -s $REPO --mkidx || fail "mkidx failed"
    die_if_empty_index "$REPO/packages.ndir$COMPR_EXT"

    rm -rf $TMPDIR/packages.*
    cp $REPO/packages.ndir.* $TMPDIR || fail "cp failed"


    # c) --st type /foo --mt type =>  type -> dtype
    msgn "$Case_c"
    $POLDEK_NOCONF --st pndir -s $TMPDIR --mkidx --mt=pdir || fail "$Case_c failed"
    die_if_empty_index "$TMPDIR/packages.dir$COMPR_EXT"

    # b) --st type -s /foo
    msgn "$Case_b"
    rm -f $TMPDIR/packages.ndir*
    $POLDEK_NOCONF --st pdir -s $TMPDIR --mkidx || fail "$Case_b failed"
    die_if_empty_index "$TMPDIR/packages.ndir$COMPR_EXT"
}

#  d) -s /foo --mt dtype        =>  dir  -> dtype
#  e) -s /foo --mt dtype,dtype2 =>  dir  -> dtype
XXXtestSelectType() # 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=/tmp/bar$COMPR_EXT
testNonDefaultName()
{
    msgn "$Case_g"
    poldek="$POLDEK_NOCONF"

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

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

    $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
    $poldek -s $REPO --mkidx --mt pndir || fail "mkidx #4 failed"


    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"

    $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=/tmp/bar$COMPR_EXT
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
