# @package      hubzero-submit-monitors
# @file         Makefile
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2010-2014 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2010-2014 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#

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

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

BATCHMONITORS = monitorCondor.py monitorCondorG.py monitorFactory.py monitorLL.py \
                monitorLSF.py monitorPBS.py monitorSGE.py monitorSLURM.py monitorWF.py
MONITORJOB = monitorJob.py
MONITORJOBUTILS = 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 = monitorProbeQ.py monitorProbeR.py monitorProbeU.py
SUBMITDIR=$(DESTDIR)/opt/submit
VARLOGDIR=$(DESTDIR)/var/log
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
	install --mode 0750 -D $(MONITORJOB) $(SUBMITDIR)/$(MONITORJOB);
	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
	# 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 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
	chown gridman $(SUBMITDIR)/$(MONITORJOB)
	# install-monitor-tunnel
	chown gridman $(SUBMITDIR)/$(MONITORTUNNEL)
	# install-monitor-probe
	chown gridman $(SUBMITDIR)/$(MONITORPROBE)
	# 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 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
