#!/bin/sh
# PCP QA Test No. 626
# pmlogger_daily with -x 0 and previously failing to merge correctly
# ... list of files below comes from an observed interactive run
# of pmlogger_daily during -x 0 development
#
# Copyright (c) 2018 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

if which zstd >/dev/null 2>&1
then
    PROG="zstd --rm --quiet"
    SUFF=zst
elif which xz >/dev/null 2>&1
then
    PROG=xz
    SUFF=xz
elif which bzip2 >/dev/null 2>&1
then
    PROG=bzip2
    SUFF=bz2
elif which gzip >/dev/null 2>&1
then
    PROG=gzip
    SUFF=gz
else
    _notrun "cannot find a compression program!"
fi

NEEDREWRITE=$PCP_LOG_DIR/pmlogger/.NeedRewrite

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
    [ -f $NEEDREWRITE.$seq ] && $sudo mv $NEEDREWRITE.$seq $NEEDREWRITE
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

[ -f $NEEDREWRITE ] && $sudo mv $NEEDREWRITE $NEEDREWRITE.$seq

_setup_control()
{
    echo '$version=1.1' >$tmp.control 
    for arg
    do
	echo "\$$arg" >>$tmp.control
    done
    echo "LOCALHOSTNAME	n   n	$tmp	$tmp.config" >>$tmp.control
    cat $tmp.control >>$seq_full
}

_filter()
{
    if [ $# -eq 0 ]
    then
	cmd=cat
    elif [ -f "$1" ]
    then
	cmd="cat \"$1\""
    else
	return
    fi
    eval $cmd \
    | tee -a $seq_full \
    | sed \
	-e '/pmlogger_daily.pid: Permission denied/d' \
	-e '/no pmlogger instance running for host/d' \
	-e '/^pmlogger_daily:.*\.control:[0-9]]$/d' \
	-e '/logging for host .* unchanged/d' \
	-e '/^rm: cannot remove .*pmlogger_daily.stamp/d' \
	-e '/^cp: cannot create regular file .*pmlogger_daily.stamp/d' \
	-e '/^cp: .*\/pmlogger_daily.stamp: Permission denied/d' \
	-e '/^.[r-][w-][x-].*pmlogger_daily.stamp/d' \
	-e '/^_save_prev_filename: unable to remove/d' \
	-e '/^Warning: cannot save previous date-and-timestamp/d' \
	-e '/^Warning: cannot install new date-and-timestamp/d' \
    # end
}

_showfiles()
{
    ls $tmp/* \
    | sed \
	-e "s@$tmp@TMP@" \
	-e "s/\.$SUFF$/.compressed/" \
	-e "s/\.xz$/.compressed/" \
	-e "s/$DATE/DATE/" \
    # end
}

cp archives/ok-foo.0 $tmp.0
$PROG $tmp.0 >>$seq_full 2>&1

mkdir $tmp
cat <<End-of-File | while read f
20180225.0.$SUFF
20180225.index
20180225.meta
20180226.0.$SUFF
20180226.index
20180226.meta
20180227.0.$SUFF
20180227.index
20180227.meta
20180301.0.$SUFF
20180301.index
20180301.meta
20180302.0.$SUFF
20180302.index
20180302.meta
20180303.0.$SUFF
20180303.index
20180303.meta
20180304.0.$SUFF
20180304.index
20180304.meta
20180306.0.$SUFF
20180306.index
20180306.meta
20180307.0.$SUFF
20180307.index
20180307.meta
20180308.0.$SUFF
20180308.index
20180308.meta
20180311.00.10.0.$SUFF
20180311.00.10.index
20180311.00.10.meta
20180311.19.16.0.$SUFF
20180311.19.16.index
20180311.19.16.meta
20180311.19.17.0.$SUFF
20180311.19.17.index
20180311.19.17.meta
20180311.19.18.0.$SUFF
20180311.19.18.index
20180311.19.18.meta
20180311.19.20-00.0.$SUFF
20180311.19.20-00.index
20180311.19.20-00.meta
20180311.19.20-01.0.$SUFF
20180311.19.20-01.index
20180311.19.20-01.meta
20180311.19.22.0.$SUFF
20180311.19.22.index
20180311.19.22.meta
20180311.19.23-00.0.$SUFF
20180311.19.23-00.index
20180311.19.23-00.meta
20180311.19.23.0.$SUFF
20180311.19.23.index
20180311.19.23.meta
20180311.19.24-00.0.$SUFF
20180311.19.24-00.index
20180311.19.24-00.meta
20180311.19.24.0.$SUFF
20180311.19.24.index
20180311.19.24.meta
20180311.19.28-00.0.$SUFF
20180311.19.28-00.index
20180311.19.28-00.meta
20180311.19.28-01.0.$SUFF
20180311.19.28-01.index
20180311.19.28-01.meta
20180311.19.28.0.$SUFF
20180311.19.28.index
20180311.19.28.meta
20180311.20.51.0.$SUFF
20180311.20.51.index
20180311.20.51.meta
20180311.20.56.0.$SUFF
20180311.20.56.10.$SUFF
20180311.20.56.11.$SUFF
20180311.20.56.12.$SUFF
20180311.20.56.1.$SUFF
20180311.20.56.2.$SUFF
20180311.20.56.3.$SUFF
20180311.20.56.4.$SUFF
20180311.20.56.5.$SUFF
20180311.20.56.6.$SUFF
20180311.20.56.7.$SUFF
20180311.20.56.8.$SUFF
20180311.20.56.9.$SUFF
20180311.20.56.index
20180311.20.56.meta
20180312.00.10.0.$SUFF
20180312.00.10.10.$SUFF
20180312.00.10.11.$SUFF
20180312.00.10.12.$SUFF
20180312.00.10.13.$SUFF
20180312.00.10.14.$SUFF
20180312.00.10.15.$SUFF
20180312.00.10.16.$SUFF
20180312.00.10.17.$SUFF
20180312.00.10.18.$SUFF
20180312.00.10.19.$SUFF
20180312.00.10.1.$SUFF
20180312.00.10.20.$SUFF
20180312.00.10.21.$SUFF
20180312.00.10.22.$SUFF
20180312.00.10.23.$SUFF
20180312.00.10.24.$SUFF
20180312.00.10.25.$SUFF
20180312.00.10.26.$SUFF
20180312.00.10.27.$SUFF
20180312.00.10.28.$SUFF
20180312.00.10.29.$SUFF
20180312.00.10.2.$SUFF
20180312.00.10.30.$SUFF
20180312.00.10.31.$SUFF
20180312.00.10.32.$SUFF
20180312.00.10.33.$SUFF
20180312.00.10.34.$SUFF
20180312.00.10.3.$SUFF
20180312.00.10.4.$SUFF
20180312.00.10.5.$SUFF
20180312.00.10.6.$SUFF
20180312.00.10.7.$SUFF
20180312.00.10.8.$SUFF
20180312.00.10.9.$SUFF
20180312.00.10.index
20180312.00.10.meta
20180312.08.45.0.$SUFF
20180312.08.45.index
20180312.08.45.meta
End-of-File
do
    case $f
    in
	*.index)
	    cp archives/ok-foo.index $tmp/$f
	    ;;
	*.meta)
	    cp archives/ok-foo.meta $tmp/$f
	    ;;
	*.[0-9].$SUFF|*.[0-9][0-9].$SUFF)
	    # fake out the .$SUFF files with a compressed ok-foo.0
	    cp $tmp.0.$SUFF $tmp/$f
	    ;;
	*)
	    echo "$f - eh?"
	    exit
	    ;;
    esac
done
_showfiles >$tmp.before

# real QA test starts here
_setup_control PCP_COMPRESSAFTER=0 PCP_COMPRESS="'$PROG'"
pmlogger_daily -P -D -f -c $tmp.control -l $tmp.log >$tmp.out 2>$tmp.err
_filter $tmp.err; _filter $tmp.out; _filter $tmp.log
_showfiles >$tmp.after

echo
echo "diffs ... expect 20180311 and 20180312 archives to be merged"
diff $tmp.before $tmp.after

# success, all done
status=0
exit
