# @package      hubzero-mw2-common
# @file         Makefile
# @author       Pascal Meunier <pmeunier@purdue.edu>
# @copyright    Copyright (c) 2016-2017 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Based on previous work by Richard L. Kennell and Nicholas Kisseberth
#
# Copyright (c) 2016-2017 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.
#

# Common libraries are installed as owned by root so the client can't modify them, as root may need to use them 

# find the highest Python version installed
ifeq (/usr/lib/python2.6,$(wildcard /usr/lib/python2.6))
  export MOD_HUBZERO=/usr/lib/python2.6/site-packages/hubzero
endif
ifeq (/usr/lib/python2.7,$(wildcard /usr/lib/python2.7))
  export MOD_HUBZERO=/usr/lib/python2.7/dist-packages/hubzero
endif
export MOD_INSTDIR=$(MOD_HUBZERO)/mw

# Does www-data or apache exist?
ifeq ($(shell grep 'www-data:' /etc/group | cut -d ':' -f1),www-data)
  # Debian systems
  export CLIENTGROUP=www-data
  export CLIENTOWNER=www-data
else
  ifeq ($(shell grep 'apache:' /etc/group | cut -d ':' -f1),apache)
    # RedHat systems
    export CLIENTGROUP=apache
    export CLIENTOWNER=apache
  else
    # for execution hosts
    export CLIENTGROUP=root
    export CLIENTOWNER=root
  endif
endif

INSTDIR=$(DESTDIR)/$(MOD_INSTDIR)
VARLIBDIR=$(DESTDIR)/var/lib
LOGDIR=$(DESTDIR)/var/log
OWNER=root
USRBIN=$(DESTDIR)/usr/bin
USRSBIN=$(DESTDIR)/usr/sbin
USRLIB=$(DESTDIR)/usr/lib
ETC=$(DESTDIR)/etc
USRSHARE=$(DESTDIR)/usr/share
USRSHAREMAN=$(DESTDIR)/usr/share/man
USRSHAREDOC=$(DESTDIR)/usr/share/doc
SBIN=$(DESTDIR)/sbin
VARLOG=$(DESTDIR)/var/log
USRSHAREMANMAN8=$(DESTDIR)/usr/share/man/man8
USRLOCAL=$(DESTDIR)/usr/local

all:
	@true

install:
	sed -e "s|MOD_HUBZERO|$(MOD_HUBZERO)|" -e "s|ETCDIR|/etc|" -e "s|VARLIBDIR|/var/lib|" -e "s|LOGDIR|/var/log|" -e "s|OWNER|$(OWNER)|" -e "s|WEBUSER|$(CLIENTOWNER)|" -e "s|USRBINDIR|/usr/bin|" < constants.template > constants.py
	install --mode 0644 -D app.py $(INSTDIR)/app.py
	install --mode 0644 -D constants.py $(INSTDIR)/constants.py
	install --mode 0644 -D container.py $(INSTDIR)/container.py
	install --mode 0644 -D display.py $(INSTDIR)/display.py
	install --mode 0644 -D errors.py $(INSTDIR)/errors.py
	install --mode 0644 -D fileserver.py $(INSTDIR)/fileserver.py
	install --mode 0644 -D heartbeat.py $(INSTDIR)/heartbeat.py
	install --mode 0644 -D host.py $(INSTDIR)/host.py
	install --mode 0644 -D __init__.py $(INSTDIR)/__init__.py
	install --mode 0644 -D log.py $(INSTDIR)/log.py
	install --mode 0644 -D mw_db.py $(INSTDIR)/mw_db.py
	install --mode 0644 -D session.py $(INSTDIR)/session.py
	install --mode 0644 -D support.py $(INSTDIR)/support.py
	install --mode 0644 -D user_account.py $(INSTDIR)/user_account.py
	install --mode 0644 -D vnc_pass.py $(INSTDIR)/vnc_pass.py
	install --mode 0644 -D zone.py $(INSTDIR)/zone.py

manual_install:  install
	rm -f $(INSTDIR)/*.pyc $(INSTDIR)/*.pyo
	python -m compileall $(INSTDIR)/
	chmod 0644 $(INSTDIR)/*.pyc 
	chmod 0644 $(INSTDIR)/*.pyo 2>/dev/null || true
	# when created inside a package install, __init__.py created here conflicts with package hubzero-python
	# this is needed for manual installation success in the absence of package hubzero-python
	if ! [ -f $(DESTDIR)/$(MOD_HUBZERO)/__init__.py ]; then touch $(DESTDIR)/$(MOD_HUBZERO)/__init__.py; fi


uninstall:
	rm -rf $(INSTDIR)/*

postinst:
	@true

clean:
	rm -rf $(INSTDIR)/*

.PHONY: all install uninstall postinst clean
