# @package      hubzero-mw-client
# @file         Makefile
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2010-2015 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-2015 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.
#

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

uninstall:
	@true

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 uninstall postinst clean
