#
# @package      hubzero-submit-distributor
# @file         Makefile
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2010-2013 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2010-2013 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.
#

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
DISTRIBUTORDFTFILES = appaccess environmentwhitelist managers \
                      monitors sites tools tunnels
PEGASUSLOCALCONFIGS = pegasus_local.rc sites_local.xml

SUBMITDIR=$(DESTDIR)/opt/submit
VARLOGDIR=$(DESTDIR)/var/log
ETCDIR=$(DESTDIR)/etc

HUBHOSTNAME:=$(shell hostname)

default all build:
	@true

install:
	# create-optsubmit
	install --mode 0755 -d $(SUBMITDIR)
	# install-distributor-dft: 
	for dftFile in $(DISTRIBUTORDFTFILES) ; do \
		install --mode 0640 -D $${dftFile}.dft $(SUBMITDIR)/$${dftFile}.dft ; \
	done
	[ -f $(ETCDIR)/submit/distributor.conf ] || \
		install --mode 0644 -D distributor.conf_dft $(ETCDIR)/submit/distributor.conf
	# install-distributor-scripts
	install --mode 2750 -d $(SUBMITDIR)/Scripts
	for batchScheduler in $(BATCHSCHEDULERS) ; do \
		install --mode 2750 -d $(SUBMITDIR)/Scripts/$${batchScheduler}; \
		for batchScript in $(BATCHSCRIPTS) ; do \
			install --mode 0755 -D Scripts/$${batchScheduler}/$${batchScript} $(SUBMITDIR)/Scripts/$${batchScheduler}; \
		done \
	done
	# install-distributor-bin
	install --mode 2751 -d $(SUBMITDIR)/bin
	for distributorScript in $(DISTRIBUTORSCRIPTS) ; do \
		install --mode 0755 -D bin/$${distributorScript} $(SUBMITDIR)/bin/$${distributorScript}; \
	done
	# install-distributor-mains
	for distributorMain in $(DISTRIBUTORMAINS) ; do \
		install --mode 0755 -D $${distributorMain} $(SUBMITDIR)/$${distributorMain}; \
	done
	(cd $(SUBMITDIR); ln --force --symbolic distributor.py distributor)
	# install-distributor-keys
	install --mode 0751 -d $(SUBMITDIR)/etc
	install --mode 0751 -d $(SUBMITDIR)/.ssh
	# install-distributor-condor-scripts:
	install --mode 2751 -d $(SUBMITDIR)/bin
	for batchScript in $(BATCHSCRIPTS) ; do \
		install --mode 0755 -D Scripts/condor/$${batchScript} $(SUBMITDIR)/bin/$${batchScript}; \
		sed -e "s/@CONDORVERSION@/$(CONDORVERSION)/" \
			--in-place $(SUBMITDIR)/bin/$${batchScript}; \
	done
	# install-distributor-logs
	install --mode 0775 -d $(VARLOGDIR)/submit
	install --mode 0755 -d $(VARLOGDIR)/submit/distributor
	touch emptyFile
	[ -f $(VARLOGDIR)/submit/distributor/distributor.log ] || \
		install --mode 0666 -D emptyFile $(VARLOGDIR)/submit/distributor/distributor.log
	[ -f $(VARLOGDIR)/submit/distributor/gridjobhistory.log ] || \
		install --mode 0666 -D emptyFile $(VARLOGDIR)/submit/distributor/gridjobhistory.log
	[ -f $(VARLOGDIR)/submit/distributor/gridjobid.log ] || \
		install --mode 0666 -D emptyFile $(VARLOGDIR)/submit/distributor/gridjobid.log
	rm -f emptyFile
	# install-pegasus-local: 
	for pegasusLocalConfig in $(PEGASUSLOCALCONFIGS) ; do \
           install --mode 0644 -D $${pegasusLocalConfig} $(SUBMITDIR)/$${pegasysLocalConfig} ; \
	done
	# add sudoers file
	install --mode 0440 -D submit-distributor.sudoers $(ETCDIR)/sudoers.d/submit-distributor
	python setup.py install $(COMPILE) --root $(DESTDIR)  --install-layout=deb

configure:
	# 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 $(SUBMITDIR)/$${dftFile}.dat ] || install --mode 0644 -D $(SUBMITDIR)/$${dftFile}.dft $(SUBMITDIR)/$${dftFile}.dat; \
	done
	sed -e "s/@HUBHOSTNAME@/$(HUBHOSTNAME)/" --in-place $(SUBMITDIR)/monitors.dat
	# install-distributor-bin
	$(SUBMITDIR)/bin/update-known-hosts $(HUBHOSTNAME)
	# install-distributor-keys
	if [ -f ${SUBMITDIR}/.ssh/submit_rsa ] ; then \
            echo "submit RSA key already exists in ${SUBMITDIR}/.ssh/submit_rsa"; \
        else \
            printf "y\n\n" | ssh-keygen -q -t rsa -f ${SUBMITDIR}/.ssh/submit_rsa -N '' -C "submit@$(HUBHOSTNAME)"; \
        fi
	chown gridman $(SUBMITDIR)/.ssh/submit_rsa $(SUBMITDIR)/.ssh/submit_rsa.pub
	install --mode 0644 -D $(SUBMITDIR)/.ssh/submit_rsa $(SUBMITDIR)/etc/submit_rsa
	install -o gridman -g gridman --mode 0700 -d /var/gridman/.ssh
	cat $(SUBMITDIR)/.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 $(ETCDIR)/submit/distributor.conf


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

