#
# 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

SUBDIRS = inf scripts modules

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

all: scripts

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

$(SUBDIRS) install:: imported

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

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

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

install:: $(HCFLIBDIR) LICENSE
	$(INSTALL) -m 444 LICENSE $(HCFLIBDIR)
	@echo ""
	@echo "To complete the installation and configuration of your HCF modem,"
	@echo "please run \"$(HCFTARGET)config\" (or \"$(HCFSBINDIR)/$(HCFTARGET)config\")"


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

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

TARDIST_EXCLUDES= *,v $(HCFTARGET)modem*.tar.gz binaries/* *.pdf

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

# 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= $(IMPORTED_ARCH)
KERNELS_generic= generic

ifeq ($(IMPORTED_ARCH),i386)
-include $(DISTROKERNHDRSDIR)/incavail.mak
endif

.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_$(DISTRO)); do \
		for k in $(KERNELS_$(DISTRO)); do \
			echo ""; \
			echo "==========================================================================="; \
			echo "Now building rpm for distro $(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 $(HCFTARGET)modem-$(HCFLINUXVERSION).tar.gz || exit $$?; \
		done; \
	done

$(HCFTARGET)modem.spec: hcfmodem.spec.in CHANGES clean
	( \
	  sed -n \
		-e 's!@HCFTARGET@!$(HCFTARGET)!g' \
		-e 's!@HCFLINUXVERSION@!$(HCFLINUXVERSION)!g' \
		-e 's!@HCFLINUXRPM_REL@!$(HCFLINUXRPM_REL)!g' \
		-e "s!@DEBUG_BUILD@!`test ! \"$(IMPORTED_DEBUG_DEFS)\" = 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 $(HCFTARGET)modem.spec

clean::
	rm -f $(HCFTARGET)modem.spec packages/SPECS/$(HCFTARGET)modem.spec

pkgclean: clean
	rm -f $(HCFTARGET)modem*.tar.gz
	rm -f $(RPMTOPDIR)/RPMS/*/$(HCFTARGET)modem* $(RPMTOPDIR)/SRPMS/$(HCFTARGET)modem*
	rm -rf $(RPMTOPDIR)/BUILD/$(HCFTARGET)modem*
	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/hcf/archive/$(HCFTARGET)modem-$(HCFLINUXVERSION); rm -rf $$d; mkdir $$d'
	scp $(HCFTARGET)modem-$(HCFLINUXVERSION).tar.gz *.pdf packages/RPMS/*/*.rpm packages/SRPMS/*.src.rpm v:/var/www/linuxant.com/drivers/hcf/archive/$(HCFTARGET)modem-$(HCFLINUXVERSION)
	ssh -n v 'cd /var/www/linuxant-build/drivers/extracted && tar xzf /var/www/linuxant.com/drivers/hcf/archive/$(HCFTARGET)modem-$(HCFLINUXVERSION)/$(HCFTARGET)modem-$(HCFLINUXVERSION).tar.gz'

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

