#!/usr/bin/make -f 					
# 							-*- makefile -*-
# debian/rules file for the Debian/GNU Linux gretl package
# Copyright 2000 - 2023 by Dirk Eddelbuettel <edd@debian.org>

package := $(shell head -1 debian/changelog | awk '{print $$1}')
sonum   := 1
libpack := libgretl$(sonum)
devpack := libgretl$(sonum)-dev
docpack := $(package)-doc
datpack := $(package)-data
compack := $(package)-common
srcver  := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f2 -d' ' | cut -f1 -d'-')
debtmp	:= $(CURDIR)/debian/$(package)

arch 	:= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

cppflags  = $(shell dpkg-buildflags --get CPPFLAGS)
cflags    = $(shell dpkg-buildflags --get CFLAGS)
ldflags   = $(shell dpkg-buildflags --get LDFLAGS)

## edd 09 Feb 2003      no optimisation or debugging on m68k
ifneq "$(findstring $(arch), m68k-linux arm-linux)" ""
cflags	= -O0 -g0 -Wall
endif

# Request that TeX seed the \today (etc.) from SOURCE_DATE_EPOCH
export FORCE_SOURCE_DATE = 1

#export DH_VERBOSE=1

upstream: get-orig-source
get-orig-source:
        # lynx http://gretl.sourceforge.net
        # lynx http://unc.dl.sourceforge.net/sourceforge/gretl
	links http://prdownloads.sourceforge.net/gretl
        # links http://twtelecom.dl.sourceforge.net/sourceforge/gretl

configure: configure-stamp
configure-stamp:
	dh_testdir
	dh_update_autotools_config
	DISPLAY="foo" 					\
	./configure	--prefix=/usr			\
			--with-x-12-arima		\
			--with-tramo-seats		\
			--with-libR			\
			--with-odbc			\
			--with-mpi           		\
			--enable-static			\
			--enable-shared			\
			--enable-build-editor		\
			--enable-build-addons		\
			--disable-rpath			\
			--disable-sse2			\
			--disable-avx			\
			--disable-xdg-utils		\
			--build $(arch)
	touch configure-stamp

## edd 02 May 2010  these are now implicit and/or obsolete
#   --with-lapack			
#   --with-gtk2			
#   --without-libole2		
#  --without-gtkextra		
#
# edd 12 Mar 2011   cannot buikd wuth this
#			--disable-sse2
# edd 11 Jun 2017
#			--with-mpfr 			

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir
	$(MAKE) CC=gcc \
		CFLAGS="$(cflags) -D_REENTRANT" \
		CPPFLAGS="$(cppflags)" \
		LDFLAGS="$(ldflags)"
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	-(cd gui && make clean)
	-find . -name \*.o -type f | xargs rm -vf 
	dh_update_autotools_config
	dh_clean build-stamp install-stamp configure-stamp check-stamp

check: build check-stamp
check-stamp:
	-$(MAKE) check
	touch check-stamp

install: build check install-stamp 
install-stamp: 
	$(MAKE) -f debian/rules DH_OPTIONS= install-work
install-work:
	dh_testdir
	dh_testroot
        #dh_clean -k
	dh_prep
	dh_installdirs	
	dh_installdirs -p$(package) 	usr/share/gnome/apps/Applications \
					usr/share/applications \
					usr/share/mime-info \
					usr/share/pixmaps 
	-$(MAKE) prefix=$(debtmp)/usr 			\
		gnome_prefix=$(debtmp)/usr 		\
		mandir=$(debtmp)/usr/share/man		install
#	(cd gnome && $(MAKE) prefix=$(debtmp)/usr 	\
#		gnome_prefix=$(debtmp)/usr 		\
#		mandir=$(debtmp)/usr/share/man		install-misc )

        #	infodir=$(debtmp)/usr/share/info	\
        # dh_undocumented		gretl.1 gretlcli.1 gretl_x11.1

        # clean extra COPYING file
	(cd $(debtmp)/usr/share/$(package); \
		ln -svf ../common-licenses/GPL COPYING)

        ## this seems to have been forgotten at install time (version 1.1.0)
        ## gretl 1.8.7 no longer has it
        #chmod 755 gretl-config
        #cp -vax gretl-config debian/gretl/usr/bin

        ## Bug#400405 to get pixmap into desktop menu (also dh_movefiles)
	cp -vax pixmaps/gretl.xpm debian/gretl/usr/share/pixmaps

        # move static libs into dev-package  and binaries into bin package
	dh_movefiles 	--sourcedir=debian/$(package) -p$(devpack)
	dh_movefiles 	--sourcedir=debian/$(package) -p$(libpack)
        # move docs
        ## dh_movefiles 	--sourcedir=debian/$(package) -p$(docpack)

        # move common and data package files
	dh_movefiles 		--sourcedir=debian/$(package) -p$(datpack)
	dh_movefiles 		--sourcedir=debian/$(package) -p$(compack)
	dh_installdocs 		-p$(compack)	
	dh_installdocs		-p$(docpack) 	README* doc/
	dh_installexamples      -p$(docpack)	utils/
        #dh_movefiles 		--sourcedir=debian/$(package) -p$(docpack)

        # edd 19 Mar 2009  remove fonts deemed non-free
	rm -vrf debian/$(compack)/usr/share/gretl/fonts

        # edd 24 Jul 2023  remove empty pkgconfig dir (#1041835)
	find debian/$(package)/usr/lib/pkgconfig -type d -empty -delete

        # edd 26 Dec 2007  some lintian silencers
#	for p in $(package) $(libpack) $(compack); do \
#		dh_installdirs -p$$p usr/share/lintian/overrides; \
#		install -v -c -m 0644 debian/$$p.overrides \
#		   $(CURDIR)/debian/$$p/usr/share/lintian/overrides/$$p; \
#		done
	dh_lintian

        # edd 23 Apr 2011
        # empty dependency_lib in .la files (cf #621213)	
	find debian/$(package) -name \*.la | \
		xargs perl -p -i -e "s/dependency_libs='.*'/dependency_libs=''/" 
	find debian/$(libpack) -name \*.la | \
		xargs perl -p -i -e "s/dependency_libs='.*'/dependency_libs=''/" 

	touch install-stamp

# This single target is used to build all the packages, all at once, or
# one at a time. So keep in mind: any options passed to commands here will
# affect _all_ packages. Anything you want to only affect one package
# should be put in another target, such as the install target.
binary-common: build check install 
	dh_testdir
	dh_testroot
        #dh_installdebconf
	dh_link			
	dh_installdocs -a
	(cd $(debtmp)/usr/share/$(package); 		\
		rmdir doc;                              \
		ln -s ../doc/$(package)/doc . ;         \
		ln -s ../doc/$(package)/examples . 	 )
        #dh_installexamples
        #dh_installmenu
        #dh_installemacsen
        #dh_installpam
        #dh_installinit
        #dh_installcron
        #dh_installman		gretl.1
        #dh_installinfo
        #dh_undocumented
	dh_installchangelogs			ChangeLog
	dh_compress
	dh_fixperms
	dh_strip --no-automatic-dbgsym
        #dh_suidregister
        #echo "libgretl-1.0 0 libgretl1 (>= ${srcver})" \
        #	> debian/$(libpack).shlibs
#	dh_makeshlibs 	-V -p$(package) -p$(devpack) -p$(libpack)
	dh_makeshlibs 	-V -n -p$(devpack) 
	dh_makeshlibs 	-p$(libpack)
	dh_installdeb
#	dh_desktop 	-p$(compack)
        #dh_perl
	dh_shlibdeps	-l debian/$(libpack)/usr/lib/ 
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build install
	#$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common
	dh_testdir -i
	dh_testroot -i
	dh_link -i			
	dh_installchangelogs -i
        #dh_installdocs -i
        #dh_installexamples -i
#	dh_installmenu -i
#	dh_installdebconf -i
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installcatalogs -i
#	dh_installpam -i
#	dh_installmime -i
#	dh_installinit -i
#	dh_installcron -i
#	dh_installinfo -i
#	dh_undocumented -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
#	dh_perl -i
#	dh_python -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture dependant packages using the common target.
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

# Any other binary targets build just one binary package at a time.
binary-%: build install
	make -f debian/rules binary-common DH_OPTIONS=-p$*

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

