# @package      hubzero-submit-monitors
# @file         Makefile
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2010-2018 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-2018 HUBzero Foundation, LLC.
#
# 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 HUBzero Foundation, LLC.
#

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
MONITORJOBUTILS = monitorJobA.py monitorJobQ.py monitorJobP.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 = 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 \
                         writecommunitysecret.sh
SUBMITDIR=$(DESTDIR)/opt/submit
VARLOGDIR=$(DESTDIR)/var/log
VARGRIDMAN=$(DESTDIR)/var/gridman
ETCDIR=$(DESTDIR)/etc

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-monitor-logs
	install --mode 0775 -d $(VARLOGDIR)/submit
	install --mode 0755 -d $(VARLOGDIR)/submit/monitors
	# install-server-init
	install --mode 0755 -D identityman $(ETCDIR)/init.d/identityman
	install --mode 0755 -D jobmon $(ETCDIR)/init.d/jobmon
	install --mode 0755 -D tunnelmon $(ETCDIR)/init.d/tunnelmon 
	install --mode 0755 -D probemon $(ETCDIR)/init.d/probemon 
	python setup.py install $(COMPILE) --root $(DESTDIR) $(il)

uninstall:
	@true

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
	for monitorJob in $(MONITORJOBS) ; do \
		chown gridman $(SUBMITDIR)/$(monitorJob); \
	done
	# install-monitor-tunnel
	chown gridman $(SUBMITDIR)/$(MONITORTUNNEL)
	# install-monitor-probe
	chown gridman $(SUBMITDIR)/$(MONITORPROBE)
	# install-manage-identities
	chown gridman $(SUBMITDIR)/$(MANAGEIDENTITIES)
	# 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/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
	insserv probemon
	insserv identityman
	insserv jobmon
	insserv tunnelmon
	insserv
	
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
