#
# Copyright (c) 2001-2002 Conexant Systems, Inc.
# 
# 1.   Permitted use. Redistribution and use in source and binary forms,
# with or without modification, are permitted under the terms set forth
# herein.
# 
# 2.   Disclaimer of Warranties. CONEXANT AND OTHER CONTRIBUTORS MAKE NO
# REPRESENTATION ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.
# IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTIES OF ANY KIND.
# CONEXANT AND OTHER CONTRIBUTORS DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE, GOOD TITLE AND AGAINST INFRINGEMENT.
# 
# This software has not been formally tested, and there is no guarantee that
# it is free of errors including, but not limited to, bugs, defects,
# interrupted operation, or unexpected results. Any use of this software is
# at user's own risk.
# 
# 3.   No Liability.
# 
# (a) Conexant or contributors shall not be responsible for any loss or
# damage to Company, its customers, or any third parties for any reason
# whatsoever, and CONEXANT OR CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY
# ACTUAL, DIRECT, INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL, OR CONSEQUENTIAL
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED, WHETHER IN CONTRACT, STRICT OR OTHER LEGAL THEORY OF
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
# 
# (b) User agrees to hold Conexant and contributors harmless from any
# liability, loss, cost, damage or expense, including attorney's fees,
# as a result of any claims which may be made by any person, including
# but not limited to User, its agents and employees, its customers, or
# any third parties that arise out of or result from the manufacture,
# delivery, actual or alleged ownership, performance, use, operation
# or possession of the software furnished hereunder, whether such claims
# are based on negligence, breach of contract, absolute liability or any
# other legal theory.
# 
# 4.   Notices. User hereby agrees not to remove, alter or destroy any
# copyright, trademark, credits, other proprietary notices or confidential
# legends placed upon, contained within or associated with the Software,
# and shall include all such unaltered copyright, trademark, credits,
# other proprietary notices or confidential legends on or in every copy of
# the 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

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_$(BASEDISTRO)); 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/*

mbsi_rel:
	ssh -n v 'd=/var/www/mbsi.ca/cnxtlindrv/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/mbsi.ca/cnxtlindrv/hcf/archive/$(HCFTARGET)modem-$(HCFLINUXVERSION)
	ssh -n v 'cd /var/www/mbsi.ca/cnxtlindrv/hcf && tar xzf 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)

