#
# Copyright (c) 2015 Ken McDonell.  All Rights Reserved.
# Copyright (c) 2012-2015 Red Hat.
# Copyright (c) 2009 Aconex.  All Rights Reserved.
# Copyright (c) 1997-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

MKARCH	= foo.0 foo_v2.0 foo_v3.0 mv-foo.0 noti-foo.0 noti-ok-foo.0 empty.0 \
	bar.0 mv-bar.0 noti-bar.0 \
	bigbin.0 mv-bigbin.0 noti-bigbin.0 truncbin.0 \
	interp.0 mv-interp.0 noti-interp.0 \
	gap.0 gap2.0 reduce-gap.0 \
	dodgey-some.0 dodgey-all.0 dodgey-mixed.0 bad-1.0 \
	sample-proc_v2.0 sample-proc_v3.0 \
	sampledso-no-delta_v2.0 sampledso-no-delta_v3.0

LDIRT = $(MKARCH) \
	$(patsubst %.0,%.meta,$(MKARCH)) $(patsubst %.0,%.index,$(MKARCH)) \
	mv-foo.[1-9] mv-bar.[1-9] mv-bigbin.[1-9] mv-interp.[1-9] \
	big1.* pmlogger.log

MYCONFIGS = config.foo config.bar config.bigbin \
	config.dodgey-all config.dodgey-mixed config.dodgey-some \
	config1.interp config2.interp

MYSCRIPTS = make.dodgey mkbig1 check-pmcd-stable make.sample-proc

OTHERS = README

ifeq ($(shell test -f ../localconfig && echo 1), 1)
include ../localconfig
else
ifeq ($(shell cd ..; ( $(MAKE) localconfig >/dev/null && echo 1 )), 1)
include ../localconfig
else
$(error Cannot make qa/localconfig)
endif
endif

ifeq ($(shell test $(PCP_VER) -ge 3811 && echo 1), 1)
CONFIG_INTERP = config2.interp
else
CONFIG_INTERP = config1.interp
endif

DEFAULT_ARCHIVE_VERSION=$(shell [ -f /etc/pcp.conf ] && sed -n -e '/^PCP_ARCHIVE_VERSION=/s/.*=//p' /etc/pcp.conf)

#
# rules to make archives
#

foo.0:	config.foo
	rm -f foo.index foo.meta foo.0
	pmlogger -c config.foo -s 8 foo

foo_v2.0:	config.foo
	rm -f foo_v2.index foo_v2.meta foo_v2.0
	pmlogger -V2 -c config.foo -s 8 foo_v2

foo_v3.0:	config.foo
	rm -f foo_v3.index foo_v3.meta foo_v3.0
	pmlogger -V3 -c config.foo -s 8 foo_v3

mv-foo.0:	config.foo
	rm -f mv-foo.index mv-foo.meta mv-foo.?
	pmlogger -c config.foo -s 8 -v 3 mv-foo

noti-foo.0:	foo.0
	cp foo.0 noti-foo.0
	cp foo.meta noti-foo.meta

noti-ok-foo.0:	../archives/ok-foo.0
	cp ../archives/ok-foo.0 noti-ok-foo.0
	cp ../archives/ok-foo.meta noti-ok-foo.meta

# just the label record
empty.0:	foo.0
	rm -f empty.index empty.meta empty.0
	if [ "$(DEFAULT_ARCHIVE_VERSION)" = 3 ]; then \
	    dd ibs=808 count=1 if=foo.0 of=empty.0 ;\
	    dd ibs=808 count=1 if=foo.meta of=empty.meta ;\
	    dd ibs=808 count=1 if=foo.index of=empty.index ;\
	else \
	    dd ibs=132 count=1 if=foo.0 of=empty.0 ;\
	    dd ibs=132 count=1 if=foo.meta of=empty.meta ;\
	    dd ibs=132 count=1 if=foo.index of=empty.index ;\
	fi

bar.0:	config.bar
	rm -f bar.index bar.meta bar.0
	pmlogger -c config.bar -s 70 bar

mv-bar.0:	config.bar
	rm -f mv-bar.index mv-bar.meta mv-bar.?
	pmlogger -c config.bar -s 70 -v 20 mv-bar

noti-bar.0:	bar.0
	cp bar.0 noti-bar.0
	cp bar.meta noti-bar.meta

bigbin.0:	config.bigbin
	rm -f bigbin.index bigbin.meta bigbin.0
	sync
	sleep 3
	pmlogger -c config.bigbin -s 1000 bigbin

mv-bigbin.0:	config.bigbin
	rm -f mv-bigbin.index mv-bigbin.meta mv-bigbin.?
	sync
	sleep 3
	pmlogger -c config.bigbin -s 1000 -v 101 mv-bigbin

noti-bigbin.0:	bigbin.0
	cp bigbin.0 noti-bigbin.0
	cp bigbin.meta noti-bigbin.meta

truncbin.0:	bigbin.0
	rm -f truncbin.index truncbin.meta truncbin.0
	ln bigbin.meta truncbin.meta
	ln bigbin.index truncbin.index
	# V2 len here is label size + 204668 = 200K
	# V3 len here is label size + 204668 + 416*4 (bigger timestamps)
	if [ "$(DEFAULT_ARCHIVE_VERSION)" = 3 ]; then \
	    dd ibs=1 count=207140 if=bigbin.0 of=truncbin.0 ;\
	else \
	    dd ibs=1 count=204800 if=bigbin.0 of=truncbin.0 ;\
	fi

# truncated in the middle of the first pmResult
bad-1.0:	bigbin.0
	rm -f bad-1.index bad-1.meta bad-1.0
	ln foo.meta bad-1.meta
	ln foo.index bad-1.index
	# len here is label size + 8
	if [ "$(DEFAULT_ARCHIVE_VERSION)" = 3 ]; then \
	    dd ibs=1 count=816 if=foo.0 of=bad-1.0;\
	else \
	    dd ibs=1 count=140 if=foo.0 of=bad-1.0;\
	fi

interp.0:	$(CONFIG_INTERP)
	rm -f interp.index interp.meta interp.0
	pmlogger -c $(CONFIG_INTERP) -s 30 interp

mv-interp.0:	$(CONFIG_INTERP)
	rm -f mv-interp.index mv-interp.meta mv-interp.?
	pmlogger -c $(CONFIG_INTERP) -s 30 -v 10 mv-interp

noti-interp.0:	interp.0
	cp interp.0 noti-interp.0
	cp interp.meta noti-interp.meta

# gap: 10 sec data, 5 sec gap, 10 sec data
# gap2: 10 sec data, 5 sec gap, 10 sec data, 5 sec gap, 10 sec data
# sample.bin instances change across gaps
# force <mark> at gaps
#
gap.0 gap2.0:
	rm -rf tmp
	mkdir tmp
	echo 'log mandatory on 1sec { pmcd.pdu_in,pmcd.numagents,hinv.ncpu,sample.bin["bin-100","bin-200","bin-400"] }' >tmp/A.config
	echo 'log mandatory on 1sec { pmcd.pdu_in,pmcd.numagents,hinv.ncpu,sample.bin["bin-100","bin-300","bin-400"] }' >tmp/B.config
	echo 'log mandatory on 1sec { pmcd.pdu_in,pmcd.numagents,hinv.ncpu,sample.bin["bin-100","bin-400"] }' >tmp/C.config
	pmlogger -s 10 -c tmp/A.config tmp/A
	sleep 5
	pmstore pmcd.seqnum 2060
	pmlogger -s 10 -c tmp/B.config tmp/B
	sleep 5
	pmstore pmcd.seqnum 2061
	pmlogger -s 10 -c tmp/C.config tmp/C
	rm -f gap.index gap.meta gap.0
	pmlogextract tmp/A tmp/B gap
	rm -f gap2.index gap2.meta gap2.0
	pmlogextract tmp/A tmp/B tmp/C gap2
	rm -rf tmp

reduce-gap.0:	../archives/reduce-1.0 ../archives/reduce-2.0
	rm -f reduce-gap.*
	pmlogextract ../archives/reduce-1 ../archives/reduce-2 reduce-gap

dodgey-some.0:	make.dodgey config.dodgey-some
	./make.dodgey some 50

dodgey-all.0:	make.dodgey config.dodgey-all
	./make.dodgey all 50

dodgey-mixed.0:	make.dodgey config.dodgey-mixed
	./make.dodgey mixed 150

sample-proc_v2.0 sample-proc_v3.0: make.sample-proc
	./make.sample-proc

sampledso-no-delta_v2.0 sampledso-no-delta_v3.0:
	rm -f sampledso-no-delta_v2.* sampledso-no-delta_v3.*
	rm -rf tmp
	mkdir tmp
	echo "log mandatory on 250msec { sampledso.long.ten sampledso.longlong.ten sampledso.bin }" >tmp/config
	pmlogger -s 3 -c tmp/config -V2 sampledso-no-delta_v2
	pmlogger -s 3 -c tmp/config -V3 sampledso-no-delta_v3
	rm -rf tmp
