# Makefile for erServer
# (c) 2000 Vadim Mikheev, PostgreSQL Inc.
# Updated for erServer 1.2 by Justin Clift
#

top_builddir = .
include Makefile.global

NAME	= erserver
SONAME	= src/$(NAME)$(DLSUFFIX)
PTE	= pte
SOPTE	= src/$(PTE)$(DLSUFFIX)
PERLS	= perl/eRServer.pm
BIN	= \
	bin/ers_ctl \
	bin/ers_addtable \
	bin/ers_addslave \
	bin/ers_masterremovefunctions \
	bin/ers_masterremovetables \
	bin/ers_mastershowcolumns \
	bin/ers_mastershowtables \
	bin/ers_monitor \
	bin/ers_remove \
	bin/ers_setup \
	bin/ers_slaveincludetable \
	bin/ers_slaveremovetables \
	bin/ers_slaveshowcolumns \
	bin/ers_slaveshowtables 
DOCS	= \
	docs/install.txt \
	docs/install.html \
	docs/install.pdf \
	docs/diagram.gif \
	docs/logo.png
TMPLS	= \
	templates/ers_setvars-template \
	templates/replication.cfg-template
CONFS	= etc/repliclog.cfg
OSQLS	= sql/lastchg_install.sql sql/lastchg_remove.sql
JARS	= jar/jdbc7.0-1.2.jar jar/log4j.jar
ERJAR	= jar/erserver.jar
RHSCRIPT = scripts/erserver.redhat
FBSCRIPT = scripts/erserver.freebsd
NBSCRIPT = scripts/erserver.netbsd

override CPPFLAGS := -I/usr/local/include $(CPPFLAGS)
override CFLAGS += $(CFLAGS_SL) -DUSE_T_XMIN

all: $(ERJAR) $(TMPLS) $(SONAME) $(SOPTE)

%.sql: %.sql.in
	sed -e "s:_OBJWD_:$(libdir):g" \
	    -e "s:_NAME_:$(NAME):g" \
	    -e "s:_DLSUFFIX_:$(DLSUFFIX):g" $< >$@

$(ERJAR):
	cd java; /bin/sh ./build.sh create; cd ..

install: all installdirs
	for file in $(BIN); do \
	  $(INSTALL_SCRIPT) $$file $(bindir) || exit ; \
	done
	for file in $(DOCS); do \
	  $(INSTALL_DATA) $$file $(docdir) || exit ; \
	done
	for file in $(CONFS); do \
	  $(INSTALL_DATA) $$file $(sysconfdir) || exit ; \
	done
	for file in $(OSQLS); do \
	  $(INSTALL_DATA) $$file $(sqldir) || exit ; \
	done
	for file in $(JARS); do \
	  $(INSTALL_DATA) $$file $(libdir) || exit ; \
	done
	for file in $(TMPLS); do \
	  $(INSTALL_SCRIPT) $$file $(templdir) || exit ; \
	done
	for file in $(ERJAR); do \
	  $(INSTALL_DATA) $$file $(libdir) || exit ; \
	done
	$(INSTALL_DATA) $(PERLS) $(libdir)
	$(INSTALL_DATA) docs/INSTALL $(basedir)
	$(INSTALL_SHLIB) $(SONAME) $(libdir)
	$(INSTALL_SHLIB) $(SOPTE) $(libdir)
	# If running on RedHat, install its startup script
	if [ -f /etc/redhat-release ] ; then cp -f $(RHSCRIPT) /etc/init.d/erserver ; chkconfig --add erserver ; fi
	# If running on FreeBSD, install its startup script
	if [ `uname -s` = "FreeBSD" ] ; then $(INSTALL_SCRIPT) $(FBSCRIPT) $(templdir) || exit ; fi
	# If running on NetBSD, install it's startup script
	if [ `uname -s` = "NetBSD" ] ; then $(INSTALL_SCRIPT) $(NBSCRIPT) $(templdir) || exit ; fi
	

installdirs:
	$(mkinstalldirs) $(bindir) \
	  $(libdir) $(docdir) $(logdir) \
	  $(sysconfdir) $(sqldir) $(templdir)

clean distclean maintainer-clean:
	cd java; /bin/sh ./build.sh clean; cd ..
	rm -f $(PERLS) $(ERJAR) $(SONAME) $(NAME).o $(SOPTE) $(PTE).o
	rm -f bin/ers_addtable bin/ers_setup bin/ers_addslave bin/ers_remove bin/ers_ctl
	rm -f templates/ers_setvars-template
	rm -f autoscan.log config.h config.log config.status
	rm -rf autom4te.cache 
	rm -f Makefile.port Makefile.global
