# Makefile for AZ-Prolog install-binary.
PREFIX = /usr/local
ESCAPED_PREFIX = \/usr\/local

DEBUG_MODE     = false
STATIC_LINK    = false

SO_VERSION         = 1.0.0
SO_VERSION_MAJOR   = 1
SO_VERSION_MINOR   = 0
SO_VERSION_RELEASE = 0

CPOPT = --preserve=mode,timestamps --no-preserve=ownership

# Shell command string to get the type of Linux OS
GET_OS_TYPE=cat /proc/version | awk -F'(' '{print $$4}' |  \
			    awk -F" " '{print $$1}' |  \
			    awk -F"/" '{print $$1}'
# Confirmed OS Types so far: Ubuntu, Gentoo
OS_TYPE=$(shell $(GET_OS_TYPE))

ifeq ($(OS_TYPE),Ubuntu)
	GET_CURSES=dpkg -l|grep "^ii  libncursesw5-dev[: ]" | awk -F' ' '{print $$1}'
	ST_CURSES=$(shell $(GET_CURSES))
else ifeq ($(OS_TYPE),Gentoo)

else

endif


default: install


install_full:
	$(MAKE) install_hiredis
	$(MAKE) install_libws
	$(MAKE) install


install:
ifeq ($(OS_TYPE),Ubuntu) 
ifeq ($(ST_CURSES),ii)
	@echo "package libncursesw5-dev is installed."
else
	apt-get install libncursesw5-dev
endif
else ifeq ($(OS_TYPE),Gentoo) 
	emerge dev-libs/cdk
	emerge dev-libs/openssl
endif

	mkdir -p -m 755 $(PREFIX)/bin
	mkdir -p -m 755 $(PREFIX)/lib
	mkdir -p -m 755 $(PREFIX)/lib/azprolog
	mkdir -p -m 755 $(PREFIX)/include
	mkdir -p -m 755 $(PREFIX)/share
	mkdir -p -m 755 $(PREFIX)/share/azprolog
	mkdir -p -m 755 $(PREFIX)/share/doc
	mkdir -p -m 755 $(PREFIX)/share/doc/azprolog
ifeq ($(DEBUG_MODE),false)
	mkdir -p -m 755 $(PREFIX)/lib/azprolog/obj
	mkdir -p -m 755 $(PREFIX)/lib/azprolog/ext
else
	mkdir -p -m 755 $(PREFIX)/lib/azprolog/debug/obj
	mkdir -p -m 755 $(PREFIX)/lib/azprolog/debug/ext
endif

	cd usr/bin; cp -r $(CPOPT) * $(PREFIX)/bin
	cd usr/lib; cp -r $(CPOPT) * $(PREFIX)/lib

ifneq ($(STATIC_LINK),true)
ifeq ($(DEBUG_MODE),true)
	cd $(PREFIX)/lib; rm -f libazp_d.so; ln -s libazp_d.so.$(SO_VERSION) libazp_d.so
	cd $(PREFIX)/lib; rm -f libazp_d.so.$(SO_VERSION_MAJOR); ln -s libazp_d.so.$(SO_VERSION) libazp_d.so.$(SO_VERSION_MAJOR)
else
	cd $(PREFIX)/lib; rm -f libazp.so; ln -s libazp.so.$(SO_VERSION) libazp.so
	cd $(PREFIX)/lib; rm -f libazp.so.$(SO_VERSION_MAJOR); ln -s libazp.so.$(SO_VERSION) libazp.so.$(SO_VERSION_MAJOR)
endif
endif

	cd usr/include; cp -r $(CPOPT) * $(PREFIX)/include
	cd usr/share/azprolog;  cp -r $(CPOPT) * $(PREFIX)/share/azprolog
	cd usr/share/doc;       cp -r $(CPOPT) * $(PREFIX)/share/doc

ifeq ($(DEBUG_MODE),false)
	$(MAKE) install_azprolog_config
	$(MAKE) install_azprolog_pc
endif

	$(MAKE)	ex_ldconfig

	@echo 
	@echo 'all done!'
	@echo
ifneq ($(PREFIX),/usr/local)
	@echo 'Please set your environment. (ex. in ~/.bashrc)'
	@echo 'export AZProlog=$(PREFIX)'
	@echo
endif


install_azprolog_config:
	rm -f $(PREFIX)/bin/azprolog-config
	cd usr/bin; sed -e "s/@prefix@/$(ESCAPED_PREFIX)/g" azprolog-config > $(PREFIX)/bin/azprolog-config
	chmod 755 $(PREFIX)/bin/azprolog-config

install_azprolog_pc:
	rm -f $(PREFIX)/lib/pkgconfig/azprolog.pc
	cd usr/lib/pkgconfig; sed -e "s/@prefix@/$(ESCAPED_PREFIX)/g" azprolog.pc > $(PREFIX)/lib/pkgconfig/azprolog.pc
	chmod 644 $(PREFIX)/lib/pkgconfig/azprolog.pc


uninstall_full:
	$(MAKE) uninstall
	$(MAKE) uninstall_libws
	$(MAKE) uninstall_hiredis


uninstall:
ifeq ($(DEBUG_MODE),true)
	rm -f  $(PREFIX)/bin/azpc_d
	rm -f  $(PREFIX)/bin/prolog_d
	rm -f  $(PREFIX)/bin/prolog_c_d
	rm -f  $(PREFIX)/lib/libazp_d.a
	rm -f  $(PREFIX)/lib/libazp_d.so.$(SO_VERSION)
	rm -f  $(PREFIX)/lib/libazp_d.so
	rm -f  $(PREFIX)/lib/libazp_d.so.$(SO_VERSION_MAJOR)
	rm -f  $(PREFIX)/lib/libazcurses_d.so.$(SO_VERSION)
	rm -f  $(PREFIX)/lib/libazcurses_d.so
	rm -f  $(PREFIX)/lib/libazcurses_d.so.$(SO_VERSION_MAJOR)
	rm -rf $(PREFIX)/lib/azprolog/debug
else
	rm -f  $(PREFIX)/bin/azpc
	rm -f  $(PREFIX)/bin/prolog
	rm -f  $(PREFIX)/bin/prolog_c
	rm -f  $(PREFIX)/bin/prologcgi
	rm -f  $(PREFIX)/bin/azprolog-config
	rm -f  $(PREFIX)/include/azprolog.h
	rm -f  $(PREFIX)/include/azst.h
	rm -f  $(PREFIX)/lib/libazp.a
	rm -f  $(PREFIX)/lib/libazp.so.$(SO_VERSION)
	rm -f  $(PREFIX)/lib/libazp.so
	rm -f  $(PREFIX)/lib/libazp.so.$(SO_VERSION_MAJOR)
	rm -f  $(PREFIX)/lib/libazcurses.so.$(SO_VERSION)
	rm -f  $(PREFIX)/lib/libazcurses.so
	rm -f  $(PREFIX)/lib/libazcurses.so.$(SO_VERSION_MAJOR)
	rm -rf $(PREFIX)/lib/azprolog
	rm -rf $(PREFIX)/lib/pkgconfig/azprolog.pc
	rm -rf $(PREFIX)/share/azprolog
	rm -rf $(PREFIX)/share/doc/azprolog
endif

	$(MAKE)	ex_ldconfig

	@echo 
	@echo 'all done!'
	@echo 


ex_ldconfig:
	if [ "$$(id -u)" -eq 0 ]; then \
		echo "ldconfig..."; \
		ldconfig; \
	fi


install_libws:
	cd build/libwebsockets; $(MAKE) -f Makefile.libws.add install

install_hiredis:
	cd build/hiredis; $(MAKE) -f Makefile.hiredis.add install

uninstall_libws:
	cd build/libwebsockets; $(MAKE) -f Makefile.libws.add uninstall

uninstall_hiredis:
	cd build/hiredis; $(MAKE) -f Makefile.hiredis.add uninstall
