#
#	Makefile for the Red Hat Linux Vitrual Server package 
#	(C) Red Hat, Inc. 1999 All rights reserved.
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program 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 General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

NAME		= piranha
VERSION 	= 0.7.0
RELEASE 	= 1
ARCH		= $(shell /bin/arch)
DATE		= $(shell date +%d/%b/%Y)

ME		= $(shell id -u )

# Note: There are also hard-coded values for this in the pulse init script,
#       piranha-gui init, and in the piranha spec file. We should tie these
#       all together.


#	I have to admit, I have a weakness for using the Compaq C compiler
#	on the Alpha because it can pick out 32/64 bit problems much more 
#	easily than gcc can. Least this way if CC is defined in the 
#	enviroment, it should override this hackery. - Phil Copeland

CC :=	$(shell if [ "$$CC" != "" ]; then echo $$CC; \
       		else if [ -x /usr/bin/ccc ]; then \
			echo "ccc"; \
		else \
			echo "gcc"; \
		fi ; fi)

COPTS   	=

ifeq ($(CC),ccc)
		CFLAGS	= -Wall -g -DVERSION=\"$(VERSION)\" -DRELEASE=\"$(RELEASE)\" -DDATE=\"$(DATE)\" $(COPTS)
else
		CFLAGS	= -Wall -g -DVERSION=\"$(VERSION)\" -DRELEASE=\"$(RELEASE)\" -DDATE=\"$(DATE)\" $(RPM_OPT_FLAGS) $(COPTS)
endif

LDFLAGS   	= -ggdb # -lefence
LOADLIBES 	= -lpopt -lm
INCLUDE   	= 

SBIN	  	= $(RPM_BUILD_ROOT)/usr/sbin
MANDIR	  	= $(RPM_BUILD_ROOT)/usr/share/man
SYSDIR	  	= $(RPM_BUILD_ROOT)/etc/rc.d/init.d
GNOMEDIR  	= $(RPM_BUILD_ROOT)/usr/share/gnome/apps/System
KDEDIR    	= $(RPM_BUILD_ROOT)/etc/X11/applnk/System
PIXMAPS   	= $(RPM_BUILD_ROOT)/usr/share/pixmaps
HADIR     	= $(RPM_BUILD_ROOT)/etc/sysconfig/ha
HALOG		= $(RPM_BUILD_ROOT)/var/log/piranha
DOCS		= $(RPM_BUILD_ROOT)/usr/share/doc/$(NAME)-$(VERSION)

INSTALL   	= install

all:		explain lvs fos pulse nanny send_arp pkglists

piranha:	all lvsconfig


explain:
ifeq ($(CC),ccc)
		@echo "--------------------------------------------------------------------------"
		@echo "Compaq C found, using it by preferance (use \"export CC=gcc\" to override)"
		@echo "--------------------------------------------------------------------------"
endif
ifeq ($(CC),gcc)
		@echo "--------------------------------------------------------------------------"
		@echo "Assuming build with GNU C compiler"
		@echo "--------------------------------------------------------------------------"
endif


pkglists:


lvs:		lvs.o lvsconfig.o util.o

fos:		fos.o lvsconfig.o util.o

nanny:		nanny.o util.o

send_arp:	send_arp.c
		$(CC) $(CFLAGS) -o send_arp send_arp.c

pulse:		pulse.o lvsconfig.o util.o

lvsconfig:	lvsconfig.o main.o 

pulse.o: 	pulse.c lvsconfig.h util.h

lvs.o:  	lvs.c lvsconfig.h util.h

fos.o:  	fos.c lvsconfig.h util.h

nanny.o:	nanny.c nanny.h

lvsconfig.o:	lvsconfig.c lvsconfig.h

util.o:		util.c

main.o:		main.c lvsconfig.h

install: 	all

		mkdir -p $(SBIN) $(MANDIR)/man5 $(MANDIR)/man8 \
			$(SYSDIR) $(GNOMEDIR) $(KDEDIR) $(DOCS)\
			$(PIXMAPS) $(HADIR) $(HALOG) 

		$(INSTALL) -m 0700 piranha-passwd $(SBIN)/
		$(INSTALL) -m 0755 -s lvs $(SBIN)/
		$(INSTALL) -m 0755 -s fos $(SBIN)/
		$(INSTALL) -m 0755 -s pulse $(SBIN)/
		$(INSTALL) -m 0755 -s nanny $(SBIN)/
		$(INSTALL) -m 0755 -s send_arp $(SBIN)/
		$(INSTALL) -m 0644 lvs.8 $(MANDIR)/man8/
		$(INSTALL) -m 0644 fos.8 $(MANDIR)/man8/
		$(INSTALL) -m 0644 lvs.cf.5 $(MANDIR)/man5/
		$(INSTALL) -m 0644 pulse.8 $(MANDIR)/man8/
		$(INSTALL) -m 0644 nanny.8 $(MANDIR)/man8/
		$(INSTALL) -m 0644 send_arp.8 $(MANDIR)/man8/
		$(INSTALL) -m 0755 pulse.init $(SYSDIR)/pulse
		$(INSTALL) -m 0755 piranha-gui.init $(SYSDIR)/piranha-gui
		$(INSTALL) -m 0755 example_script $(HADIR)/example_script

		#
		# Web Installation.

		ln -sf /usr/lib/apache $(HADIR)/modules
		ln -sf /usr/sbin/httpd $(SBIN)/piranha_gui
		touch $(HALOG)/piranha-gui
		touch $(HALOG)/piranha-gui-access

		#
		# Install as 755 so that dirs are created correctly.
		#

		$(INSTALL) -d -m 0755 web $(HADIR)
		$(INSTALL) -d -m 0755 web/logs $(HADIR)/logs
		$(INSTALL) -d -m 0755 web/conf $(HADIR)/conf
		$(INSTALL) -d -m 0755 web/web $(HADIR)/web
		$(INSTALL) -d -m 0755 web/web/secure $(HADIR)/web/secure
		
		#
		# Install web files.
		#

		$(INSTALL) -m 644 web/conf/httpd.conf		$(HADIR)/conf/httpd.conf
		$(INSTALL) -m 644 web/logs/README		$(HADIR)/logs/README

		$(INSTALL) -m 644 web/web/about_login.php3	$(HADIR)/web/about_login.php3
		$(INSTALL) -m 644 web/web/cluster_logo.gif	$(HADIR)/web/cluster_logo.gif
		$(INSTALL) -m 644 web/web/docs.html		$(HADIR)/web/docs.html
		$(INSTALL) -m 644 web/web/index.html		$(HADIR)/web/index.html
		$(INSTALL) -m 644 web/web/RedHat.gif		$(HADIR)/web/RedHat.gif
		$(INSTALL) -m 644 web/web/secure/control.php3	$(HADIR)/web/secure/control.php3

		#
		# Avoid the CVS dir if it exists.
		#

		$(INSTALL) -m 644 web/web/secure/*.php3		$(HADIR)/web/secure
		$(INSTALL) -m 644 web/web/secure/*.html		$(HADIR)/web/secure

		#
		# typically the .htaccess file always gets forgotten.
		#

		$(INSTALL) -m 644 web/web/secure/.htaccess	$(HADIR)/web/secure/.htaccess


		if [ -s $(RPM_BUILD_ROOT)/etc/lvs.cf ] && [ ! -s $(HADIR)/lvs.cf ]; then \
			echo "# This file was created from your original lvs.cf in /etc" \
				>  $(HADIR)/lvs.cf ;\
			echo >> $(HADIR)/lvs.cf ;\
			cat $(RPM_BUILD_ROOT)/etc/lvs.cf >> $(HADIR)/lvs.cf ;\
			rm -f $(RPM_BUILD_ROOT)/etc/lvs.cf ;\
		fi;

		#
		# Just make sure lvs.cf exists.
		#

		touch $(HADIR)/lvs.cf


		#
		# Ok, better make sure the file permissions are correct.
		# owner,group stuff is irrelevant at this point
		#

		chmod 664 $(HADIR)/lvs.cf
		chmod 775 $(HALOG)
		chmod 644 $(HADIR)/conf/httpd.conf
		chmod 644 $(HADIR)/web/*
		chmod 755 $(HADIR)/web/secure
		chmod 644 $(HADIR)/web/secure/*
		chmod 755 $(HADIR)
		chmod 664 $(HALOG)/piranha-gui
		chmod 664 $(HALOG)/piranha-gui-access

		#
		# This is so that we can install as a standalone compile
		# or as part of the distribution build system.
		# If we are root, we are probably not in the build system
		# which means we are being installed as root from the Makefile.
		# Assume that we have to setup the ownerships and groups
		# 
		# If we are not root then DO NOT do this. The rpm spec file will handle it
		# (I hope)
		#

		if [ $(ME) = 0 ]; then \
			/usr/sbin/groupadd -g 60 -r -f piranha > \
				/dev/null 2>&1 ||:			;\
			/usr/sbin/useradd -u 60 -g 60 -d \
				/etc/sysconfig/ha -r -s \
				/dev/null piranha >/dev/null 2>&1 ||:	;\
			chown root.piranha $(HADIR)/lvs.cf 		;\
			chown root.piranha $(HALOG)			;\
			chown root.nobody $(HADIR)/conf/httpd.conf	;\
			chown -R root.piranha $(HADIR)/web		;\
			chown root.piranha $(HALOG)/piranha-gui		;\
			chown root.piranha $(HALOG)/piranha-gui-access	;\
		fi

		#
		# Lastly, copy the documentation to the right place
		#

		cp -prR docs/* $(DOCS)

clean:
		rm -f nanny lvs lvsconfig fos pulse piranha.spec *.o *.bak \
		piranha-*.tar.gz send_arp cf
		rm -f `find -name "*~"`
		rm -f `find -name "core"`

dist:		clean
		sed -e "s/@@VERSION@@/$(VERSION)/g" <piranha.spec.in >piranha.spec.tmp
		sed -e "s/@@RELEASE@@/$(RELEASE)/g" <piranha.spec.tmp >piranha.spec
		rm -f piranha.spec.tmp
		( cd .. ; tar czvf piranha-$(VERSION).tar.gz \
			--exclude CVS  \
			--exclude gui  \
			--exclude misc \
		piranha ; \
		mv $(NAME)-$(VERSION).tar.gz piranha ; cd piranha)

rpms:		dist
		cp $(NAME)-$(VERSION).tar.gz /usr/src/redhat/SOURCES/
		cp $(NAME).spec /usr/src/redhat/SPECS/
		(cd /usr/src/redhat/SPECS/ ; rpm -ba $(NAME).spec)

srpm:		dist
		cp $(NAME)-$(VERSION).tar.gz /usr/src/redhat/SOURCES/
		cp $(NAME).spec /usr/src/redhat/SPECS/
		(cd /usr/src/redhat/SPECS/ ; rpm -bs $(NAME).spec)
