#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# quilt
include /usr/share/quilt/quilt.make

DEBVERSION:=$(shell head -n 1 debian/changelog \
                    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
LOCAL_TAR_VER:=$(shell echo $(DEBVERSION) | sed -e 's/^.*://' -e 's/-[0-9.]*$$//')
UP_MAJOR_VER:=$(shell echo $(LOCAL_TAR_VER) | sed -e 's/~[0-9.]*//')
UP_DATE_VER:=$(shell echo $(LOCAL_TAR_VER) | sed -e 's/[0-9.]*~//')

UPFILENAME := lcr_$(UP_DATE_VER).tar.gz
FILENAME := lcr_$(LOCAL_TAR_VER).orig.tar.gz
URL := http://isdn.eversberg.eu/download/lcr-$(UP_MAJOR_VER)/$(UPFILENAME)

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif



config.status: patch configure
	dh_testdir
	# Add here commands to configure the package.
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	CPPFLAGS="-I/usr/include/mISDNuser" ./configure $(CROSS) --without-ssl --without-gsm --prefix=/usr --localstatedir=/var --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info CFLAGS="$(CFLAGS)" # LDFLAGS="-Wl,-z,defs"

build: build-stamp
build-stamp:  config.status 
	dh_testdir
	$(MAKE)
	touch $@

clean: clean-patched unpatch
clean-patched: 
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.sub config.guess
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) DESTDIR=$(CURDIR)/debian/lcr install
	rm $(CURDIR)/debian/lcr/usr/sbin/genrc


# Build architecture-independent files here.
binary-indep: install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installlogrotate
	dh_installinit  -- defaults 21
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

print-version:
	@@echo "Debian version:          $(DEBVERSION)"
	@@echo "Upstream version:        $(UP_DATE_VER)"

get-orig-source:
	@@dh_testdir
	@@[ -d ../tarballs/. ] || mkdir -p ../tarballs
	@@wget -nv -T10 -t3 --verbose -O ../tarballs/$(FILENAME) $(URL)

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