# for syntax checks
BASH_SCRIPTS = \
	kamcmd/memdbg \
	libexec/ngcp-apt-hint-helper \
	libexec/ngcp-crm-helper \
	libexec/ngcp-eaddress-helper \
	libexec/ngcp-glusterd-waiter \
	libexec/ngcp-ha-await-stable-status \
	libexec/ngcp-kannel-bearerbox-waiter \
	libexec/ngcp-kernel-helper \
	libexec/ngcp-openipmi-helper \
	libexec/ngcp-policy-rc.d \
	libexec/ngcp-sync-snmpd-creds \
	libexec/ngcp-wait-pingnodes \
	ngcp-approx-cache \
	ngcp-approx-snapshots \
	ngcp-check-active \
	ngcp-check-cluster-ssh \
	ngcp-check-cluster-ping \
	ngcp-check-ssh-keys \
	ngcp-cluster-status \
	ngcp-coredump-analyzer \
	ngcp-eaddress \
	ngcp-ha-clear-failcounts \
	ngcp-ha-crm \
	ngcp-ha-crm-reload \
	ngcp-ha-host-state \
	ngcp-ha-proc-state \
	ngcp-ha-show-failcounts \
	ngcp-initialize-udev-rules-net \
	ngcp-io-scheduler \
	ngcp-iptables-apply \
	ngcp-kam-parallel-xmlrpc \
	ngcp-kamcmd \
	ngcp-kamctl \
	ngcp-logs \
	ngcp-make-active \
	ngcp-make-standby \
	ngcp-mariadb-replication-check \
	ngcp-mysql-central \
	ngcp-mysql-local \
	ngcp-mysql-pair \
	ngcp-hostname \
	ngcp-nodename \
	ngcp-parallel-scp \
	ngcp-parallel-ssh \
	ngcp-ppa \
	ngcp-prepare-translations \
	ngcp-reset-db \
	ngcp-site \
	ngcp-ssh \
	ngcp-sync-db-wrapper \
	ngcp-toggle-performance-config \
	ngcp-type \
	ngcp-update \
	ngcp-usr-location \
	ngcp-virt-identify \
	# EOL
PERL_SCRIPTS = \
	lib/NGCP/Config.pm \
	lib/NGCP/Daemon.pm \
	lib/NGCP/Log.pm \
	lib/NGCP/Log/Functions.pm \
	lib/NGCP/Service/IO.pm \
	lib/NGCP/Service/Meta.pm \
	ngcp-active-calls \
	ngcp-check-rev-applied \
	ngcp-location-cleanup \
	ngcp-memdbg-csv \
	ngcp-migrate-openvpn-certs \
	ngcp-service \
	ngcp-snmpd-adduser \
	ngcp-snmpd-deluser \
	ngcp-sync-db \
	# EOL
PROGRAMS = \
	ngcp-chroot-shell \
	# EOL
CONFIGS = \
	build-ce/ngcp-logs.conf \
	build-pro/ngcp-logs.conf \
	# EOL
POD_MANS = \
	ngcp-active-calls.8 \
	ngcp-check-active.8 \
	ngcp-check-cluster-ssh.8 \
	ngcp-check-cluster-ping.8 \
	ngcp-check-rev-applied.1 \
	ngcp-check-ssh-keys.8 \
	ngcp-cluster-status.8 \
	ngcp-coredump-analyzer.8 \
	ngcp-ha-clear-failcounts.8 \
	ngcp-ha-crm-reload.8 \
	ngcp-ha-crm.8 \
	ngcp-ha-host-state.8 \
	ngcp-ha-proc-state.8 \
	ngcp-ha-show-failcounts.8 \
	ngcp-keydb-backup.8 \
	ngcp-location-cleanup.8 \
	ngcp-logs.8 \
	ngcp-make-active.8 \
	ngcp-make-standby.8 \
	ngcp-migrate-openvpn-certs.8 \
	ngcp-nodename.8 \
	ngcp-service.8 \
	ngcp-site.8 \
	ngcp-snmpd-adduser.8 \
	ngcp-snmpd-deluser.8 \
	ngcp-sync-db.8 \
	ngcp-type.8 \
	ngcp-virt-identify.8 \
	# EOL
LINK_MANS = \
	ngcp-redis-migrate.8 \
	# EOL

RESULTS ?= reports

CFLAGS ?= -ggdb -Wall -Wextra -Wno-unused-parameter

all: man build

%.8: %.man
	cp $< $@

%.8: %.pod
	pod2man --section=8 $< >$@

%.1: %
	pod2man --section=1 $< >$@

%.8: %
	pod2man --section=8 $< >$@

man: $(POD_MANS) $(LINK_MANS)

build-ce build-pro:
	mkdir -p $@

build-ce/%: % build-ce
	tpage $< >$@

build-pro/%: % build-pro
	tpage --define PRO=true  $< >$@

build: $(PROGRAMS) $(CONFIGS)

clean:
	rm -rf \
	  build-*/ \
	  $(PROGRAMS) \
	  $(POD_MANS) \
	  $(LINK_MANS) \
	  tests/etc/init.d/* tests/run/systemd/* \
	  tests/bin/* tests/usr/bin/* tests/usr/sbin/* \
	  tests/etc/ngcp-service/* tests/*pyc tests/__pycache__/

dist-clean: clean
	rm -rf RESULTS

test_perl_modules:
	mkdir -p ${RESULTS}
	prove --timer --formatter TAP::Formatter::JUnit -l -b -Ilib | \
	  tee -a ${RESULTS}/$@.xml

test_ngcp-site: tests/ngcp-site.py
	mkdir -p ${RESULTS}
	py.test-3  --junit-xml=${RESULTS}/$@.xml $<

test_ngcp-service: tests/ngcp-service.py
	mkdir -p ${RESULTS}
	py.test-3  --junit-xml=${RESULTS}/$@.xml $<

test_ngcp-kamcmd: tests/ngcp-kamcmd.py
	rm -rf ${RESULTS}/tmp.$(@)
	mkdir -p ${RESULTS}/tmp.$(@)
	WORKSPACE=${RESULTS}/tmp.$(@) py.test-3 --junit-xml=${RESULTS}/$@.xml $<

test_ngcp-memdbg-csv: tests/ngcp-memdbg-csv.py
	rm -rf ${RESULTS}/tmp.$(@)
	mkdir -p ${RESULTS}/tmp.$(@)
	WORKSPACE=${RESULTS}/tmp.$(@) py.test-3 --junit-xml=${RESULTS}/$@.xml $<

test_ngcp-debug-subscriber: tests/ngcp-debug-subscriber.py
	mkdir -p ${RESULTS}
	-pkill -f "./tests/fake_redis.py"
	./tests/fake_redis.py & echo $$! > ${RESULTS}/fake_pid.PID
	-timeout 15 py.test-3 --junit-xml=${RESULTS}/$@.xml $<
	if [ -e ${RESULTS}/fake_pid.PID ]; then \
		kill -TERM $$(cat ${RESULTS}/fake_pid.PID) || true; \
	fi;

test_ngcp-check-rev-applied: tests/ngcp-check-rev-applied.py
	mkdir -p ${RESULTS}
	py.test-3  --junit-xml=${RESULTS}/$@.xml $<

test_ngcp-reset-db: tests/ngcp-reset-db.py
	rm -rf ${RESULTS}/tmp.$(@)
	mkdir -p ${RESULTS}/tmp.$(@)
	WORKSPACE=${RESULTS}/tmp.$(@) py.test-3 --junit-xml=${RESULTS}/$@.xml $<

UNIT_TESTS = \
	test_perl_modules \
	test_ngcp-check-rev-applied \
	test_ngcp-debug-subscriber \
	test_ngcp-kamcmd \
	test_ngcp-reset-db \
	test_ngcp-memdbg-csv \
	test_ngcp-service \
	test_ngcp-site \
	# EOL

unittest: $(UNIT_TESTS)

# check for syntax errors
syntaxcheck: shellcheck perlcheck podcheck

shellcheck:
	@echo "Checking for shell syntax errors: "; \
	RC=0; \
	for SCRIPT in $(BASH_SCRIPTS); do \
	  echo -n "  $${SCRIPT}: "; \
	  if ! [ -r "$${SCRIPT}" ]; then \
	    echo "FAIL (missing)"; RC=1; \
	  elif ! bash -n $${SCRIPT}; then \
	    echo "FAIL (syntax)"; RC=1; \
	  else \
	    echo "PASS"; \
	  fi; \
	done; \
	echo "done."; \
	exit $${RC}

perlcheck:
	@echo "Checking for perl syntax errors: "; \
	RC=0; \
	for SCRIPT in $(PERL_SCRIPTS); do \
	  echo -n "  $${SCRIPT}: "; \
	  if ! [ -r "$${SCRIPT}" ]; then \
	    echo "FAIL (missing)"; RC=1; \
	  elif ! perl -Ilib -CSD -w -c $${SCRIPT}; then \
	    echo "FAIL (syntax)"; RC=1; \
	  fi; \
	done; \
	echo "done."; \
	exit $${RC}

podcheck:
	@echo "Checking for POD syntax errors: "; \
	RC=0; \
	for MAN in $(POD_MANS); do \
	  echo -n "  $${MAN}: "; \
	  POD="$${MAN%.*}"; \
	  if [ -r "$${POD}.pod" ]; then \
	    POD="$${POD}.pod"; \
	  fi; \
	  if ! [ -r "$${POD}" ]; then \
	    echo "FAIL (missing)"; RC=1; \
	  elif ! podchecker -warnings -warnings $${POD}; then \
	    echo "FAIL (syntax)"; RC=1; \
	  fi; \
	done; \
	echo "done."; \
	exit $${RC}

# EOF
