#!/bin/sh
# $Id$

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

create_local_conf() {
echo "
_name_prefix = test

[source]
name = %{_name_prefix}1
type = pndir
path = ${REPOURL}1

[source]
name = %{_name_prefix}2
type = pdir
path = ${REPOURL}2
" > $TMPDIR/poldek.conf
}

testConf()
{
    #cat $TMPDIR/poldek.conf
    create_local_conf
    poldek_l="$POLDEK --conf $TMPDIR/poldek.conf -q  -l"

    is_verbose_mode && $poldek_l

    n=$($poldek_l | grep 'test://' | wc -l)
    assertEquals "2 sources expected" "$n" "2"

    n=$($poldek_l | grep -E '^test[12] ' | wc -l)
    assertEquals "2 sources expected, but not those" "$n" "2"
}

# argp loops or crashes on --help below about 38 columns; poldek gives it at least 40
testHelpNarrowTerminal()
{
    if ! command -v script >/dev/null; then
        msgn "no script(1), skipped this test"
        return 0
    fi

    typeset out
    out=$(timeout 10 script -qec "stty cols 20; $POLDEK --help" /dev/null </dev/null 2>&1)
    typeset rc=$?
    if [ "$rc" -ne 0 ]; then
        echo "$out" >&2
        fail "--help on a 20-column terminal failed with exit $rc"
    fi
}

# with stdout a pipe or a pty with no size set, the width comes from $COLUMNS
testListSourcesColumns()
{
    path="/nonexistent/$(printf 'long-directory-name-%02d/' 1 2 3 4 5)repo"

    out=$(COLUMNS=500 $POLDEK --noconf -s $path --st dir -l)
    echo "$out" | grep -qF "$path" || fail "path shortened with COLUMNS=500: $out"

    out=$(COLUMNS= $POLDEK --noconf -s $path --st dir -l)
    echo "$out" | grep -qF '[...]' || fail "path not shortened to 80 columns: $out"

    # script(1) run without a terminal gives a pty with no size set
    if command -v script >/dev/null; then
        out=$(COLUMNS=500 script -qec "$POLDEK --noconf -s $path --st dir -l" /dev/null </dev/null)
        echo "$out" | grep -qF "$path" || fail "path shortened on a pty with no size: $out"
    fi
}

testListSourcesCompr()
{
    echo "
[source]
name = zst
type = pndir
path = /nonexistent/repo/
compr = zst
" > $TMPDIR/compr.conf

    out=$($POLDEK --conf $TMPDIR/compr.conf -l 2>&1) || fail "-l failed with exit $?: $out"
    echo "$out" | grep -qF 'compr=zst' || fail "compr=zst not listed: $out"
}

# [source] has only "signed", which stands for gpg and pgp too
testLegacySourceOptions()
{
    echo "
[global]
source = gpg,pri=7,gpg,compr=zst /nonexistent/repo1/
source = pgp,pgp /nonexistent/repo2/
" > $TMPDIR/legacy.conf

    out=$($POLDEK --conf $TMPDIR/legacy.conf -l 2>&1) || fail "-l failed with exit $?: $out"
    for o in pri=7 sign compr=zst; do
        echo "$out" | grep -E '^gpg ' | grep -qF "$o" || fail "gpg: $o not listed: $out"
    done
    echo "$out" | grep -E '^pgp ' | grep -qF sign || fail "pgp: sign not listed: $out"
}

# same option given on the command line, in the "name,opt path" syntax
testListSourcesComprFromCommandLine()
{
    out=$($POLDEK --noconf -s "zst,compr=zst /nonexistent/repo/" --st dir -l 2>&1) || fail "-l with -s failed with exit $?: $out"
    echo "$out" | grep -qF 'compr=zst' || fail "compr=zst from -s not listed: $out"
}

# a legacy "source = name,opt path" line keeps the value, not just the fact
testListSourcesComprLegacyLine()
{
    printf '%s\n' "[global]" "source = zst,compr=zst /nonexistent/repo/" > $TMPDIR/compr-legacy.conf

    out=$($POLDEK --conf $TMPDIR/compr-legacy.conf -l 2>&1) || fail "-l failed with exit $?: $out"
    echo "$out" | grep -qF 'compr=zst' || fail "compr=zst from a legacy source line not listed: $out"
}

testConfAsRoot()
{
    if command -v ldd >/dev/null; then
        if ldd $POLDEK_RAW | grep -q asan; then
            msgn "asan detected, skipped this test"
            return 0
        fi
    fi

    create_local_conf
    msgn "--conf"
    poldek_l="$POLDEK --conf $TMPDIR/poldek.conf -q  -l"

    is_verbose_mode && $poldek_l

    n=$(fakeroot $poldek_l | grep 'test://' | wc -l)
    assertEquals "2 sources expected" "$n" "2"

    n=$(fakeroot $poldek_l | grep -E '^test[12] ' | wc -l)
    assertEquals "2 sources expected, but not those" "$n" "2"

    msgn "default conf"
    mkdir $TMPDIR/home || fail "mkdir"
    mv -f $TMPDIR/poldek.conf $TMPDIR/home/.poldekrc
    HOME=$TMPDIR/home
    export HOME

    poldek_l="$POLDEK -q -l"
    is_verbose_mode && $poldek_l

    n=$(fakeroot $poldek_l | grep 'test://' | wc -l)
    assertEquals "2 sources expected" "$n" "2"

    n=$(fakeroot $poldek_l | grep -E '^test[12] ' | wc -l)
    assertEquals "2 sources expected, but not those" "$n" "2"
}

testConfMacros()
{
    echo "
_arch   = x86_64
_snap	= 2019
_prefix = https://test/dists/th/%{_snap}

[source]
type   = pndir
name   = th-%{_snap}
path   = %{_prefix}/PLD/%{_arch}/RPMS/
" > $TMPDIR/macros.conf

    local source_url=$($POLDEK --conf $TMPDIR/macros.conf -l | awk '{print $2}')
    local url="https://test/dists/th/2019/PLD/x86_64/RPMS/"
    assertEquals "expected $url, got $source_url" "$source_url" "$url"
}

testConfMacrosNested()
{
    echo "
_arch   = x86_64
_snap	= 2019
_zprefix = https://test/dists/th/%{_snap}
_prefix = %{_zprefix}

[source]
type   = pndir
name   = th-%{_snap}
path   = %{_prefix}/PLD/%{_arch}/RPMS/
" > $TMPDIR/macros.conf

    local source_url=$($POLDEK --conf $TMPDIR/macros.conf -l | grep -P '^th-2019' | awk '{print $2}')
    local url="https://test/dists/th/2019/PLD/x86_64/RPMS/"
    assertEquals "expected $url, got $source_url" "$source_url" "$url"
}

testConfInclude()
{
    #cat $TMPDIR/poldek.conf
    create_local_conf
    conf="$TMPDIR/poldek.conf"
    poldek_l="$POLDEK --conf $conf -q -l"
    cp $conf $conf.orig

    msgn "%include foo.conf"

    echo -e "[global]\n%include source.conf\n" > $conf
    cat $conf.orig >> $conf
    echo -e "[source]\nname=test3\ntype=pndir\npath=test:///foo/bar/z\n" > $TMPDIR/source.conf

    is_verbose_mode && $poldek_l

    n=$($poldek_l | grep 'test://' | wc -l)
    assertEquals "3 sources expected, got $n" "$n" "3"

    n=$($poldek_l | grep -E '^test[123] ' | wc -l)
    assertEquals "3 sources expected, but not those" "$n" "3"


    msgn "%include /bar/baz/foo.conf"

    echo -e "[global]\n%include $REPO/source-abs.conf\n" > $conf
    cat $conf.orig >> $conf
    echo -e "[source]\nname=test4\ntype=pndir\npath=test:///foo/bar/z\n" > $REPO/source-abs.conf
    is_verbose_mode && $poldek_l

    n=$($poldek_l | grep 'test://' | wc -l)
    assertEquals "3 sources expected, got $n" "$n" "3"

    n=$($poldek_l | grep -E '^test[124] ' | wc -l)
    [ "$n" == "3" ] && assertEquals "3 sources expected, but not those" "$n" "3"
}


testRemoteConfAndMacros()
{

    create_local_conf

    POLDEK_TESTING_PRELOAD_CONF="$SHLIBDIR/vftest-fetch.conf"
    export POLDEK_TESTING_PRELOAD_CONF

    echo "
[global]
%include ${REPOURL}remote.conf

[source]
name = %{_name_prefix}3
type = pndir
path = ${REPOURL}3

[source]
name = %{_name_prefix}4
type = pndir
path = ${REPOURL}4
" >> $TMPDIR/poldek.conf

    echo "
_name_prefix = xtest
[source]
name = %{_name_prefix}1
type = pndir
path = ${REPOURL}x1

[source]
name = %{_name_prefix}2
type = pdir
path = ${REPOURL}x2
" > $REPO/remote.conf


    poldek_l="$POLDEK --conf $TMPDIR/poldek.conf -q -l"
    poldek_l_cdir="$poldek_l --cachedir $CACHEDIR"

    n=$($poldek_l --noconf | grep 'test://' | wc -l)
    assertEquals "--noconf failed" "$n" "0"

    #$POLDEK --conf $TMPDIR/poldek.conf  -l --cachedir $CACHEDIR

    n=$($poldek_l_cdir | grep 'test://' | wc -l)
    assertEquals "6 sources expected, got $n" "$n" "6"

    is_verbose_mode && $poldek_l_cdir

    n=$($poldek_l_cdir | grep -E '^test[1234] ' | wc -l)
    [ "$n" == "4" ] && assertEquals "4 sources expected, but not those" "$n" "4"

    n=$($poldek_l_cdir  -l | grep -E '^xtest[1234] ' | wc -l)
    [ "$n" == "2" ] && assertEquals "2 xtest* sources expected, but not those $n" "$n" "2"

    msgn "upconf"

    echo "
[source]
name = %{_name_prefix}3
type = pndir
path = ${REPOURL}x3

[source]
name = %{_name_prefix}4
type = pdir
path = ${REPOURL}x4
" >> $REPO/remote.conf

    POLDEK_TESTING_DENIED_FILES="remote.conf"
    #cat $TMPDIR/poldek.conf
    is_verbose_mode && echo $poldek_l_cdir && $poldek_l_cdir

    n=$($poldek_l_cdir | grep 'test://' | wc -l)
    assertEquals "6 sources expected" "$n" "6"
    #$POLDEK --conf $TMPDIR/poldek.conf -q  -l | grep -E '^test[12] '
    n=$($poldek_l_cdir | grep -E '^test[1234] ' | wc -l)
    [ "$n" == "4" ] && assertEquals "4 sources expected, but not those" "$n" "4"

    POLDEK_TESTING_DENIED_FILES=""
    $POLDEK --conf $TMPDIR/poldek.conf --upconf --cachedir $CACHEDIR

    POLDEK_TESTING_DENIED_FILES="remote.conf"
    n=$($poldek_l_cdir | grep 'test://' | wc -l)
    assertEquals "8 sources exepected, got $n" "$n" "8"

    n=$($poldek_l_cdir | grep -E '^test[1234] ' | wc -l)
    [ "$n" == "4" ] && assertEquals "4 sources expected, but not those" "$n" "4"

    n=$($poldek_l_cdir | grep -E '^xtest[1234] ' | wc -l)
    [ "$n" == "4" ] && assertEquals "4 sources expected, but not those" "$n" "4"

    n=$(find $CACHEDIR -name \*.conf | wc -l)
    assertEquals "Expected cachedir/remote.conf only" "$n" "1"

    #find $CACHEDIR
    POLDEK_TESTING_DENIED_FILES="remote.conf"

    msgn "whole conf remote"
    $POLDEK --conf test://$TMPDIR/poldek.conf --cachedir $CACHEDIR -q -l >/dev/null
    is_verbose_mode && $poldek_l_cdir

    n=$($poldek_l_cdir | grep 'test://' | wc -l)
    assertEquals "8 sources exepected, got $n" "$n" "8"

    n=$($poldek_l_cdir | grep -E '^test[1234] ' | wc -l)
    [ "$n" == "4" ] && assertEquals "4 sources expected, but not those" "$n" "4"

    n=$($poldek_l_cdir | grep -E '^xtest[1234] ' | wc -l)
    [ "$n" == "4" ] && assertEquals "4 sources expected, but not those" "$n" "4"

    n=$(find $CACHEDIR -name remote.conf | wc -l)
    assertEquals "Where is remote.conf cached copy??" "$n" "1"

    n=$(find $CACHEDIR -name poldek.conf | wc -l)
    assertEquals "Where is poldek.conf cached copy??" "$n" "1"
}

# argp prints the bug address only if it can see poldek's argp_program_bug_address
testHelpShowsBugAddress()
{
    out=$(LC_ALL=C $POLDEK --help)
    echo "$out" | grep -q "^Report bugs to " || fail "no bug address in --help: $(echo "$out" | tail -2)"
}

. ./sh/lib/shunit2
