# @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
#
# Packaging of original work by Richard L. Kennell
#
# 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

ifeq ($(DISTRIBUTION),Debian)
    WEBUSER=www-data
else
    WEBUSER=apache
endif

HOSTNAME=$(shell hostname -f)
export HOSTNAME

all:
	@true

install:
	install --mode 0750 -d $(ETCDIR)/mw-proxy
	install --mode 0750 -d $(LOGDIR)/mw-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
	if [ ! -e $(ETCDIR)/mw-proxy/exec-proxy.key ]; then sh ./make_exec_proxy_cert.sh; 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

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
