# @package      hubzero-mw2-file-service
# @file         Makefile
# @author       Pascal Meunier <pmeunier@purdue.edu>
# @copyright    Copyright (c) 2016-2017 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Based on template from package hubzero-pkgtemplate
#
# 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.
#

# Common libraries are installed as owned by root so the client can't modify them, as root may need to use them

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

all:
	@true

install:
	install --mode 0755 -D maxwell_fs $(USRBIN)/maxwell_fs
	install --mode 0644 -D logrotate_mw2-file-service $(ETC)/logrotate.d/logrotate_mw2-file-service
	install --mode 0755 -d $(ETC)/mw-service
	install --mode 0640 -D mw-file-service.conf $(ETC)/mw-service/mw-file-service.conf
	install --mode 0644 -D template.environ $(ETC)/mw-service/template.environ
	install --mode 0750 -d $(VARLOG)/mw-service

quota:
	# for manual installation, not part of package install.  Optional or obsolete.
	# Was only called from maxwell_fs.  Don't overwrite quota.conf file if it already exists.
	if [ ! -e $(ETCDIR)/mw-service/quota.conf ]; then \
	  install --owner $(OWNER) --group $(OWNER) --mode 0600 -D quota.conf $(ETC)/mw-service/quota.conf; \
	fi
	install --owner root --group root --mode 0750 -D set_quotas $(USRBIN)/set_quotas

manual_install: install quota postinst
	# additional things to do when not installing as a package and when running make directly
	# symlink from  maxwell_service to maxwell_fs helps when the service check (through the tools web interface)
	# calls the wrong executable.  When that happens it may be a sign that the host provision (host_type) in the database is incorrect
	if [ ! -e $(USRBIN)/maxwell_service ]; then ln $(USRBIN)/maxwell_fs $(USRBIN)/maxwell_service; fi

uninstall:
	rm -f $(USRBIN)/maxwell_fs
	rm -f $(ETC)/logrotate.d/logrotate_mw2-file-service

postinst:
	chown root:adm /var/log/mw-service
	if [ -f /etc/mw-client/notify.key -a ! -f /etc/mw-service/notify.key ]; then \
	  cp /etc/mw-client/notify.key /etc/mw-service/notify.key; \
	fi
	if [ -f /etc/mw-service/notify.key ]; then \
	  chown root:root /etc/mw-service/notify.key; \
	  chmod 0400 /etc/mw-service/notify.key; \
	fi


clean:
	rm -f $(USRBIN)/maxwell_fs
	rm -f $(ETC)/logrotate.d/logrotate_mw2-file-service
	rm -f $(ETC)/mw-service/mw-file-service.conf
	rm -f $(ETC)/mw-service/template.environ

.PHONY: all install uninstall postinst clean
