# @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
VARLIB=$(DESTDIR)/var/lib
USRSHAREMANMAN8=$(DESTDIR)/usr/share/man/man8
USRLOCAL=$(DESTDIR)/usr/local
ROOT=$(DESTDIR)/

# @TODO Don't rely on link between /var/lib/vz and /vz
DISTRIBUTION:=$(shell lsb_release -i -s)
MAJOR_VERSION:=$(shell lsb_release -r -s | cut -d . -f 1)

# Debian OpenVZ directory is /var/lib/vz,  we create /vz -> /var/lib/vz [ ln -s /var/lib/vz /vz ]
# Redhat OpenVZ directory is /vz, we create /var/lib/vz -> /vz [ ln -s /vz /var/lib/vz ]

ifeq ($(DISTRIBUTION),Debian)
VZ=$(DESTDIR)/var/lib/vz
else
VZ=$(DESTDIR)/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/mw-exec-service.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

	if [ -f /etc/debian_version ]; then install --mode 0750 -d $(VARLIB)/vz/lock ; fi
	if [ -f /etc/debian_version -a ! -e $(ROOT)/vz ]; then ln -s /var/lib/vz $(ROOT)/vz ; fi

	# /var/lib must be 0755
	if [ -f /etc/redhat-release ]; then install --mode 0755 -d $(VARLIB); fi
	if [ -f /etc/redhat-release ]; then install --mode 0750 -d $(ROOT)/vz/lock ; fi
	if [ -f /etc/redhat-release -a ! -e $(VARLIB)/vz ]; then ln -s /vz $(VARLIB)/vz ; fi

manual_install:  install
  # create OpenVZ container template
	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
  # self-signed XVNC certificate used by stunnel 
	/usr/bin/make_xvnc_cert.sh

docker_install: install
  # like manual_install but for Docker instead of OpenVZ
  # self-signed XVNC certificate used by stunnel 
	/usr/bin/make_xvnc_cert.sh
	# Docker support
	# /etc/mw-service/docker/
	#                       -hubzero_services
	#                       -hubzero_tools
	#                       -hubzero_vncpasswd
	install --mode 0644 -D Dockerfile.hubzero_services $(ETC)/mw-service/docker/hubzero_services/Dockerfile.hubzero_services
	install --mode 0644 -D hubzero_jessie.list $(ETC)/mw-service/docker/hubzero_services/hubzero_jessie.list
	# hzvncresize provided by hubzero-mw-session package
	# install --mode 0644 -D hzvncresize $(ETC)/mw-service/docker/hubzero_services/hubzero_jessie.list
	install --mode 0644 -D hubzero_package_key $(ETC)/mw-service/docker/hubzero_services/hubzero_package_key
	install --mode 0644 -D startxvnc.docker $(ETC)/mw-service/docker/hubzero_services/startxvnc.docker
	install --mode 0644 -D Dockerfile.hubzero_tools $(ETC)/mw-service/docker/hubzero_tools/Dockerfile.hubzero_tools
	install --mode 0644 -D setup_accounts $(ETC)/mw-service/docker/hubzero_tools/setup_accounts
	install --mode 0644 -D Dockerfile.hubzero_vncpasswd $(ETC)/mw-service/docker/hubzero_vncpasswd/Dockerfile.hubzero_vncpasswd
	# /etc/docker default Docker network
	install --mode 0644 -D daemon.json $(ETC)/docker/daemon.json
	# middleware config with options to use Docker
	install --mode 0640 -D mw-exec-service_docker.conf $(ETC)/mw-service/mw-exec-service.conf
	# build the images
	cd /etc/mw-service/docker/hubzero_vncpasswd; docker build -f Dockerfile.hubzero_vncpasswd --tag hubzero_vncpasswd .
	cd /etc/mw-service/docker/hubzero_services; docker build -f Dockerfile.hubzero_services --tag hubzero_services .
	cd /etc/mw-service/docker/hubzero_tools; docker build -f Dockerfile.hubzero_tools --tag hubzero_tools .

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
	rm -f /var/lib/vz/vz

.PHONY: all install uninstall postinst clean
