include ../../main.mk

srvname=sngtc_server
tcplibname=libSngTcpServer2.so
sample_xml=sngtc_server.conf.xml.sample
ctrl_script=sngtc_server_ctrl
safe_script=safe_sngtc_server
safe_rc=safe_sngtc_server.rc
server_rc=sngtc_server.rc
logrotate_script=sngtc_log_rotate

.PHONY: all
all:
	make -C library/build/linux
	make -C server/build/linux

.PHONY: clean
clean:
	make -C library/build/linux clean
	make -C server/build/linux  clean

.PHONY: install
install:
	install -m 755 -D server/build/linux/$(srvname) $(DESTDIR)$(sbindir)/$(srvname)
	install -m 755 -D library/build/linux/$(tcplibname) $(DESTDIR)$(libdir)/$(tcplibname)
	install -m 644 -D server/$(sample_xml) $(DESTDIR)$(sysconfdir)/sngtc/$(sample_xml)
	install -m 755 -D linux/$(ctrl_script) $(DESTDIR)$(sbindir)/$(ctrl_script)
	install -m 755 -D linux/$(ctrl_script) $(DESTDIR)$(sysconfdir)/init.d/$(ctrl_script)
	install -m 755 -D linux/$(safe_script) $(DESTDIR)$(sbindir)/$(safe_script)
	install -m 755 -D linux/$(safe_rc) $(DESTDIR)$(sysconfdir)/sngtc/$(safe_rc)
	install -m 755 -D linux/$(server_rc) $(DESTDIR)$(sysconfdir)/sngtc/$(server_rc)
	@if [ ! -d $(DESTDIR)/$(sysconfdir)/logrotate.d ]; then \
		mkdir -p $(DESTDIR)/$((sysconfdir)/logrotate.d ; \
	fi
	install -m 644 -D linux/$(logrotate_script) $(DESTDIR)$(sysconfdir)/logrotate.d/$(logrotate_script)
	

.PHONY: uninstall
uninstall:
	rm -f $(DESTDIR)$(sbindir)/$(srvname)
	rm -f $(DESTDIR)$(libdir)/$(tcplibname)
	rm -f $(DESTDIR)$(sbindir)/$(ctrl_script)
	rm -f $(DESTDIR)$(sysconfdir)/init.d/$(ctrl_script)
	rm -f $(DESTDIR)$(sbindir)/$(safe_script)

.PHONY: boot
boot:
	@if [ -e /sbin/chkconfig ]; then \
		echo "chkconfig --add sngtc_server_ctrl" ; \
		chkconfig --add $(ctrl_script) ; \
	else \
		echo ; \
		echo "Error: Failed to find chkconfig" ; \
		echo "Please add $(DESTDIR)$(sbindir)/$(ctrl_script) to the boot sequence manually" ; \
		echo ; \
	fi

