# @package      hubzero-mw2-exec-proxy
# @file         Makefile
# @author       Pascal Meunier <pmeunier@purdue.edu>
# @copyright    Copyright (c) 2016-2017 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
#
# Copyright (c) 2016-2017 HUBzero Foundation, LLC.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#

USRBIN=$(DESTDIR)/usr/bin
USRSBIN=$(DESTDIR)/usr/sbin
USRLIB=$(DESTDIR)/usr/lib
ETCDIR=$(DESTDIR)/etc
USRSHARE=$(DESTDIR)/usr/share
USRSHAREMAN=$(DESTDIR)/usr/share/man
USRSHAREDOC=$(DESTDIR)/usr/share/doc
SBIN=$(DESTDIR)/sbin
LOGDIR=$(DESTDIR)/var/log
USRSHAREMANMAN8=$(DESTDIR)/usr/share/man/man8
USRLOCAL=$(DESTDIR)/usr/local

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

ifeq (/etc/logrotate.hubzero.d,$(wildcard /etc/logrotate.hubzero.d))
  export LOGROTDIR=$(DESTDIR)/etc/logrotate.hubzero.d
else
  export LOGROTDIR=$(DESTDIR)/etc/logrotate.d
endif

# run under RUNUSER instead of running as www-data or root
# owns the log directory
RUNUSER=hz-exec-proxy

# Group ownership is WEBGROUP for the configuration. 
# The middleware must be able to read the configuration  to provide the correct URL to the proxy
# Configuration is initially read by front-proxy as the root user before relinquishing privileges
# so doesn't need to be readable by RUNUSER.  
ifeq ($(DISTRIBUTION),Debian)
    WEBGROUP=www-data
else
    WEBGROUP=apache
endif

HOSTNAME=$(shell hostname -f)
export HOSTNAME

all:
	@true

install:
	install --mode 0750 -d $(ETCDIR)/mw-proxy
	install --mode 0750 -d $(LOGDIR)/exec-proxy
	install --mode 0644 -D logrotate_mw-proxy $(LOGROTDIR)/hubzero-mw2-exec-proxy
	install --mode 0700 -D init_d_exec-proxy $(ETCDIR)/init.d/exec-proxy
	install --mode 0640 -D exec-proxy.conf.template $(ETCDIR)/mw-proxy/exec-proxy.conf
	install --mode 0755 -D exec-proxy.py $(USRBIN)/exec-proxy.py

manual_install:  install postinst
	if [ ! -e $(ETCDIR)/mw-proxy/exec-proxy.key ]; then sh ./make_exec_proxy_cert.sh; fi
	# dhparam for SSL  
	if [ ! -e /etc/mw-proxy/dhparam.pem ]; then \
	  /usr/bin/openssl dhparam -out /etc/mw-proxy/dhparam.pem 2048; \
	fi

uninstall:
	rm -f $(USRBIN)/exec-proxy.py
	rm -f $(LOGROTDIR)/hubzero-mw2-exec-proxy

postinst:
	sed -i "s|HOSTNAME|$(HOSTNAME)|" $(ETCDIR)/mw-proxy/exec-proxy.conf
	if [ ! -h /usr/sbin/adduser ] && [ -f /usr/sbin/adduser ]; then \
		getent passwd $(RUNUSER) >/dev/null || adduser --system --home /var/lib/$(RUNUSER) --quiet --group --gecos "HUBzero Weber Front Proxy Service" $(RUNUSER) ; \
		getent group $(WEBGROUP) >/dev/null || adduser --system --home /var/lib/$(WEBGROUP) --quiet --group --gecos "Web service user" $(WEBGROUP) ; \
	else \
		getent group $(RUNUSER) >/dev/null || groupadd -r $(RUNUSER) ; \
		getent passwd $(RUNUSER) >/dev/null || useradd -r -g $(RUNUSER) -d /var/lib/$(RUNUSER) -s /sbin/nologin \
		-c "HUBzero Weber Proxy Service" $(RUNUSER) ; \
		getent group $(WEBGROUP) >/dev/null || groupadd -r $(WEBGROUP) ; \
	fi
	getent group $(RUNUSER) >/dev/null || exit 1
	install --mode 0750 -o $(RUNUSER) -g $(RUNUSER) -d /var/log/exec-proxy
	chown -R root:$(WEBGROUP) $(ETCDIR)/mw-proxy
	chmod 0750 $(ETCDIR)/mw-proxy
	chown -R $(RUNUSER):$(RUNUSER) $(LOGDIR)/exec-proxy

clean:
	rm -f $(USRBIN)/exec-proxy.py
	rm -f $(LOGROTDIR)/hubzero-mw2-exec-proxy
	rm -f $(ETCDIR)/init.d/exec-proxy

.PHONY: all install uninstall postinst clean
