# @package      hubzero-mw-client
# @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
ETC=$(DESTDIR)/etc
USRSHARE=$(DESTDIR)/usr/share
VARLOG=$(DESTDIR)/var/log

DISTRIBUTION=$(shell lsb_release -i -s)

ifeq ($(DISTRIBUTION),Debian)
WEBUSER=www-data
else
WEBUSER=apache
endif


all:
	@true

install:
	install --mode 0750 -d $(ETC)/mw-client
	install --mode 0755 -D maxwell $(USRBIN)/maxwell
	install --mode 0644 -D mw-client.conf $(USRSHARE)/hubzero-mw-client/mw-client.conf
	install --mode 0750 -d $(VARLOG)/mw-client
	install --mode 0750 -d $(VARLOG)/mw-client/sessions

postinst:
	chown $(WEBUSER):$(WEBUSER) /etc/mw-client
	chown $(WEBUSER):$(WEBUSER) /var/log/mw-client
	chown $(WEBUSER):$(WEBUSER) /var/log/mw-client/sessions
	@if [ ! -f /etc/mw-client/maxwell.key.pub ]; then echo 'y\n' | ssh-keygen -t rsa -f /etc/mw-client/maxwell.key -N '' -C $(WEBUSER)@`hostname` ; fi
	@if [ ! -f /etc/mw-client/notify.key.pub ]; then echo 'y\n' | ssh-keygen -t rsa -f /etc/mw-client/notify.key -N '' -C $(WEBUSER)@`hostname` ; fi
	chown $(WEBUSER):$(WEBUSER) /etc/mw-client/maxwell.key.pub
	chmod 0400 /etc/mw-client/maxwell.key.pub
	chown $(WEBUSER):$(WEBUSER) /etc/mw-client/maxwell.key
	chmod 0400 /etc//mw-client/maxwell.key
	chown $(WEBUSER):$(WEBUSER) /etc/mw-client/notify.key.pub
	chmod 0400 /etc/mw-client/notify.key.pub
	chown $(WEBUSER):$(WEBUSER) /etc/mw-client/notify.key
	chmod 0400 /etc/mw-client/notify.key
	if [ -f /etc/mw-client/notify.key -a -d $(ETCDIR)/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 $(ETCDIR)/mw-service/notify.key; chmod 0400 /etc/mw-service/notify.key; fi	

clean:
	@true

.PHONY: all install postinst clean
