#!/usr/bin/make -f

# debug
#export DH_VERBOSE=y
#export TO_NULL=&>/dev/null
#TO_NULL=&>/dev/null
#MAKEFLAGS+=-s --no-print-directory

#export DH_OPTIONS

# used by debian/config-for-lwng that is sourced by ./Configure
# in this way no modules are built and no errors are reported if there are no
# kenrel headers
ENV=BUILD_MODULES=n BUILD_UTILS=y

TARGET_DIR=debian/tmp/etc/wlan/
TARGET_DIR_COPYRIGHT=debian/tmp/usr/share/doc/linux-wlan-ng-firmware-files/

###################### CONFIGURATION ###################################
configure-stamp:
	@echo detected flavours for current arch are: $(flavours)
	# to make the configure script happy we have to provide a real
	# kernel source tree.
	$(ENV) CC=$(CC) ./Configure -d debian/config-for-lwng $(TO_NULL)
	touch configure-stamp


# install stuff that is arch dependent
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# firmware files	
	mkdir -p $(TARGET_DIR)
	for x in src/prism2/*.hex ; do \
		if [ -f "$$x" ]; then \
			SUFFIX=`echo $$x |  cut -c12-13`;\
			cp $$x $(TARGET_DIR)prism2_$$SUFFIX.hex ; \
		fi; \
	done

	# copyright
	mkdir -p $(TARGET_DIR_COPYRIGHT)
	cp src/prism2/README.firmware $(TARGET_DIR_COPYRIGHT)/copyright.firmware
	cp COPYING $(TARGET_DIR_COPYRIGHT)/copyright

	# prism2dl
	mkdir -p debian/tmp/sbin
	cp src/prism2/download/prism2dl debian/tmp/sbin/

	if [ -f man/prism2dl.1.no ]; then \
		mv man/prism2dl.1.no man/prism2dl.1; \
	fi

	touch install-stamp

binary-indep:

build-stamp: configure-stamp
	dh_testdir

# prism2dl binary
	$(MAKE) -C src/mkmeta $(TO_NULL)
	$(MAKE) -C src/prism2/download $(TO_NULL)

	touch build-stamp

build: build-stamp

############################ CLEAN ################################

clean:
	dh_clean debian/postinst

# stamps
	$(RM) configure-stamp build-stamp install-stamp 
	
########################## MAIN ##################################

binary-arch: install-stamp
	dh_installdirs
	dh_install
	dh_installchangelogs
	dh_strip 
	dh_fixperms
	dh_shlibdeps
	dh_installdeb
	dh_installman  $(DH_OPTIONS) man/prism2dl.1
	dh_gencontrol
	dh_compress
	dh_md5sums
	dh_builddeb
	
binary: binary-arch 

.PHONY: build clean binary-arch binary
