# @package      hubzero-mailgateway
# @file         Makefile
# @copyright    Copyright (c) 2012-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2012-2020 The Regents of the University of California.
#
# 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 The Regents of the University of California.
#

USRLIB=$(DESTDIR)/usr/lib
ETC=$(DESTDIR)/etc
VARLOG=$(DESTDIR)/var/log
VARLIB=$(DESTDIR)/var/lib

DISTRIBUTION:=$(shell lsb_release -i -s)

all:
	@true

install:
	install --mode 0755 -D python/mailproc.py $(USRLIB)/hubzero-mailgateway/bin/mailproc/mailproc.py
	install --mode 0755 -D python/processgroupforumpost.py $(USRLIB)/hubzero-mailgateway/bin/mailproc/processgroupforumpost.py
	install --mode 0755 -D python/processsupportemail.py $(USRLIB)/hubzero-mailgateway/bin/mailproc/processsupportemail.py
	install --mode 0755 -D python/processticketcomment.py $(USRLIB)/hubzero-mailgateway/bin/mailproc/processticketcomment.py
	install --mode 0755 -D python/filecopy.py $(USRLIB)/hubzero-mailgateway/bin/mailproc/filecopy.py
	install --mode 0755 -D python/process_emailbounces.py $(USRLIB)/hubzero-mailgateway/bin/mailproc/process_emailbounces.py
ifeq ($(DISTRIBUTION),Debian)
	install --mode 0644 -D exim4/router/950_exim4-config_groupmessage  $(ETC)/exim4/conf.d/router/950_exim4-config_groupmessage 
	install --mode 0644 -D exim4/router/950_exim4-config_ticketcomment  $(ETC)/exim4/conf.d/router/950_exim4-config_ticketcomment
	install --mode 0644 -D exim4/router/955_exim4-config_supportmessage  $(ETC)/exim4/conf.d/router/955_exim4-config_supportmessage
	install --mode 0644 -D exim4/transport/40_exim4-config_hubgroupmessage_pipe  $(ETC)/exim4/conf.d/transport/40_exim4-config_hubgroupmessage_pipe
	install --mode 0644 -D exim4/transport/40_exim4-config_ticketcomment_pipe  $(ETC)/exim4/conf.d/transport/40_exim4-config_ticketcomment_pipe
	install --mode 0644 -D exim4/transport/45_exim4-config_support_email_pipe  $(ETC)/exim4/conf.d/transport/45_exim4-config_support_email_pipe
	install --mode 0644 -D exim4/router/975_exim4-config_emailbounces $(ETC)/exim4/conf.d/router/975_exim4-config_emailbounces
	install --mode 0644 -D exim4/transport/65_exim4-config_emailbounces_pipe $(ETC)/exim4/conf.d/transport/65_exim4-config_emailbounces_pipe
	install --mode 0755 -D hubzero-mailgateway.logrotate.debian $(ETC)/logrotate.d/hubzero-mailgateway
	install --mode 0755 -D hubzero-mailgateway.sudoers.debian $(ETC)/sudoers.d/hubzero-mailgateway
else
	install --mode 0755 -D hubzero-mailgateway.logrotate.el $(ETC)/logrotate.d/hubzero-mailgateway
	install --mode 0755 -D hubzero-mailgateway.sudoers.el $(ETC)/sudoers.d/hubzero-mailgateway
endif
	install --mode 0750 -d $(VARLOG)/hubzero-mailgateway
	install --mode 0750 -d $(VARLIB)/hubzero-mailgateway

collectfiles:
	if [ -f /usr/lib/hubzero-mailgateway/bin/mailproc/mailproc.py ]; then cp /usr/lib/hubzero-mailgateway/bin/mailproc/mailproc.py python/mailproc.py; fi
	if [ -f /usr/lib/hubzero-mailgateway/bin/mailproc/processgroupforumpost.py ]; then cp /usr/lib/hubzero-mailgateway/bin/mailproc/processgroupforumpost.py python/processgroupforumpost.py; fi
	if [ -f /usr/lib/hubzero-mailgateway/bin/mailproc/processsupportemail.py ]; then cp /usr/lib/hubzero-mailgateway/bin/mailproc/processsupportemail.py python/processsupportemail.py; fi
	if [ -f /usr/lib/hubzero-mailgateway/bin/mailproc/processticketcomment.py ]; then cp /usr/lib/hubzero-mailgateway/bin/mailproc/processticketcomment.py python/processticketcomment.py; fi
	if [ -f /usr/lib/hubzero-mailgateway/bin/mailproc/filecopy.py ]; then cp /usr/lib/hubzero-mailgateway/bin/mailproc/filecopy.py python/filecopy.py; fi
	if [ -f /etc/exim4/conf.d/router/950_exim4-config_groupmessage ]; then cp /etc/exim4/conf.d/router/950_exim4-config_groupmessage exim4/router/950_exim4-config_groupmessage; fi
	if [ -f /etc/exim4/conf.d/router/950_exim4-config_ticketcomment ]; then cp /etc/exim4/conf.d/router/950_exim4-config_ticketcomment exim4/router/950_exim4-config_ticketcomment; fi
	if [ -f /etc/exim4/conf.d/router/955_exim4-config_supportmessage ]; then cp /etc/exim4/conf.d/router/955_exim4-config_supportmessage exim4/router/955_exim4-config_supportmessage; fi
	if [ -f /etc/exim4/conf.d/transport/40_exim4-config_hubgroupmessage_pipe ]; then cp /etc/exim4/conf.d/transport/40_exim4-config_hubgroupmessage_pipe exim4/transport/40_exim4-config_hubgroupmessage_pipe; fi
	if [ -f /etc/exim4/conf.d/transport/40_exim4-config_ticketcomment_pipe ]; then cp /etc/exim4/conf.d/transport/40_exim4-config_ticketcomment_pipe exim4/transport/40_exim4-config_ticketcomment_pipe; fi
	if [ -f /etc/exim4/conf.d/transport/45_exim4-config_support_email_pipe ]; then cp /etc/exim4/conf.d/transport/45_exim4-config_support_email_pipe exim4/transport/45_exim4-config_support_email_pipe; fi

uninstall:
	rm -f $(USRLIB)/hubzero-mailgateway/bin/mailproc/mailproc.py
	rm -f $(USRLIB)/hubzero-mailgateway/bin/mailproc/processgroupforumpost.py
	rm -f $(USRLIB)/hubzero-mailgateway/bin/mailproc/processsupportemail.py
	rm -f $(USRLIB)/hubzero-mailgateway/bin/mailproc/processticketcomment.py
	rm -f $(USRLIB)/hubzero-mailgateway/bin/mailproc/filecopy.py
	rm -f $(USRLIB)/hubzero-mailgateway/bin/mailproc/process_emailbounces.py
	rm -f $(ETC)/exim4/conf.d/router/950_exim4-config_groupmessage
	rm -f $(ETC)/exim4/conf.d/router/950_exim4-config_ticketcomment
	rm -f $(ETC)/exim4/conf.d/router/955_exim4-config_supportmessage
	rm -f $(ETC)/exim4/conf.d/transport/40_exim4-config_hubgroupmessage_pipe
	rm -f $(ETC)/exim4/conf.d/transport/40_exim4-config_ticketcomment_pipe
	rm -f $(ETC)/exim4/conf.d/transport/45_exim4-config_support_email_pipe
	rm -f $(ETC)/exim4/conf.d/router/975_exim4-config_emailbounces
	rm -f $(ETC)/exim4/conf.d/transport/65_exim4-config_emailbounces_pipe
	rm -f $(ETC)/logrotate.d/hubzero-mailgateway
	rm -fr $(VARLOG)/hubzero-mailgateway
	rm -fr $(VARLIB)/hubzero-mailgateway
	rm -fr $(ETC)/sudoers.d/hubzero-mailgateway

postinst:
ifeq ($(DISTRIBUTION),Debian)
	chown Debian-exim:Debian-exim /var/log/hubzero-mailgateway
	chown Debian-exim:Debian-exim /var/lib/hubzero-mailgateway
else
	adduser --system -m --home-dir /var/lib/hzmail --shell /sbin/nologin hzmail
	chown hzmail:hzmail /var/log/hubzero-mailgateway
	chown hzmail:hzmail /var/lib/hubzero-mailgateway
endif

clean:
	@true

.PHONY: all install uninstall postinst clean


