#
# Copyright (c) 2003 Linuxant inc.
# Copyright (c) 2001-2003 Conexant Systems, Inc.
#
# NOTE: The use and distribution of this software is governed by the terms in
# the file LICENSE, which is included in the package. You must read this and
# agree to these terms before using or distributing this software.
#
TOP= .

include $(TOP)/config.mak
-include $(TOP)/imported/makeflags.mak

DEBUG_TOOLS =
# Include the DEBUG_TOOLS if they are supported
ifeq ($(IMPORTED_SCR_SUPPORT),yes)
DEBUG_TOOLS+= dbgsrv qtmodemon
endif
ifeq ($(IMPORTED_DMP_SUPPORT),yes)
DEBUG_TOOLS+= dmpsrv
endif

SUBDIRS = inf inf2bin firm2bin scripts modules $(DEBUG_TOOLS)

.PHONY: default
default:
	@echo "Use:"
	@echo "    \"make install\" to install this software"
#	@echo "    \"make modules\" to recompile the kernel modules (normally done by hsfconfig)"
	@echo "    \"make clean\" to remove objects and other derived files"
	@echo "    \"hsfconfig\" (after installation) to setup your modem"
	@echo ""
	@false

all: inf2bin firm2bin scripts $(DEBUG_TOOLS)

imported:
	@[ -d imported ] || (echo "\"imported\" directory missing!"; exit 1)

$(SUBDIRS) install:: imported

.PHONY: $(SUBDIRS)
$(SUBDIRS)::
	$(MAKE) -C $@ all

.PHONY: uninstall
uninstall::
	if [ -x $(HSFSBINDIR)/hsfconfig ]; then \
		$(HSFSBINDIR)/hsfconfig -remove; \
	else \
		true; \
	fi

.PHONY: clean install
clean install uninstall::
	@for subdir in $(SUBDIRS); do \
		$(MAKE) -C $$subdir $@ || exit $$?; \
	done

install:: $(HSFLIBDIR) LICENSE
	$(INSTALL) -m 444 LICENSE $(HSFLIBDIR)
	@echo ""
	@echo "To complete the installation and configuration of your HSF modem,"
	@echo "please run \"hsfconfig\" (or \"$(HSFSBINDIR)/hsfconfig\")"

$(HSFLIBDIR):
	$(MKDIR) -p $@

uninstall::
	rm -f $(HSFLIBDIR)/LICENSE

TARDIST_EXCLUDES= *,v hsflinmodem*.tar.gz binaries/* *.pdf

ifeq ($(DEBUG_TOOLS),)
TARDIST_EXCLUDES+= *dbgscr* *dbgsrv* *dmpsrv* qtmodemon
endif

HSFTEMPDIST=/tmp/hsfdist/hsflinmodem-$(HSFLINUXVERSION)
.PHONY: tardist
tardist: hsflinmodem.spec
	rm -rf $(HSFTEMPDIST)
	$(MKDIR) -p $(HSFTEMPDIST)
	[ -d modules/binaries ] || $(MKDIR) modules/binaries
	find . -depth -print | grep -v '^\.\/packages\/' | cpio -pdm $(HSFTEMPDIST)
	(cd /tmp/hsfdist && tar $(patsubst %, --exclude '%', $(TARDIST_EXCLUDES)) -cf - hsflinmodem-$(HSFLINUXVERSION)) | gzip > hsflinmodem-$(HSFLINUXVERSION).tar.gz
	rm -rf $(HSFTEMPDIST)

# Test if our rpm supports --define and --eval options. Early versions didn't
RPMOPTDEFINE=$(shell rpm --define 'test test' >/dev/null 2>&1 && echo yes)
RPMOPTEVAL=$(shell rpm --eval 'test' >/dev/null 2>&1 && echo yes)

ifeq ($(RPMOPTDEFINE),yes)
# We're fine
RPMTOPDIR=$(CURDIR)/packages
else
ifeq ($(RPMOPTEVAL),yes)
RPMTOPDIR=$(shell rpm --eval '%_topdir')
else
RPMTOPDIR=
endif
endif
RPMTOPDIRDEFINED=$(shell grep -q '%_topdir' $$HOME/.rpmmacros 2>/dev/null && echo yes)

ifneq ($(RPMOPTDEFINE),yes)
UID=$(shell id -u 2>/dev/null)
ifneq ($(UID),0)
ifneq ($(RPMTOPDIRDEFINED),yes)
$(warning If you cannot run make as root and the rpm creation fails with)
$(warning a Permission denied error, try adding the line:)
$(warning %_topdir $(CURDIR)/packages)
$(warning to your ~/.rpmmacros file, and creating under packages)
$(warning the BUILD/ RPMS/ SPECS/ SRPMS/ subdirectories.)
endif
endif
endif

ifneq ($(RPMTOPDIR),)
RPMDIRS=$(patsubst %, $(RPMTOPDIR)/%, BUILD RPMS SPECS SRPMS)

$(RPMDIRS):
	mkdir -p $@
endif

DISTROKERNHDRSDIR= /usr/src/distrokernhdrs

RPMBUILD= $(shell test -x /usr/bin/rpmbuild && echo rpmbuild || echo rpm)

RPMBUILDARGS=
ifeq ($(RPMOPTDEFINE),yes)
RPMBUILDARGS+=	--define='_topdir $(RPMTOPDIR)'
RPMBUILDARGS+=	--define='_distro_kernels $(DISTROKERNHDRSDIR)'
RPMBUILDARGS+=	--define='_unpackaged_files_terminate_build 0'
endif

DISTROS= generic
TARGETS_generic= i386
KERNELS_generic= generic

-include $(DISTROKERNHDRSDIR)/incavail.mak

.PHONY: rpmdist
rpmdist: tardist $(RPMDIRS)
	for d in $(DISTROS); do \
		$(MAKE) _buildrpm BASEDISTRO=`echo $$d | sed 's/[0-9]*//g'` DISTRO=$$d || exit $$?; \
	done

.PHONY: _buildrpm
_buildrpm:
	@for t in $(TARGETS_$(BASEDISTRO)); do \
		for k in $(KERNELS_$(DISTRO)); do \
			echo ""; \
			echo "==========================================================================="; \
			echo "Now building rpm for $(BASEDISTRO) kernel $$k cpu $$t"; \
			echo "==========================================================================="; \
			$(RPMBUILD) -t`[ "$$k" = "generic" ] && echo a || echo b` --define="_target_distro $(BASEDISTRO)" --define="_target_kernel $$k" $(RPMBUILDARGS) --target $$t hsflinmodem-$(HSFLINUXVERSION).tar.gz || exit $$?; \
		done; \
	done

hsflinmodem.spec: hsflinmodem.spec.in CHANGES clean
	( \
	  sed -n \
		-e 's!@HSFLINUXVERSION@!$(HSFLINUXVERSION)!g' \
		-e 's!@HSFLINUXRPM_REL@!$(HSFLINUXRPM_REL)!g' \
		-e "s!@SCR_SUPPORT@!`test ! \"$(IMPORTED_SCR_SUPPORT)\" = yes; echo $$?`!g" \
		-e "s!@DMP_SUPPORT@!`test ! \"$(IMPORTED_DMP_SUPPORT)\" = yes; echo $$?`!g" \
		-e '/@LICENSE_FILE@/ {' \
		-e 'r LICENSE' \
		-e 'd' \
		-e '}' \
		-e 'p' \
		< $< ; \
	  sed -e 's/^\([A-Z]\)/* \1/' -e 's/^	\* /	- /' < CHANGES \
	) > $@
	chmod 644 hsflinmodem.spec

clean::
	rm -f hsflinmodem.spec packages/SPECS/hsflinmodem.spec

pkgclean: clean
	rm -f hsflinmodem*.tar.gz
	rm -f $(RPMTOPDIR)/RPMS/*/hsflinmodem* $(RPMTOPDIR)/SRPMS/hsflinmodem*
	rm -rf $(RPMTOPDIR)/BUILD/hsflinmodem*
	rm -rf modules/binaries/*

rpmsign:
	echo 'if {$$argc >= 4} { set pf [lindex $$argv 0] ; eval spawn [lrange $$argv 1 end]; expect "Enter pass phrase:"; send "$$pf\r"; expect eof }' | ( expect -f - "$(GPGPASSPHRASE)" rpm --resign `find packages -name "*.rpm"`)

lnxt_rel: rpmsign
	ssh -n v 'd=/var/www/linuxant.com/drivers/hsf/archive/hsflinmodem-$(HSFLINUXVERSION); rm -rf $$d; mkdir $$d'
	scp hsflinmodem-$(HSFLINUXVERSION).tar.gz *.pdf packages/RPMS/*/*.rpm packages/SRPMS/*.src.rpm v:/var/www/linuxant.com/drivers/hsf/archive/hsflinmodem-$(HSFLINUXVERSION)
	ssh -n v 'cd /var/www/linuxant-build/drivers/extracted && tar xzf /var/www/linuxant.com/drivers/hsf/archive/hsflinmodem-$(HSFLINUXVERSION)/hsflinmodem-$(HSFLINUXVERSION).tar.gz'

nf_rel:
	ssh -n marc@cvs.netfilter.org 'd=public_html/dl/hsflinmodem-$(HSFLINUXVERSION); rm -rf $$d; mkdir $$d'
	scp hsflinmodem-$(HSFLINUXVERSION).tar.gz packages/RPMS/*/*.rpm packages/SRPMS/*.src.rpm marc@cvs.netfilter.org:public_html/dl/hsflinmodem-$(HSFLINUXVERSION)

