#!/usr/bin/make -f

testdir  = test -f src/wmweather.c && test -f debian/rules
testroot = test x`whoami` = xroot

# FOR AUTOCONF 2.52 AND NEWER ONLY
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	CONFFLAGS += --build $(DEB_HOST_GNU_TYPE)
else
	CONFFLAGS += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS   = $(shell dpkg-buildflags --get CFLAGS) -Wno-error=format-security
LDFLAGS  = $(shell dpkg-buildflags --get LDFLAGS)
export CPPFLAGS
export CFLAGS
export LDFLAGS

clean:
	$(testdir)
	rm -f build-stamp debian/files debian/substvars
	[ ! -f src/Makefile ] || $(MAKE) -C src -i distclean
	rm -rf debian/wmweather

build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp:
	$(testdir)
	cd src && ./configure $(CONFFLAGS) --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --with-xmessage=/usr/bin/xmessage
	$(MAKE) -C src
	touch $@

binary: binary-arch binary-indep

binary-arch: build
	$(testdir)
	$(testroot)
	rm -rf debian/wmweather

	$(MAKE) -C src install DESTDIR=$(CURDIR)/debian/wmweather
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	strip -R .comment -R .note debian/wmweather/usr/bin/wmweather
endif
	gzip -9n debian/wmweather/usr/share/man/man1/wmweather.1
	rm -f debian/wmweather/usr/share/man/man1/wmWeather.1
	ln -s wmweather.1.gz debian/wmweather/usr/share/man/man1/wmWeather.1.gz
	install -D -p -m 0644 src/wmweather.conf debian/wmweather/etc/wmweather.conf
	install -D -p -m 0755 debian/ip-up debian/wmweather/etc/ppp/ip-up.d/wmweather
	install -D -p -m 0644 debian/menu  debian/wmweather/usr/share/menu/wmweather

	install -D -p -m 0644 CHANGES debian/wmweather/usr/share/doc/wmweather/changelog
	install -D -p -m 0644 debian/changelog debian/wmweather/usr/share/doc/wmweather/changelog.Debian
	install    -p -m 0644 README debian/copyright debian/wmweather/usr/share/doc/wmweather
	gzip -9n debian/wmweather/usr/share/doc/wmweather/changelog*

	install -d -p -m 0755 debian/wmweather/DEBIAN
	install    -p -m 0644 debian/control debian/conffiles debian/wmweather/DEBIAN
	install    -p -m 0755 debian/postinst debian/postrm debian/wmweather/DEBIAN
	cd debian/wmweather && find usr -type f -print0 | xargs -0 md5sum > DEBIAN/md5sums

	dpkg-shlibdeps debian/wmweather/usr/bin/wmweather
	dpkg-gencontrol -isp -pwmweather -Pdebian/wmweather
	chmod -R u+w,go=u-w debian/wmweather
	dpkg --build debian/wmweather ..

binary-indep:

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