# vim:noexpandtab:softtabstop=2:tabstop=2:shiftwidth=2:nowrap
# 
# Copyright (c) 2015-2016, iwrite authors
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

.SILENT:

UNAME:= $(shell uname)
BITS:= $(shell uname -m)

ifndef prefix
prefix=/usr
endif

ifndef BINDIR
	ifeq ($(UNAME), Linux)
		BINDIR=${prefix}/bin
	endif
endif

ifndef LIBDIR
	ifeq ($(UNAME), Linux)
		ifeq ($(BITS), x86_64)
			LIBDIR=${prefix}/lib64
		else
			LIBDIR=${prefix}/lib
		endif
	endif
endif

ifndef DATADIR
	ifeq ($(UNAME), Linux)
		DATADIR=${prefix}/share/iwrite
	endif
endif

ifndef DOCDIR
	ifeq ($(UNAME), Linux)
		DOCDIR=${prefix}/share/doc/iwrite
	endif
endif

ifeq ($(UNAME), Linux)
CFGFILE=data/iwrite_driver.cfg_linux
else
CFGFILE=data/iwrite_driver.cfg_bsd
endif

#CFGDIR=`echo ${XDG_CONFIG_DIRS} | cut -d ":" -f 1`
CFGDIR=/etc/xdg

.PHONY: all
all: drivers bson decNumber icu ruler geometry iwrite 

.PHONY: bson
bson:
	@echo making bson
	cd src/bson && ${MAKE} -k

.PHONY: decNumber
decNumber:
	@echo making decnumber
	cd src/decNumber && ${MAKE} -k

.PHONY: drivers
drivers:
	@echo making drivers
	cd drivers && ${MAKE} -k

.PHONY: icu
icu:
	@echo making decnumber
	cd src/icu  && ${MAKE} -k

.PHONY: ruler
ruler:
	@echo making ruler
	cd src/ruler  && ${MAKE} -k

.PHONY: geometry
geometry:
	@echo making geometry
	cd src/geometry  && ${MAKE} -k

.PHONY: iwrite
iwrite:
	@echo making iwrite
	cd src && ${MAKE} -k

.PHONY: clean
clean:
	@echo making clean
	cd drivers && ${MAKE} clean
	cd src/bson && ${MAKE} clean
	cd src/decNumber && ${MAKE} clean
	cd src/icu && ${MAKE} clean
	cd src/ruler && ${MAKE} clean
	cd src && ${MAKE} clean

.PHONY: install
ifeq ($(UNAME), Linux)
install: install_Linux
else
install: 
	@echo install unsupported
endif

.PHONY: uninstall
ifeq ($(UNAME), Linux)
uninstall: uninstall_Linux
else
uninstall:
	@echo uninstall unsupported
endif

install_Linux:\
	src/iwrite \
	drivers/postgres/libiwsql_pq.so  \
	drivers/mariadb/libiwsql_mariadb.so \
	drivers/sqlite3/libiwsql_sqlite3.so \
	drivers/oracle/libiwsql_oracle.so \
	doc/iwrite.pdf
	@echo installing iwrite
	install -D --strip --mode=0655 src/iwrite ${DESTDIR}${BINDIR}/iwrite
	install -D --strip --mode=0755 drivers/postgres/libiwsql_pq.so ${DESTDIR}${LIBDIR}/libiwsql_pq.so 
	install -D --strip --mode=0755 drivers/mariadb/libiwsql_mariadb.so ${DESTDIR}${LIBDIR}/libiwsql_mariadb.so 
	install -D --strip --mode=0755 drivers/sqlite3/libiwsql_sqlite3.so ${DESTDIR}${LIBDIR}/libiwsql_sqlite3.so 
	install -D --strip --mode=0755 drivers/oracle/libiwsql_oracle.so ${DESTDIR}${LIBDIR}/libiwsql_oracle.so 
	install -D --mode 0644 ${CFGFILE} ${DESTDIR}${CFGDIR}/iwrite_driver.cfg
	install -D --mode 0444 README ${DESTDIR}${DOCDIR}/README
	install -D --mode 0444 COPYING ${DESTDIR}${DOCDIR}/COPYING
	install -D --mode 0444 doc/iwrite.pdf ${DESTDIR}${DOCDIR}/iwrite.pdf
	install -D --mode 0644 src/break.glade ${DESTDIR}${DATADIR}/ui/break.glade
	install -D --mode 0644 src/circle.glade ${DESTDIR}${DATADIR}/ui/circle.glade
	install -D --mode 0644 src/database.glade ${DESTDIR}${DATADIR}/ui/database.glade
	install -D --mode 0644 src/date.glade ${DESTDIR}${DATADIR}/ui/date.glade
	install -D --mode 0644 src/export.glade ${DESTDIR}${DATADIR}/ui/export.glade
	install -D --mode 0644 src/export_page_setup.glade ${DESTDIR}${DATADIR}/ui/export_page_setup.glade
	install -D --mode 0644 src/fbarcode.glade ${DESTDIR}${DATADIR}/ui/fbarcode.glade
	install -D --mode 0644 src/field.glade ${DESTDIR}${DATADIR}/ui/field.glade
	install -D --mode 0644 src/fimage.glade ${DESTDIR}${DATADIR}/ui/fimage.glade
	install -D --mode 0644 src/fnumber.glade ${DESTDIR}${DATADIR}/ui/fnumber.glade
	install -D --mode 0644 src/frame.glade ${DESTDIR}${DATADIR}/ui/frame.glade
	install -D --mode 0644 src/fsum.glade ${DESTDIR}${DATADIR}/ui/fsum.glade
	install -D --mode 0644 src/ftext.glade ${DESTDIR}${DATADIR}/ui/ftext.glade
	install -D --mode 0644 src/image.glade ${DESTDIR}${DATADIR}/ui/image.glade
	install -D --mode 0644 src/label_page_setup.glade ${DESTDIR}${DATADIR}/ui/label_page_setup.glade
	install -D --mode 0644 src/line.glade ${DESTDIR}${DATADIR}/ui/line.glade
	install -D --mode 0644 src/mainwindow.glade ${DESTDIR}${DATADIR}/ui/mainwindow.glade
	install -D --mode 0644 src/page_number.glade ${DESTDIR}${DATADIR}/ui/page_number.glade
	install -D --mode 0644 src/query.glade ${DESTDIR}${DATADIR}/ui/query.glade
	install -D --mode 0644 src/README.glade ${DESTDIR}${DATADIR}/ui/README
	install -D --mode 0644 src/runreport.glade ${DESTDIR}${DATADIR}/ui/runreport.glade
	install -D --mode 0644 src/scrollwin.glade ${DESTDIR}${DATADIR}/ui/scrollwin.glade
	install -D --mode 0644 src/special.glade ${DESTDIR}${DATADIR}/ui/special.glade
	install -D --mode 0644 src/text.glade ${DESTDIR}${DATADIR}/ui/text.glade

.PHONY: uninstall_Linux
uninstall_Linux:
	@echo uninstalling iwrite
	@rm -f ${DESTDIR}${BINDIR}/iwrite
	@rm -f ${DESTDIR}${LIBDIR}/libiwsql_pq.so 
	@rm -f ${DESTDIR}${LIBDIR}/libiwsql_mariadb.so 
	@rm -f ${DESTDIR}${LIBDIR}/libiwsql_sqlite3.so 
	@rm -f ${DESTDIR}${LIBDIR}/libiwsql_oracle.so 
	@rm -f ${DESTDIR}${CFGDIR}/iwrite_driver.cfg
	@rm -rf ${DESTDIR}${DOCDIR}
	@rm -rf ${DESTDIR}${DATADIR}

doc/iwrite.pdf:
	@cd doc && make

src/iwrite: 
	@cd src && ${MAKE}

drivers/postgres/libiwsql_pq.so: 
	@cd drivers && ${MAKE} postgres

drivers/mariadb/libiwsql_mariadb.so:
	@cd drivers && ${MAKE} mariadb

drivers/sqlite3/libiwsql_sqlite3.so:
	@cd drivers && ${MAKE} sqlite3

drivers/oracle/libiwsql_oracle.so:
	@cd drivers && ${MAKE} oracle

