#!/bin/sh
# PCP QA Test No. 1864
# -d and related bits for pmlogger_daily
#
# Copyright (c) 2026 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

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

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

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

# some non-determinism in these lines due to du(1) and filesystem space
# allocation algorithms:
# ... archives for localhost (1056 Kbytes) ...
# ... localhost, archives (524 Kbytes) ...
#
_filter()
{
    sed \
	-e '/^Roll .*\/NOTICES/d' \
	-e '/^Start .*\/NOTICES/d' \
	-e '/^Start \[/s/ 2[0-9:. -]*/ DATESTAMP/' \
	-e '/^End \[/s/ 2[0-9:. -]*/ DATESTAMP /' \
	-e '/archives for localhost/{
s/localhost (10[12][0-9] /localhost ([1010+] /
s/localhost (10[56][0-9] /localhost ([1050+] /
}' \
	-e '/localhost, archives/{
s/archives (5[0-2][0-9] /archives ([500+] /
}' \
	-e '/Info: purge 20260202/{
s/reclaims 3[0-9] Kbyte/reclaims [30+] Kbyte/
}' \
	-e '/Info: purge 20260203/{
s/reclaims 49[0-9] Kbyte/reclaims [490+] Kbyte/
s/reclaims 5[01][0-9] Kbyte/reclaims [490+] Kbyte/
}' \
	-e "s@$tmp@TMP@g" \
    # end
}

_setup()
{
    cd $here
    $sudo rm -rf $tmp/*
    if ./mk.logfarm -c $tmp.farm $tmp >$tmp.out 2>&1
    then
	_filter <$tmp.out
    else
	cat $tmp.out
	echo "Arrgh ... mk.logfarm failed!"
	_exit
    fi
    cd $tmp
    find * \
    | while read f
    do
	case "$f"
	in
	    */..)	;;
	    *)		$sudo chown $PCP_USER:$PCP_GROUP "$f"
	    		;;
	esac
    done
    echo "setup() ..." >>$seq_full
    find $tmp/* -type d \
    | while read dir
    do
	du -sk $dir >>$seq_full
    done
}

_doit()
{
    ( cd $tmp; find * -type f ) | _filter | sort >$tmp.before
    $sudo rm -f $tmp.log
    $sudo -E -u $PCP_USER $PCP_BINADM_DIR/pmlogger_daily $@ -c $tmp.control -D -P -V -l $tmp.log >$tmp.err 2>&1
    if [ -s $tmp.err ]
    then
	_filter <$tmp.err
	[ -s $tmp.log ] && _filter <$tmp.log
	echo "... errors from pmlogger_daily!"
    else
	[ -s $tmp.err ] && _filter <$tmp.err
	[ -s $tmp.log ] && _filter <$tmp.log
	echo
	( cd $tmp; find * -type f ) | _filter | sort >$tmp.after
	echo "Changes ..."
	diff $tmp.before $tmp.after
    fi
}

# archive	Kbytes
# cgroups-units	 36
# 20041125	340
# ok-bigbin	492
# ok-mv-bigbin	520
#
cat <<End-of-File >$tmp.farm
# expect 1048 Kbytes in total
foo	archives/ok-mv-bigbin	20260204
foo	archives/ok-bigbin	20260203
foo	archives/cgroups-units	20260202
End-of-File

mkdir $tmp
_setup

# real QA test starts here

echo "=== errors in control file ==="

# bad control file
#
cat <<End-of-File >$tmp.control
\$version=1.1
\$PCP_CULLAFTER=never
\$PCP_SPACELIMIT
\$PCP_SPACELIMIT=
\$PCP_SPACELIMIT=foo
\$PCP_SPACELIMIT=123
#Host           P?  S?  directory       args
localhost       n   n   $tmp/foo    -c /no/such/config
End-of-File
_doit

# good control file
#
cat <<End-of-File >$tmp.control
\$version=1.1
\$PCP_CULLAFTER=never
#Host           P?  S?  directory       args
localhost       n   n   $tmp/foo    -c /no/such/config
End-of-File

echo
echo "=== command line errors ==="
_doit "-d foo"
_doit "-d"
_doit "-d 1234"

echo
echo "=== expect no purging ==="
_doit

echo
echo "=== -d 1020k ==="
_doit "-d 1020k" 

echo
echo "=== PCP_SPACELIMIT=1000k"
PCP_SPACELIMIT=1000k
export PCP_SPACELIMIT
_doit

echo
echo "=== ignore -d use value from env ==="
_doit "-d 500K"

unset PCP_SPACELIMIT

# another good control file
#
cat <<End-of-File >$tmp.control
\$version=1.1
\$PCP_CULLAFTER=never
\$PCP_SPACELIMIT=1000K
#Host           P?  S?  directory       args
localhost       n   n   $tmp/foo    -c /no/such/config
End-of-File

echo
echo "=== ignore -d use value from control file ==="
_doit "-d 500K"

# success, all done
exit
