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

VARCONDORDIR=$(DESTDIR)/var/condor
ETCDIR=$(DESTDIR)/etc

CONDORVERSION := 7.6.6
CONDORTARBALL := condor-7.6.6-x86_64_deb_6.0-updated-stripped.tar.gz
HUBHOSTNAME   := $(shell hostname)
CONDORUID     := $(shell id --user condor 2>/dev/null)
CONDORGID     := $(shell id --group condor 2>/dev/null)
RANDOMLOCKID  := $(shell tr -dc "[:digit:]" < /dev/urandom | head -c 15)

.PHONY: default all build install configure clean 

default all build:
	@true

install:
	install --mode 0755 -d $(VARCONDORDIR)/Condor/$(CONDORVERSION)
	tar -C $(VARCONDORDIR)/Condor/$(CONDORVERSION) --strip-components 1 -xvpf $(CONDORTARBALL) 
	install --mode 0755 -D condor_grid $(ETCDIR)/init.d/condor_grid
	install --mode 0755 -d $(VARCONDORDIR)/Grid/etc
	install --mode 0755 -d $(VARCONDORDIR)/Grid/etc/sysconfig
	install --mode 1777 -d $(VARCONDORDIR)/Grid/execute
	install --mode 1777 -d $(VARCONDORDIR)/Grid/log
	install --mode 0755 -d $(VARCONDORDIR)/Grid/spool
	install --mode 1777 -d $(VARCONDORDIR)/Grid/tmp
	install --mode 0644 -D condor.in $(VARCONDORDIR)/condor.in
	install --mode 0644 -D condor_config.in $(VARCONDORDIR)/condor_config.in
	install --mode 0644 -D condor_config.local.in $(VARCONDORDIR)/condor_config.local.in
	
configure:
	adduser --system --group --force-badname --home /var/condor --shell /bin/bash --disabled-password --gecos Condor condor
	chown condor:condor /var/condor
	chown condor:condor /var/condor/Condor
	chown -R condor:condor /var/condor/Condor/$(CONDORVERSION)
	chown condor:condor /var/condor/Grid/etc
	chown condor:condor /var/condor/Grid/etc/sysconfig
	chown condor:condor /var/condor/Grid/execute
	chown condor:condor /var/condor/Grid/log
	chown condor:condor /var/condor/Grid/spool
	chown condor:condor /var/condor/Grid/tmp
	sed -e "s/@CONDORVERSION@/$(CONDORVERSION)/" /var/condor/condor.in > /var/condor/Grid/etc/sysconfig/condor
	sed -e "s/@CONDORVERSION@/$(CONDORVERSION)/" /var/condor/condor_config.in > /var/condor/Grid/etc/condor_config
	sed -e "s/@CONDORVERSION@/$(CONDORVERSION)/" \
            -e "s/@HUBHOSTNAME@/$(HUBHOSTNAME)/" \
            -e "s/@CONDORUID@/$(CONDORUID)/" \
            -e "s/@CONDORGID@/$(CONDORGID)/" \
            -e "s/@RANDOMLOCKID@/$(RANDOMLOCKID)/" /var/condor/condor_config.local.in > /var/condor/Grid/etc/condor_config.local
	chown condor:condor /var/condor/Grid/etc/sysconfig/condor
	chown condor:condor /var/condor/Grid/etc/condor_config
	chown condor:condor /var/condor/Grid/etc/condor_config.local
	chmod 1777 $(VARCONDORDIR)/Grid/execute
	chmod 1777 $(VARCONDORDIR)/Grid/log
	chmod 1777 $(VARCONDORDIR)/Grid/tmp
	insserv condor_grid
	insserv
	
clean:
	rm -f condor condor_config condor_config.local
	
uninstall:
	rm -fr /var/condor
	deluser condor ||  true
	groupdel condor || true
	