#! /bin/sh
# PCP QA Test No. 532
# pmlogger_check with *-xx.* archives and .meta files in subdirs
# (loosely based on 530 and 338)
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#
# Note: for reasons that are not understood, this test sometimes fails
# 	but passes when rerun
#
# check-group-include: pmlogrewrite pmlogextract
#

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

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

_filter()
{
    tee -a $seq_full \
    | sed -f $tmp/sed \
    | sed \
	-e '/^Roll .*\/NOTICES/d' \
	-e '/^Start .*\/NOTICES/d' \
	-e '/^Start \[/s/ 2[0-9:. -]*/ DATESTAMP/' \
	-e '/^End \[/s/ 2[0-9:. -]*/ DATESTAMP /' \
	-e '/^Add merge callback for: pmlogger_daily_report/d' \
	-e '/^Merge callbacks .../d' \
	-e '/^callback: .*pmlogger_daily_report/d' \
	-e "s;$tmp;TMP;g" \
	-e "s/local:/HOST/g" \
	-e "s/`hostname`/HOST/g" \
	-e "s/`hostname | sed -e 's/\..*//'`/HOST/g" \
	-e '/\/var\/adm\/pcplog\/NOTICES/d' \
	-e 's/-rw-r--r--\./-rw-r--r--/' \
    | $PCP_AWK_PROG '
/-rw-r--r--/	{ print $1,"...",$5,"...",$9; next }
		{ print }' \
    | _filter_cron_scripts
}

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

# take explicit control of the umask
#
umask 022

mkdir $tmp
mkdir $tmp/foo

# real QA test starts here
verbose=-V
echo >$tmp/sed
for day in 0 1 2 3
do
    fmt=%Y%m%d

    for hour in 1 4 16
    do
	[ $day -gt 0 -a $hour -gt 1 ] && continue
	# aiming for the time to be hour:10, hour:20 and hour:30 on
	# each day, even if that is ahead of NOW for today
	#
	now_hr=`pmdate %H`
	hr_set=`expr $hour - $now_hr`
	[ "$hr_set" -ge 0 ] && hr_set="+$hr_set"
	now_min=`pmdate %M`
	min_set10=`expr 10 - $now_min`
	[ "$min_set10" -ge 0 ] && min_set10="+$min_set10"
	min_set20=`expr 20 - $now_min`
	[ "$min_set20" -ge 0 ] && min_set20="+$min_set20"
	min_set30=`expr 30 - $now_min`
	[ "$min_set30" -ge 0 ] && min_set30="+$min_set30"
	namea=`pmdate -${day}d ${hr_set}H ${min_set10}M $fmt.%H.%M`
	nameb=`pmdate -${day}d ${hr_set}H ${min_set20}M $fmt.%H.%M`
	namec=`pmdate -${day}d ${hr_set}H ${min_set30}M $fmt.%H.%M`
	yesterday=`pmdate -1d ${hr_set}H ${min_set10}M $fmt`
	stampa=`echo $namea | sed -e 's/\.//g'`
	stampb=`echo $nameb | sed -e 's/\.//g'`
	stampc=`echo $namec | sed -e 's/\.//g'`
	nameb=${namea}-00
	namec=${namea}-01
	echo "s/$namea/NOW-$day.${hour}H.MM/g" >>$tmp/sed
	echo "s/$nameb/NOW-$day.${hour}H.MM-00/g" >>$tmp/sed
	echo "s/$namec/NOW-$day.${hour}H.MM-01/g" >>$tmp/sed

	for ext in 0 index meta
	do
	    if [ $hour -ne 4 ]
	    then
		cp archives/arch_a.$ext $tmp/$namea.$ext
		touch -t $stampa $tmp/$namea.$ext
	    fi
	    cp archives/arch_b.$ext $tmp/$nameb.$ext
	    touch -t $stampb $tmp/$nameb.$ext
	    cp archives/arch_b.$ext $tmp/$namec.$ext
	    touch -t $stampc $tmp/$namec.$ext
	done
    done
done

( cd $tmp; tar cf - *.0 *.meta *.index 2>/dev/null ) \
| ( cd $tmp/foo; tar xpf - 2>/dev/null )

echo "s/NOW-0/TODAY/g" >>$tmp/sed
echo "s/NOW-1/YESTERDAY/g" >>$tmp/sed
echo "s/`pmdate %Y%m%d`/TODAY/g" >>$tmp/sed
echo "s/$yesterday/YESTERDAY/g" >>$tmp/sed

echo >$tmp/control '$version=1.1
LOCALHOSTNAME	n   n	'"$tmp"'	-c ./config'

# pmlogger_daily runs as the user "pcp" ... fix ownership
#
touch $tmp.log
find $tmp $tmp.log -type f | xargs $sudo chown $PCP_USER:$PCP_GROUP
$sudo chown $PCP_USER:$PCP_GROUP $tmp

echo
echo "Before ..."
ls $tmp | sed -f $tmp/sed | LC_COLLATE=POSIX sort | tee $tmp.before
ls -l $tmp >>$seq_full

# list of culled files is in directory order, hence the cut and sort
# complexity at the end of this pipeline
#
echo
# need to be a place the user $PCP_USER can cd to from pmlogger_daily
#
cd /var/tmp
$sudo -u $PCP_USER -g $PCP_GROUP sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_daily -P -D -o -c $tmp/control -x never -k 3 $verbose -l $tmp.log"
cd $here
$sudo cat $tmp.log \
| _filter \
| $PCP_AWK_PROG '
state == 0		{ print >"'$tmp.out.0'"
			  if ($0 ~ /older than /) state = 1
			  next
			}
state == 1 && $0 ~ /Input archives to be merged:/	{ state = 2 }
state == 1		{ for (i = 1; i <= NF; i++) print $i >"'$tmp.out.1'" }
state == 2		{ print >"'$tmp.out.2'" }'
cat $tmp.out.0
LC_COLLATE=POSIX sort $tmp.out.1
[ -f $tmp.out.0 ] && cat $tmp.out.2

echo "out.0 ..." >>$seq_full
cat $tmp.out.0 >>$seq_full
echo "out.1 ..." >>$seq_full
cat $tmp.out.1 >>$seq_full

echo
echo "After ..."
ls $tmp | sed -f $tmp/sed | LC_COLLATE=POSIX sort | tee $tmp.after

pmdumplog -dilmst $tmp/$yesterday >>$seq_full

echo
echo "Differences ..."
diff $tmp.before $tmp.after

# all done
status=0
exit
