# @package      hubzero-vncproxyd-ws
# @file         Makefile
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2010-2015 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2010-2015 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, version 3.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#

USRBIN=$(DESTDIR)/usr/bin
USRSBIN=$(DESTDIR)/usr/sbin
ETC=$(DESTDIR)/etc
VARLOG=$(DESTDIR)/var/log
VARRUN=$(DESTDIR)/var/run

all:
	@true

install:
	install --mode 0755 hzvncproxyd-ws -D $(USRSBIN)/hzvncproxyd-ws
	install --mode 0755 hzvncproxyd-ws-config -D $(USRSBIN)/hzvncproxyd-ws-config
	install --mode 0755 -d $(ETC)/hzvncproxyd-ws
	install --mode 0750 -d $(ETC)/hzvncproxyd-ws/targets
	install --mode 0755 hzvncproxyd-ws.init -D $(ETC)/init.d/hzvncproxyd-ws
	install --mode 0644 hzvncproxyd-ws.logrotate -D $(ETC)/logrotate.d/hzvncproxyd-ws
	if [ ! -d /etc/sysconfig -a ! -d /etc/default  ]; then install --mode=0644 hzvncproxyd-ws.default -D $(ETC)/default/hzvncproxyd-ws; fi
	if [ ! -d /etc/sysconfig -a   -d /etc/default -a ! -r $(ETC)/sysconfig/hzvncproxyd-ws ]; then install --mode=0644 hzvncproxyd-ws.default -D $(ETC)/default/hzvncproxyd-ws; fi
	if [   -d /etc/sysconfig -a ! -r $(ETC)/sysconfig/hzvncproxyd-ws ]; then install --mode=0644 hzvncproxyd-ws.default -D $(ETC)/sysconfig/hzvncproxyd-ws; fi
	if [ ! -r $(ETC)/hzvncproxyd-ws/targets/default.conf ]; then install --mode 0640 default.conf -D $(ETC)/hzvncproxyd-ws/targets/default.conf; fi

uninstall:
	rm -f $(ETC)/init.d/hzvncproxyd-ws
	rm -f $(ETC)/logrotate.d/hzvncproxyd-ws
	rm -f $(ETC)/default/hzvncproxyd-ws
	rm -f $(ETC)/sysconfig/hzvncproxyd-ws
	rm -f $(USRSBIN)/hzvncproxyd-ws
	
postinst:
	if [ ! -f /sbin/nologin ]; then \
		adduser --system --home /var/lib/hzvncproxy --quiet --group --gecos "HUBzero VNC Proxy Service" hzvncproxy ; \
	else \
		getent group hzvncproxy >/dev/null || groupadd -r hzvncproxy ; \
		getent passwd hzvncproxy >/dev/null || useradd -r -g hzvncproxy -d /var/lib/hzvncproxy -s /sbin/nologin \
		-c "HUBzero VNC Proxy Service" hzvncproxy ; \
	fi

	install --mode 0755 -o root -g root -d /var/log/hzvncproxyd-ws
	install --mode 0755 -o root -g root -d /var/log/hzvncproxyd-ws/daily

	chown hzvncproxy:hzvncproxy /etc/hzvncproxyd-ws/targets
	chown hzvncproxy:hzvncproxy /etc/hzvncproxyd-ws/targets/default.conf

	if [ ! -f /etc/hzvncproxyd-ws/dhparams.pem ]; then \
		openssl dhparam -out /etc/hzvncproxyd-ws/dhparams.pem 2048 ; \
	fi

	if [ -x "/etc/init.d/hzvncproxyd-ws" ]; then \
        	if [ -x /usr/sbin/update-rc.d ]; then \
			update-rc.d hzvncproxyd-ws defaults >/dev/null; \
		fi ; \
	        if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then \
        	        invoke-rc.d hzvncproxyd-ws start || exit $?; \
        	else \
                	/etc/init.d/hzvncproxyd-ws start || exit $?; \
        	fi \
	fi

purge: uninstall
	rm -fr /etc/hzvncproxyd-ws

clean:
	@true

.PHONY: all install uninstall postinst clean
