# @package      hubzero-mw2-exec-service
# @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.
#

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

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

# @TODO Don't rely on link between /var/lib/vz and /vz
DISTRIBUTION:=$(shell lsb_release -i -s)
ifeq ($(DISTRIBUTION),Debian)
VZ=$(DESTDIR)/var/lib/vz
VZLINK=/var/lib/vz $(DESTDIR)/vz
else
VZ=$(DESTDIR)/vz
VZLINK=/vz $(DESTDIR)/var/lib/vz
endif

all:
	@true

install:
	install --mode 0755 -D maxwell_service $(USRBIN)/maxwell_service
	install --mode 0755 -D firewall_readd $(USRBIN)/firewall_readd
	install --mode 0755 -D mkvztemplate $(USRBIN)/mkvztemplate
	install --mode 0755 -D make_xvnc_cert.sh $(USRBIN)/make_xvnc_cert.sh
	install --mode 0644 -D logrotate_mw2-exec-service $(ETC)/logrotate.d/logrotate_mw2-exec-service
	install --mode 0640 -D mw-exec-service.conf $(ETC)/mw-service/exec.conf
	install --mode 0600 -D hub-session.conf $(ETC)/vz/conf/hub-session.conf
	# mount and umount scripts must be executable by root (0700)
	install --mode 0700 -D hub-session.mount $(ETC)/vz/conf/hub-session.mount
	install --mode 0700 -D hub-session.umount $(ETC)/vz/conf/hub-session.umount
	install --mode 0750 -d $(VARLOG)/mw-service
	install --mode 0750 -d $(VARLOG)/mw-service/open-sessions
	install --mode 0750 -d $(VZ)/lock
	ln -s $(VZLINK)

manual_install:  install
	if [ ! -d $(VZ)/template/debian-7.0-amd64-maxwell ]; then \
	  $(USRBIN)/mkvztemplate amd64 wheezy ellie; \
	fi
	if [ -e $(ETC)/mw-service/notify.key ]; then chmod 0600 $(ETC)/mw-service/notify.key; fi

uninstall:
	rm -f $(USRBIN)/maxwell_service
	rm -f $(USRBIN)/firewall_readd
	rm -f $(ETC)/logrotate.d/logrotate_mw2-exec-service

postinst:
	chown root:adm /var/log/mw-service
	chown root:adm /var/log/mw-service/open-sessions
	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 0600 /etc/mw-service/notify.key \
	fi

clean:
	rm -f $(USRBIN)/maxwell_service
	rm -f $(USRBIN)/firewall_readd
	rm -f $(ETC)/logrotate.d/logrotate_mw2-exec-service

.PHONY: all install uninstall postinst clean
