# @package      hubzero-submit-monitors
# @file         Makefile
# @copyright    Copyright (c) 2010-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-2020 The Regents of the University of California.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

DISTRIBUTION := $(shell lsb_release -i -s)
MAJORVERSION := $(shell lsb_release -rs | cut -d . -f 1)

ifeq ($(DISTRIBUTION),Debian)
il=--install-layout=deb
endif

BATCHMONITORS = monitorBOINC.py monitorCondor.py monitorCondorG.py monitorFactory.py monitorLL.py \
                monitorLSF.py monitorPBS.py monitorSGE.py monitorSLURM.py monitorWF.py
MONITORJOBS = monitorJob.py monitorJobSite.py monitorJobSQL.py monitorJobSiteSQL.py \
              jobCacheSimTools.py jobExecuter.py jobMetrics.py jobProcessor.py jobStageInput.py
MONITORJOBUTILS = monitorJobA.py monitorJobP.py monitorJobQ.py monitorJobR.py \
                  monitorJobS.py monitorJobT.py monitorJobU.py
MONITORTUNNEL = monitorTunnel.py
MONITORTUNNELUTILS = monitorTunnelA.py monitorTunnelD.py monitorTunnelI.py monitorTunnelR.py monitorTunnelT.py
MONITORPROBE = monitorProbe.py
MONITORPROBEUTILS = monitorProbeD.py monitorProbeQ.py monitorProbeR.py monitorProbeU.py
MANAGEIDENTITIES = manageIdentities.py
MANAGEIDENTITYUTILS = manageIdentitiesQ.py
IDENTITYMANAGERSCRIPTS = generate-grid-proxy.sh generate-voms-proxy.sh \
                         genuserid.sh genuserpki.sh \
                         personalize-copy-proxy.sh personalize-saml-proxy.sh \
                         refresh-condor-proxy.sh refresh-jobmon-proxy.sh \
                         refresh-oauth-token.sh writecommunitysecret.sh
SERVICEMONITORS = start status stop
SUBMITDIR=$(DESTDIR)/opt/submit
VARLOGDIR=$(DESTDIR)/var/log
VARGRIDMAN=$(DESTDIR)/var/gridman
ETCDIR=$(DESTDIR)/etc
USRDIR=$(DESTDIR)/usr
USRLIB=$(DESTDIR)/usr/lib

all:
	@true

install:
	# create-optsubmit
	install --mode 0755 -d $(SUBMITDIR)
	# install-batchmonitors
	install --mode 2750 -d $(SUBMITDIR)/BatchMonitors
	for batchMonitor in $(BATCHMONITORS) ; do \
		install --mode 0750 -D BatchMonitors/$${batchMonitor} $(SUBMITDIR)/BatchMonitors/$${batchMonitor}; \
	done
	# install-monitor-job
	for monitorJob in $(MONITORJOBS) ; do \
		install --mode 0750 -D $${monitorJob} $(SUBMITDIR)/$${monitorJob}; \
	done
	for monitorJobUtil in $(MONITORJOBUTILS) ; do \
		install --mode 0750 -D $${monitorJobUtil} $(SUBMITDIR)/$${monitorJobUtil}; \
	done
	# install-monitor-tunnel
	install --mode 0750 -D $(MONITORTUNNEL) $(SUBMITDIR)/$(MONITORTUNNEL); \
	for monitorTunnelUtil in $(MONITORTUNNELUTILS) ; do \
		install --mode 0750 -D $${monitorTunnelUtil} $(SUBMITDIR)/$${monitorTunnelUtil}; \
	done
	# install-monitor-probe
	install --mode 0750 -D $(MONITORPROBE) $(SUBMITDIR)/$(MONITORPROBE); \
	for monitorProbeUtil in $(MONITORPROBEUTILS) ; do \
		install --mode 0750 -D $${monitorProbeUtil} $(SUBMITDIR)/$${monitorProbeUtil}; \
	done
	# install-manage-identities
	install --mode 0750 -D $(MANAGEIDENTITIES) $(SUBMITDIR)/$(MANAGEIDENTITIES); \
	for manageIdentityUtil in $(MANAGEIDENTITYUTILS) ; do \
		install --mode 0750 -D $${manageIdentityUtil} $(SUBMITDIR)/$${manageIdentityUtil}; \
	done
	for identityManagerScript in $(IDENTITYMANAGERSCRIPTS) ; do \
		install --mode 0755 -D bin/$${identityManagerScript} $(VARGRIDMAN)/bin/$${identityManagerScript}; \
	done
	# initialize-monitor-job
	install --mode 0750 -D monitorJobInitialize.py $(SUBMITDIR)/monitorJobInitialize.py
	install --mode 0750 -D monitorJobInitialize.sh $(SUBMITDIR)/monitorJobInitialize.sh
	# install-process-monitors
	for serviceMonitor in $(SERVICEMONITORS) ; do \
		install --mode 0750 -D $${serviceMonitor} $(SUBMITDIR)/$${serviceMonitor}; \
	done
	# install-monitor-logs
	install --mode 0775 -d $(VARLOGDIR)/submit
	install --mode 0755 -d $(VARLOGDIR)/submit/monitors
	# install-server-init
	[ -f $(ETCDIR)/submit/jobmonitors.conf ] || install --mode 0644 -D jobmonitor.conf_dft $(ETCDIR)/submit/jobmonitor.conf
	install --mode 0755 -d $(SUBMITDIR)/systemd
	install --mode 0640 -D cloudmon.service         $(USRLIB)/systemd/system/cloudmon.service
	install --mode 0640 -D identityman.service      $(USRLIB)/systemd/system/identityman.service
	install --mode 0640 -D jobcachesimtools.service $(USRLIB)/systemd/system/jobcachesimtools.service
	install --mode 0640 -D jobexe.service           $(USRLIB)/systemd/system/jobexe.service
	install --mode 0640 -D jobmetrics.service       $(USRLIB)/systemd/system/jobmetrics.service
	install --mode 0640 -D jobmon-sqlite.service    $(USRLIB)/systemd/system/jobmon.service
	install --mode 0640 -D jobpro.service           $(USRLIB)/systemd/system/jobpro.service
	install --mode 0640 -D jobstageinput.service    $(USRLIB)/systemd/system/jobstageinput.service
	install --mode 0640 -D probemon.service         $(USRLIB)/systemd/system/probemon.service
	install --mode 0640 -D tunnelmon.service        $(USRLIB)/systemd/system/tunnelmon.service
	install --mode 0640 -D jobmon-sql.service       $(SUBMITDIR)/systemd/jobmon.service
	python3 setup.py install $(COMPILE) --root $(DESTDIR) $(il)

uninstall:
	rm -f $(USRLIB)/systemd/system/cloudmon.service $(USRLIB)/systemd/system/identityman.service \
         $(USRLIB)/systemd/system/jobcachesimtools.service $(USRLIB)/systemd/system/jobexe.service \
         $(USRLIB)/systemd/system/jobmetrics.service $(USRLIB)/systemd/system/jobmon.service \
         $(USRLIB)/systemd/system/jobpro.service $(USRLIB)/systemd/system/jobstageinput.service \
         $(USRLIB)/systemd/system/probemon.service $(USRLIB)/systemd/system/tunnelmon.service \
	      $(SUBMITDIR)/systemd/jobmon.service

postinst:
	# install-user-gridman
	adduser --quiet --system --group --force-badname --home /var/gridman --shell /bin/bash --disabled-password --gecos Gridman gridman || true
	adduser --quiet gridman tunneler || true

	# install-monitor-job
	chgrp gridman $(SUBMITDIR)/monitorJobInitialize.py
	chgrp gridman $(SUBMITDIR)/monitorJobInitialize.sh
	for monitorJob in $(MONITORJOBS) ; do \
		chgrp gridman $(SUBMITDIR)/$(monitorJob); \
	done
	for monitorJobUtil in $(MONITORJOBUTILS) ; do \
		chgrp gridman $(SUBMITDIR)/$(monitorJobUtil); \
	done
	chown gridman:gridman $(ETCDIR)/submit/jobmonitor.conf
	# install-monitor-tunnel
	chgrp gridman $(SUBMITDIR)/$(MONITORTUNNEL)
	for monitorTunnelUtil in $(MONITORTUNNELUTILS) ; do \
		chgrp gridman $(SUBMITDIR)/$(monitorTunnelUtil); \
	done
	# install-monitor-probe
	chgrp gridman $(SUBMITDIR)/$(MONITORPROBE)
	for monitorProbeUtil in $(MONITORPROBEUTILS) ; do \
		chgrp gridman $(SUBMITDIR)/$(monitorProbeUtil); \
	done
	# install-manage-identities
	chgrp gridman $(SUBMITDIR)/$(MANAGEIDENTITIES)
	for manageIdentityUtil in $(MANAGEIDENTITYUTILS) ; do \
		chgrp gridman $(SUBMITDIR)/$(manageIdentityUtil); \
	done
	# install-process-monitors
	for serviceMonitor in $(SERVICEMONITORS) ; do \
		chgrp gridman $(SUBMITDIR)/$(serviceMonitor); \
	done
	# install-monitor-logs
	if [ ! -d ${VARLOG}/submit ] ; \
	then \
		mkdir --mode=0775 ${VARLOG}/submit ; \
	fi
	if [ ! -d ${VARLOG}/submit/monitors ] ; \
	then \
		mkdir --mode=0755 ${VARLOG}/submit/monitors ; \
		chown gridman ${VARLOG}/submit/monitors ; \
	fi
	if [ ! -f ${VARLOG}/submit/monitors/monitorJob.log ] ; \
	then \
		touch ${VARLOG}/submit/monitors/monitorJob.log ; \
		chmod 0644 ${VARLOG}/submit/monitors/monitorJob.log ; \
		chown gridman:gridman ${VARLOG}/submit/monitors/monitorJob.log ; \
	fi
	if [ ! -f ${VARLOG}/submit/monitors/monitorJobSite.log ] ; \
	then \
		touch ${VARLOG}/submit/monitors/monitorJobSite.log ; \
		chmod 0644 ${VARLOG}/submit/monitors/monitorJobSite.log ; \
		chown gridman:gridman ${VARLOG}/submit/monitors/monitorJobSite.log ; \
	fi
	if [ ! -f ${VARLOG}/submit/monitors/monitorJobSQL.log ] ; \
	then \
		touch ${VARLOG}/submit/monitors/monitorJobSQL.log ; \
		chmod 0644 ${VARLOG}/submit/monitors/monitorJobSQL.log ; \
		chown gridman:gridman ${VARLOG}/submit/monitors/monitorJobSQL.log ; \
	fi
	if [ ! -f ${VARLOG}/submit/monitors/monitorJobSiteSQL.log ] ; \
	then \
		touch ${VARLOG}/submit/monitors/monitorJobSiteSQL.log ; \
		chmod 0644 ${VARLOG}/submit/monitors/monitorJobSiteSQL.log ; \
		chown gridman:gridman ${VARLOG}/submit/monitors/monitorJobSiteSQL.log ; \
	fi
	if [ ! -f ${VARLOG}/submit/monitors/jobCacheSimTools.log ] ; \
	then \
		touch ${VARLOG}/submit/monitors/jobCacheSimTools.log ; \
		chmod 0644 ${VARLOG}/submit/monitors/jobCacheSimTools.log ; \
		chown gridman:gridman ${VARLOG}/submit/monitors/jobCacheSimTools.log ; \
	fi
	if [ ! -f ${VARLOG}/submit/monitors/jobExecuter.log ] ; \
	then \
		touch ${VARLOG}/submit/monitors/jobExecuter.log ; \
		chmod 0644 ${VARLOG}/submit/monitors/jobExecuter.log ; \
		chown gridman:gridman ${VARLOG}/submit/monitors/jobExecuter.log ; \
	fi
	if [ ! -f ${VARLOG}/submit/monitors/jobMetrics.log ] ; \
	then \
		touch ${VARLOG}/submit/monitors/jobMetrics.log ; \
		chmod 0644 ${VARLOG}/submit/monitors/jobMetrics.log ; \
		chown gridman:gridman ${VARLOG}/submit/monitors/jobMetrics.log ; \
	fi
	if [ ! -f ${VARLOG}/submit/monitors/jobProcessor.log ] ; \
	then \
		touch ${VARLOG}/submit/monitors/jobProcessor.log ; \
		chmod 0644 ${VARLOG}/submit/monitors/jobProcessor.log ; \
		chown gridman:gridman ${VARLOG}/submit/monitors/jobProcessor.log ; \
	fi
	if [ ! -f ${VARLOG}/submit/monitors/jobStageInput.log ] ; \
	then \
		touch ${VARLOG}/submit/monitors/jobStageInput.log ; \
		chmod 0644 ${VARLOG}/submit/monitors/jobStageInput.log ; \
		chown gridman:gridman ${VARLOG}/submit/monitors/jobStageInput.log ; \
	fi

	if [ ! -f ${VARLOG}/submit/monitors/monitorTunnel.log ] ; \
	then \
		touch ${VARLOG}/submit/monitors/monitorTunnel.log ; \
		chmod 0644 ${VARLOG}/submit/monitors/monitorTunnel.log ; \
		chown gridman:gridman ${VARLOG}/submit/monitors/monitorTunnel.log ; \
	fi

	# initialize-monitor-job:
	$(SUBMITDIR)/monitorJobInitialize.sh

	# install-server-init
	rm -f $(ETCDIR)/init.d/identityman $(ETCDIR)/init.d/jobcachesimtools \
         $(ETCDIR)/init.d/jobexe $(ETCDIR)/init.d/jobmetrics \
         $(ETCDIR)/init.d/jobmon  $(ETCDIR)/init.d/jobpro \
         $(ETCDIR)/init.d/jobstageinput $(ETCDIR)/init.d/tunnelmon \
         $(ETCDIR)/init.d/probemon $(ETCDIR)/init.d/cloudmon
	systmctl daemon-reload

clean:
	find . -name '*.pyc' -delete
	rm -f build-stamp
	rm -f configure-stamp
	python setup.py clean
	rm -fr build

.PHONY: all install uninstall postinst clean
