#!/bin/sh
# $Id$

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

create_local_conf() {
    > $TMPDIR/poldek.conf
    [ -n "$1" ] && echo "cachedir = $1" >> $TMPDIR/poldek.conf
    echo -e "\n_name_prefix = test\n" >> $TMPDIR/poldek.conf
}

testCacheDir()
{
    USER=${USER:-$(id -un)}

    HOME="$TMPDIR/fakehome"     # in TESTING mode $HOME is used
    mkdir $HOME || fail "mkdir"

    export XDG_CACHE_HOME="$TMPDIR/fakehome-cache"
    mkdir $XDG_CACHE_HOME || fail "mkdir"

    default_home_cachedir="$XDG_CACHE_HOME/poldek"
    tmpdir_cachedir="$TMPDIR/poldek-cache-${USER}"
    legacy_default_home_cachedir="$HOME/.poldek-cache"

    [ -d $default_home_cachedir ] && fail "$default_home_cachedir should does not exists"

    msgn "--noconf --cachedir=<NON-EXISTING-DIR>"
    $POLDEK --noconf --cachedir=$REPO/foobar/baz | grep -q "/foobar/baz: invalid cache directory"
    [ $? -eq "0" ] || fail "non existent directory accepted"
    [ -d $default_home_cachedir ] && fail "$default_home_cachedir should not exists"
    [ -d $REPO/foobar ] && fail "directory exist"


    cmdline_cachedir="$REPO/cmdline-cachedir"
    msgn "--noconf --cachedir"
    $POLDEK --noconf --cachedir=$cmdline_cachedir -l
    [ -d $cmdline_cachedir ] || fail "$cmdline_cachedir does not exists"
    [ -d $default_home_cachedir ] && fail "$default_home_cachedir should not exists"
    rm -rf $cmdline_cachedir $default_home_cachedir

    configured_cachedir="$REPO/configured-cachedir"
    create_local_conf $configured_cachedir

    msgn "--conf  --cachedir"
    $POLDEK --conf $TMPDIR/poldek.conf --cachedir=$cmdline_cachedir -l
    [ -d $cmdline_cachedir ] || fail "--cachedir'ed $cmdline_cachedir does not exist"
    [ -d $configured_cachedir ] && fail "overwritten by cmdline $configured_cachedir exists"
    [ -d $default_home_cachedir ] && fail "$default_home_cachedir should not exists"
    rm -rf $cmdline_cachedir $configured_cachedir $default_home_cachedir

    msgn "--conf with cachedir"
    $POLDEK --conf $TMPDIR/poldek.conf -l
    [ ! -d $configured_cachedir ] && fail "$configured_cachedir does not exists"
    [ -d $default_home_cachedir ] && fail "$default_home_cachedir should not exists"
    rm -rf $configured_cachedir $default_home_cachedir $legacy_default_home_cachedir

    msgn "--conf with an invalid cachedir"
    create_local_conf "$REPO/foobar/baz"
    out=$($POLDEK --conf $TMPDIR/poldek.conf -l 2>&1)
    rc=$?
    assertEquals "an invalid cachedir in the configuration should fail: $out" \
                 "1" "$rc"
    echo "$out" | grep -q "invalid cache directory" || fail "no reason given: $out"
    [ -d $REPO/foobar ] && fail "$REPO/foobar should not exists"
    rm -rf $REPO/foobar

    create_local_conf ""

    msgn "--conf without cachedir"
    $POLDEK --conf $TMPDIR/poldek.conf -l
    [ -d $tmpdir_cachedir ] && fail "$tmpdir_cachedir does not exists"
    [ -d $default_home_cachedir ] || fail "$default_home_cachedir should exists"
    rm -rf $default_home_cachedir $tmpdir_cachedir $legacy_default_home_cachedir

    legacy_default_home_cachedir="$HOME/.poldek-cache"
    msgn "--conf without cachedir, with legacy HOME/.poldek-cache"
    mkdir $legacy_default_home_cachedir
    $POLDEK --conf $TMPDIR/poldek.conf -l
    [ -d $default_home_cachedir ] && fail "$default_home_cachedir should not exists"
    [ -d $tmpdir_cachedir ] && fail "$tmpdir_cachedir should not exists"
    [ ! -d $legacy_default_home_cachedir ] && fail "$legacy_default_home_cachedir"
    rm -rf $default_home_cachedir $tmpdir_cachedir $legacy_default_home_cachedir

    rm -rf $configured_cachedir $default_home_cachedir $legacy_default_home_cachedir

    # a remote --conf, with its cachedir taken from the configuration, is gone:
    # see testRemoteConfRefused in 04-conf

    return 0
}


. ./sh/lib/shunit2
