#!gmake
#
# Copyright (c) 2015-2019 Red Hat.
# Copyright (c) 1997-2002 Silicon Graphics, Inc.  All Rights Reserved.
#
# NB: this file is renamed GNUmakefile upon installation into $PCP_VAR_DIR/testsuite.
# It is only for executing the testsuite.

ifdef PCP_CONF
include $(PCP_CONF)
else
include $(PCP_DIR)/etc/pcp.conf
endif
PATH	= $(shell . $(PCP_DIR)/etc/pcp.env; echo $$PATH)
include $(PCP_INC_DIR)/builddefs

SUBDIRS = src pmdas cisco gluster pconf sadist collectl nfsclient named \
	  archives badarchives views qt linux unbound cifs gpfs lustre ganglia \
	  java mmv postfix perl json slurm tmparch sheet smart hacluster denki
ifeq "$(PMDA_PERFEVENT)" "true"
SUBDIRS += perfevent
endif
ifeq "$(HAVE_PYTHON)" "true"
SUBDIRS += secure mic haproxy lio openmetrics
endif

# Before installing the pcpqa policy module we need a PCP build with
# ENABLE_SELINUX set and we need the current SELinux mode to be
# Enforcing or Permissive (not Disabled), and the latest pcp policy
# module needs to be loaded.
#
SELINUX_PCPQA =
ifeq "$(ENABLE_SELINUX)" "true"
ifneq "$(shell getenforce)" "Disabled"
ifeq "$(shell if sudo semodule -lfull | grep '^200 pcp[ 	]*pp$$' >/dev/null; then echo OK; fi)" "OK"
SELINUX_PCPQA = pcpqa.installed
endif
endif
endif

default default_pcp setup: $(SUBDIRS) localconfig qa_hosts $(SELINUX_PCPQA)
	$(QA_SUBDIRS_MAKERULE)

LDIRT += 051.work 134.full.* \
         *.bak *.bad *.core *.full *.notrun *.raw *.o core a.out core.* \
	 *.log eek* urk* so_locations tmp.* gmon.out oss.qa.tar.gz \
	 *.full.ok *.new rc_cron_check.clean \
	 make.out qa_hosts localconfig localconfig.h check.time \
	 $(SELINUX_PCPQA)
	# these ones are links to the real files created when the associated
	# test is run
LDIRT += $(shell [ -f .gitignore ] && grep '\.out$$' .gitignore)

qa_hosts:	qa_hosts.primary mk.qa_hosts
	PATH=$(PATH); ./mk.qa_hosts

localconfig:
	PATH=$(PATH); ./mk.localconfig

install_pcp install: 
	@echo The PCP QA testsuite is already installed.
	false

include $(BUILDRULES)

ifeq "$(ENABLE_SELINUX)" "true"
pcpqa.installed: pcp-testsuite.pp.bz2
	if sudo semodule -lfull | grep '^200 pcp-testsuite[ 	]*pp$$' >/dev/null; then sudo semodule -r pcp-testsuite || true; fi
	sudo semodule -X 200 -i pcp-testsuite.pp
	touch pcpqa.installed

pcp-testsuite.pp: pcp-testsuite.te
	make -f /usr/share/selinux/devel/Makefile pcp-testsuite.pp
	@# cleanup temp files from selinux Makefile
	@rm -f ./tmp/all_interfaces.conf ./tmp/iferror.m4 ./tmp/pcp-testsuite.tmp

pcp-testsuite.pp.bz2: pcp-testsuite.pp
	$(BZIP2) -9 < $^ > $@
endif
