# @package      hubzero-vncproxyd-ws
# @file         Makefile
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2010-2014 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2010-2014 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, either version 3 of the License, or (at your option) any
# later version.
#
# 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 0750 -d $(ETC)/hzvncproxyd-ws
	install --mode 0700 -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 0600 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 id hzvncproxy > /dev/null 2>&1; then \
		IUID=`id --user hzvncproxy` ;	\
		IGID=`id --group hzvncproxy` ;	\
	else \
		IUID="NONE" ; \
		IGID="NONE" ; \
	fi ; \
    if [ "$IUID" = "NONE" ]; then \
		adduser --system --disabled-password  \
                --home /var/lib/hzvncproxy \
                --force-badname --group --shell /bin/false --gecos "HUBzero VNC Proxy Service" hzvncproxy ; \
    fi

	mkdir -p $(VARLOG)/hzvncproxyd-ws
	mkdir -p $(VARLOG)/hzvncproxyd-ws/daily
	mkdir -p $(VARRUN)/hzvncproxyd-ws

	chown hzvncproxy:hzvncproxy $(VARLOG)/hzvncproxyd-ws
	chown hzvncproxy:hzvncproxy $(VARLOG)/hzvncproxyd-ws/daily
	chown hzvncproxy:hzvncproxy $(VARRUN)/hzvncproxyd-ws

	chown hzvncproxy:hzvncproxy $(ETC)/hzvncproxyd-ws/targets/default.conf

	if [ -x "/etc/init.d/hzvncproxyd-ws" ]; then \
        update-rc.d hzvncproxyd-ws defaults >/dev/null; \
        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

clean:
	@true

.PHONY: all install uninstall postinst clean
