#!/usr/bin/make -f

# Debian Makefile for gnade
# Copyright (c) 2009, 2010 Stephen Leake <stephen_leake@stephe-leake.org>
# Copyright (c) 2003-2006 Ludovic Brenta <lbrenta@debian.org>
#
# 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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA
#
# To setup for building this package from the latest Debian:
# sudo schroot -d `cd ..; pwd` --chroot=unstable -- apt-get build-dep gnade
#
# To build this package in a chroot:
# schroot -d `cd ..; pwd` --chroot=unstable -- dpkg-buildpackage -uc -us
#
# To run lintian:
# schroot -d `cd ..; pwd` --chroot=unstable -- make -f debian/rules lint
#
# To install the packages and run the examples:
# sudo /etc/init.d/mysql stop
# schroot -d `cd ..; pwd` --chroot=unstable -- make -f debian/rules run_test
#
# To review changes from previous version:
# schroot -d `cd ..; pwd` --chroot=unstable -- debdiff

.SUFFIXES=

# We use gnatmake, not make, for parallel builds.
.NOTPARALLEL:
CPUS := $(shell getconf _NPROCESSORS_ONLN)

# get soversions, aliversions from debian/control
common_soversion  := $(shell sed -n -e "s/^Package: libgnadecommon\([0123456789]\+\)$$/\1/p" debian/control)
odbc_soversion 	  := $(shell sed -n -e "s/^Package: libgnadeodbc\([0123456789]\+\)$$/\1/p" debian/control)
sqlite3_soversion := $(shell sed -n -e "s/^Package: libgnadesqlite3-\([0123456789]\+\)$$/\1/p" debian/control)

common_aliversion  := $(shell sed -n -e "s/^Package: libgnadecommon\(.\+\)-dev$$/\1/p" debian/control)
odbc_aliversion    := $(shell sed -n -e "s/^Package: libgnadeodbc\(.\+\)-dev$$/\1/p" debian/control)
sqlite3_aliversion := $(shell sed -n -e "s/^Package: libgnadesqlite3-\(.\+\)-dev$$/\1/p" debian/control)

# Targets mandated by the Debian Policy

.PHONY: build build-arch build-indep binary binary-arch binary-indep clean

build: build-arch build-indep
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -f debian/rules check
endif

libs := common odbc sqlite3

build-arch: $(addprefix debian/libgnade,$(addsuffix -stamp,$(libs)))

build-indep:

all_packages := $(foreach l,$(libs), libgnade$(l)-so libgnade$(l)-dev) libgnadeodbc-example libgnadesqlite3-example

clean:
	dh_clean
	rm -f debian/*-stamp
	rm -f debian/gnadesqlite3*-dev.README.Debian
	rm -f debian/gnadeodbc*-dev.README.Debian

binary: binary-arch

binary-indep:

# we ignore the warnings about "dependency on libgnadecommon.so.1
# could be avoided" for libgnadeodbc; see gnade_odbc_build.gpr for
# more info.
binary-arch: build-arch testroot $(all_packages)
	dh_shlibdeps -a
	dh_installdeb -a
	dh_installdocs -a
	dh_installchangelogs -a
	dh_compress -a
	dh_fixperms -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

# Helper targets

.PHONY: testroot

testroot:
	dh_testdir
	dh_testroot

##############################################################################
# Targets that build.

odbc_gpb_files := $(wildcard dbi/odbc/*.gpb)
odbc_preprocessed_files := $(patsubst %.gpb,debian/tmp/odbc-prep/%.adb,$(notdir $(odbc_gpb_files)))

.PRECIOUS: debian/tmp/odbc-prep debian/tmp/odbc-example-prep

GNATPREP_FLAGS=-r -DDEBUG=False -DUNICODE=True -DCALLCONVENTION=C -DLINKOPT=\"-lodbc\"

debian/tmp/odbc-prep/%.adb: dbi/odbc/%.gpb | debian/tmp/odbc-prep
	gnatprep $(GNATPREP_FLAGS) $< $@

# Make the libraries for a package.
debian/libgnade%-stamp:
	gnatmake -p -j$(CPUS) -Pdebian/gnade_$*_build -XLIBTYPE=static  -Xsoversion=$($*_soversion)
	gnatmake -p -j$(CPUS) -Pdebian/gnade_$*_build -XLIBTYPE=dynamic -Xsoversion=$($*_soversion)
	touch $@

debian/libgnadeodbc-stamp: $(odbc_preprocessed_files) debian/gnadeodbc$(odbc_aliversion)-dev.README.Debian

debian/libgnadeodbc-stamp debian/libgnadesqlite3-stamp: debian/libgnadecommon-stamp

debian/libgnadesqlite3-stamp: debian/gnadesqlite3-$(sqlite3_aliversion)-dev.README.Debian

debian/gnade%-dev.README.Debian:
	cd debian; ln -s gnadecommon$(common_aliversion)-dev.README.Debian gnade$*-dev.README.Debian

# Make a directory.
debian/tmp/%-prep:
	-mkdir -p $@

##############################################################################
# Targets that move files into the proper packages

# Install a shared object library package
libgnadecommon-so:  package := libgnadecommon$(common_soversion)
libgnadeodbc-so:    package := libgnadeodbc$(odbc_soversion)
libgnadesqlite3-so: package := libgnadesqlite3-$(sqlite3_soversion)
libgnade%-so: debian/libgnade%-stamp
	dh_installdirs -p$(package) usr/lib
	dh_install -p$(package) debian/tmp/libgnade$*.so.$($*_soversion) usr/lib
	dh_strip -p$(package) --dbg-package=libgnade$*-dbg
	dh_makeshlibs -p$(package)
	dh_lintian -p$(package)

# Install a -dev and -dbg package.
libgnadecommon-dev:  package := libgnadecommon$(common_aliversion)-dev
libgnadeodbc-dev:    package := libgnadeodbc$(odbc_aliversion)-dev
libgnadesqlite3-dev: package := libgnadesqlite3-$(sqlite3_aliversion)-dev
libgnade%-dev: debian/libgnade%-stamp
	dh_installdirs -p$(package)  usr/share/ada/adainclude/gnade$* usr/lib/ada/adalib/gnade$*
	dh_install -p$(package) debian/tmp/$*-dynamic/*.ali usr/lib/ada/adalib/gnade$*
	if [ -d dbi/$* ]; then \
	   dh_install -p$(package) dbi/$*/*.ad[bs] usr/share/ada/adainclude/gnade$* ; \
	else \
	   dh_install -p$(package) support/*.ad[bs] usr/share/ada/adainclude/gnade$* ; \
	fi
	if [ -d debian/tmp/$*-prep ]; then dh_install -p$(package) debian/tmp/$*-prep/*.ad[bs] usr/share/ada/adainclude/gnade$*; fi
	dh_install -p$(package) debian/tmp/libgnade$*.a /usr/lib
	dh_link -p$(package) /usr/lib/libgnade$*.so.$($*_soversion) /usr/lib/libgnade$*.so
	dh_install -p$(package) debian/gnade$*.gpr /usr/share/ada/adainclude

# dh_installexamples compresses demo.adb. The Debian Ada Policy says
# to not compress, but any decent editor can read a compressed file.
libgnadeodbc-example: package := libgnadeodbc$(odbc_aliversion)-dev
libgnadeodbc-example: debian/tmp/odbc-example-prep/demo.adb
	dh_installexamples -p$(package) debian/tmp/odbc-example-prep/demo.adb

libgnadesqlite3-example: package := libgnadesqlite3-$(sqlite3_aliversion)-dev
libgnadesqlite3-example:
	dh_installexamples -p$(package) samples/sqlite3/demo.adb

# Check the build by running some examples.
check: debian/check-sqlite3-stamp

# create 'gnade' mysql account, 'gnade' mysql database
# install 'MySQL' odbc driver, 'gnade_mysql' odbc source
# assumes mysql root account has no password
# assumes mysqld in main install has been stopped; use 'sudo /etc/init.d/mysql stop'
# assumes mysqld in main chroot has been started; use 'sudo schroot -- /etc/init.d/mysql start'
# assumes gnade db and user have not already been created
debian/odbc-source-stamp:
	mysqladmin -uroot create gnade
	echo "create user gnade identified by 'gnade'" | mysql -uroot
	echo "grant all on gnade.* to gnade" | mysql -uroot
	mysql -ugnade -pgnade < samples/sample_db/gnade.mysql.sql
	odbcinst -i -d -f debian/odbc_mysql_driver.ini
	odbcinst -i -s -l -f debian/odbc_mysql_source.ini
	touch $@

# Running the odbc example requires root privs to set it up, so we
# can't run it as part of the package build process. We keep the odbc
# target here so we can easily run it while developing the next
# release.
debian/check-odbc-stamp: debian/libgnadeodbc-stamp debian/odbc-source-stamp debian/tmp/odbc-example-prep/demo.adb
	gnatmake -p -Pdebian/gnade_odbc_example_build -XLIBTYPE=dynamic
	export LD_LIBRARY_PATH=`pwd`/debian/tmp; debian/tmp/odbc-example-obj/demo
	touch $@

debian/tmp/odbc-example-prep/%.adb: samples/odbc/%.gpb | debian/tmp/odbc-example-prep
	gnatprep -r -DDBSOURCE=\"gnade_mysql\" -DDBUSER=\"gnade\" -DDBPASSWD=\"gnade\" $< $@

debian/check-sqlite3-stamp: debian/libgnadesqlite3-stamp
	gnatmake -p -Pdebian/gnade_sqlite3_example_build -XLIBTYPE=dynamic
	export LD_LIBRARY_PATH=`pwd`/debian/tmp; debian/tmp/sqlite3-example-obj/demo debian/tmp/demo.db
	touch $@

lint: version := $(shell dpkg-parsechangelog | grep "Version: " | sed "s/Version: //")
lint: arch := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
lint:
	cd ..; lintian -i gnade_$(version)_$(arch).changes

# get debian version (used by test_installed) from debian/changelog
export debversion := $(shell dpkg-parsechangelog | sed -r -n -e "s/^Version: [0123456789.]+-([0123456789]+)$$/\1/p")
export common_aliversion
export odbc_aliversion
export sqlite3_aliversion
export common_soversion
export odbc_soversion
export sqlite3_soversion
run_test:
	cd debian; ./test_installed.sh

# end of file
