# @package      hubzero-submit-condor
# @file         Makefile
# @copyright    Copyright (c) 2010-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/Apache-2.0 Apache2
#
# Copyright (c) 2010-2020 The Regents of the University of California.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

ETC=$(DESTDIR)/etc
VARCONDOR=$(DESTDIR)/var/condor

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

CONDORVERSION := 8.6.10
HUBHOSTNAME   := $(shell hostname --fqdn)
RANDOMLOCKID  := $(shell tr -dc "[:digit:]" < /dev/urandom | head -c 15)

ifeq ($(DISTRIBUTION),Debian)
# We don't have a binary for 8.6.10 for Debian 9, for now we cheat a little
# here.
ifeq ($(MAJORVERSION),9)
CONDORTARBALL := condor-8.6.13-x86_64_Debian9-stripped.tar.gz 
else
CONDORTARBALL := condor-$(CONDORVERSION)-x86_64_Debian$(MAJORVERSION)-stripped.tar.gz 
endif
else
CONDORTARBALL := condor-$(CONDORVERSION)-x86_64_RedHat$(MAJORVERSION)-stripped.tar.gz 
endif

all:
	@true

install:
	install --mode 0755 -d $(VARCONDOR)/Condor/$(CONDORVERSION)
	tar -C $(VARCONDOR)/Condor/$(CONDORVERSION) --strip-components 1 -xvpf $(CONDORTARBALL) 
	install --mode 0755 -D condor_grid $(ETC)/init.d/condor_grid
	install --mode 0755 -d $(VARCONDOR)/Grid/etc
	install --mode 0755 -d $(VARCONDOR)/Grid/etc/sysconfig
	install --mode 1777 -d $(VARCONDOR)/Grid/execute
	install --mode 1777 -d $(VARCONDOR)/Grid/log
	install --mode 0755 -d $(VARCONDOR)/Grid/spool
	install --mode 1777 -d $(VARCONDOR)/Grid/tmp
	install --mode 0644 -D condor.in $(VARCONDOR)/condor.in
	install --mode 0644 -D condor_config.in $(VARCONDOR)/condor_config.in
	install --mode 0644 -D condor_config.local.in $(VARCONDOR)/condor_config.local.in

uninstall:
	rm -fr $(VARCONDOR)
	rm -fr $(ETC)/init.d/condor_grid

postrm:
	deluser condor ||  true
	groupdel condor || true

postinst:
	(getent passwd condor > /dev/null 2>&1) || adduser --system --group --force-badname --home /var/condor \
                                                      --shell /bin/bash --disabled-password --gecos Condor condor
	$(eval CONDORUID := $(shell id --user  condor 2> /dev/null))
	$(eval CONDORGID := $(shell id --group condor 2> /dev/null))
	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
	if [ ! -f /var/condor/Grid/etc/condor_config ]; then \
	sed -e "s/@CONDORVERSION@/$(CONDORVERSION)/" /var/condor/condor_config.in > /var/condor/Grid/etc/condor_config ; \
	else \
	sed -e "s/8.2.9/$(CONDORVERSION)/" \
	    -e "s/8.0.7/$(CONDORVERSION)/" \
            -e "s/8.0.6/$(CONDORVERSION)/" \
            -e "s/7.6.6/$(CONDORVERSION)/" --in-place /var/condor/Grid/etc/condor_config ; \
	fi
	if [ ! -f /var/condor/Grid/etc/condor_config.local ]; then \
	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 ; \
	else \
	sed -e "s/8.2.9/$(CONDORVERSION)/" \
	    -e "s/8.0.7/$(CONDORVERSION)/" \
            -e "s/8.0.6/$(CONDORVERSION)/" \
            -e "s/7.6.6/$(CONDORVERSION)/" --in-place /var/condor/Grid/etc/condor_config.local ; \
	fi
	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

.PHONY: all install uninstall postinst clean
