# @package      hubzero-submit-distributor
# @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.
#

ETC=$(DESTDIR)/etc
VARLOG=$(DESTDIR)/var/log
OPTSUBMIT=$(DESTDIR)/opt/submit

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

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

BATCHSCHEDULERS = condor ll lsf pbs sge slurm
BATCHSCRIPTS = cleanupjob.sh killbatchjob.sh movepegtmp2home.sh postprocessjob.sh \
               receiveinput.sh submitbatchjob.sh transmitresults.sh
DISTRIBUTORSCRIPTS = genuserid genuserpki update-known-hosts hubmail.py
DISTRIBUTORMAINS = distributor.py distributor.sh jobKill.py jobStatus.py
DISTRIBUTORDFTFILES = appaccess environmentwhitelist managers \
                      monitors sites tools tunnels
PEGASUSLOCALCONFIGS = pegasus_local.rc sites_local.xml


HUBHOSTNAME:=$(shell hostname --fqdn)

all:
	@true

install:
	# create-optsubmit
	install --mode 0755 -d $(OPTSUBMIT)
	# install-distributor-dft: 
	for dftFile in $(DISTRIBUTORDFTFILES) ; do \
		install --mode 0640 -D $${dftFile}.dft $(OPTSUBMIT)/$${dftFile}.dft ; \
	done
	[ -f $(ETC)/submit/distributor.conf ] || \
		install --mode 0644 -D distributor.conf_dft $(ETC)/submit/distributor.conf
	# install-distributor-scripts
	install --mode 2750 -d $(OPTSUBMIT)/Scripts
	for batchScheduler in $(BATCHSCHEDULERS) ; do \
		install --mode 2750 -d $(OPTSUBMIT)/Scripts/$${batchScheduler}; \
		for batchScript in $(BATCHSCRIPTS) ; do \
			install --mode 0755 -D Scripts/$${batchScheduler}/$${batchScript} $(OPTSUBMIT)/Scripts/$${batchScheduler}; \
		done \
	done
	# install-distributor-bin
	install --mode 2751 -d $(OPTSUBMIT)/bin
	for distributorScript in $(DISTRIBUTORSCRIPTS) ; do \
		install --mode 0755 -D bin/$${distributorScript} $(OPTSUBMIT)/bin/$${distributorScript}; \
	done
	# install-distributor-mains
	for distributorMain in $(DISTRIBUTORMAINS) ; do \
		install --mode 0755 -D $${distributorMain} $(OPTSUBMIT)/$${distributorMain}; \
	done
	(cd $(OPTSUBMIT); ln --force --symbolic distributor.py distributor)
	# install-distributor-keys
	install --mode 0751 -d $(OPTSUBMIT)/etc
	install --mode 0751 -d $(OPTSUBMIT)/.ssh
	# install-distributor-condor-scripts:
	install --mode 2751 -d $(OPTSUBMIT)/bin
	for batchScript in $(BATCHSCRIPTS) ; do \
		install --mode 0755 -D Scripts/condor/$${batchScript} $(OPTSUBMIT)/bin/$${batchScript}; \
		sed -e "s/@CONDORVERSION@/$(CONDORVERSION)/" \
			--in-place $(OPTSUBMIT)/bin/$${batchScript}; \
	done
	# install-distributor-logs
	install --mode 0775 -d $(VARLOG)/submit
	install --mode 0755 -d $(VARLOG)/submit/distributor
	# install-pegasus-local: 
	for pegasusLocalConfig in $(PEGASUSLOCALCONFIGS) ; do \
           install --mode 0644 -D $${pegasusLocalConfig} $(OPTSUBMIT)/$${pegasusLocalConfig} ; \
	done
	# add sudoers file
	install --mode 0440 -D submit-distributor.sudoers $(ETC)/sudoers.d/submit-distributor
	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-distrubtor-dat
	for dftFile in $(DISTRIBUTORDFTFILES) ; do \
		[ -e $(OPTSUBMIT)/$${dftFile}.dat ] || install --mode 0644 -D $(OPTSUBMIT)/$${dftFile}.dft $(OPTSUBMIT)/$${dftFile}.dat; \
	done
	sed -e "s/@HUBHOSTNAME@/$(HUBHOSTNAME)/" --in-place $(OPTSUBMIT)/monitors.dat
	# install-distributor-bin
	$(OPTSUBMIT)/bin/update-known-hosts $(HUBHOSTNAME)
	# install-distributor-keys
	if [ -f ${OPTSUBMIT}/.ssh/submit_rsa ] ; then \
            echo "submit RSA key already exists in ${OPTSUBMIT}/.ssh/submit_rsa"; \
        else \
            printf "y\n\n" | ssh-keygen -q -t rsa -f ${OPTSUBMIT}/.ssh/submit_rsa -N '' -C "submit@$(HUBHOSTNAME)"; \
        fi
	chown gridman $(OPTSUBMIT)/.ssh/submit_rsa $(OPTSUBMIT)/.ssh/submit_rsa.pub
	install --mode 0644 -D $(OPTSUBMIT)/.ssh/submit_rsa $(OPTSUBMIT)/etc/submit_rsa
	install -o gridman -g gridman --mode 0700 -d /var/gridman/.ssh
	cat $(OPTSUBMIT)/.ssh/submit_rsa.pub >> /var/gridman/.ssh/authorized_keys
	chown gridman:gridman /var/gridman/.ssh/authorized_keys
	chmod 0600 /var/gridman/.ssh/authorized_keys
	chown gridman:apps $(ETC)/submit/distributor.conf

	touch emptyFile
	[ -f $(VARLOG)/submit/distributor/distributor.log ] || \
		install --mode 0640 -D emptyFile $(VARLOG)/submit/distributor/distributor.log
	rm -f emptyFile

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
