#
# Copyright (c) 2001 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

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: clean 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

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

DISTROS= generic rh mdk suse

TARGETS_generic= i386
TARGETS_rh=		i386 i586 i686 athlon
TARGETS_mdk=	i586
TARGETS_suse=	i386

KERNELS_generic= generic

KERNELS_rh70=	2.2.16-22 2.2.19-7.0.16
KERNELS_rh71=	2.4.2-2 2.4.9-31 2.4.9-34
KERNELS_rh72=	2.4.7-10 2.4.9-31 2.4.9-34
KERNELS_rh73=	2.4.18-3 2.4.18-4 2.4.18-5 2.4.18-10

KERNELS_mdk80=	2.2.19-10mdk 2.4.3-20mdk 2.4.8-31.2mdk 2.2.19-20.1mdk
KERNELS_mdk81=	2.2.19-19mdk 2.4.8-26mdk 2.2.19-20.1mdk 2.4.8-34.1mdk
KERNELS_mdk82=	2.2.20-9mdk 2.4.18-6mdk 2.4.18-8.1mdk
KERNELS_mdk90=	2.4.19-16mdk

KERNELS_suse72=	2.4.7-4GB 2.4.16-4GB
KERNELS_suse73=	2.4.10-4GB 2.4.16-4GB
KERNELS_suse80=	2.4.18-4GB

# The GNU make sort function is used here to remove duplicates
KERNELS_rh=		$(sort $(KERNELS_rh70) $(KERNELS_rh71) $(KERNELS_rh72) $(KERNELS_rh73))
KERNELS_mdk=	$(sort $(KERNELS_mdk80) $(KERNELS_mdk81) $(KERNELS_mdk82) $(KERNELS_mdk90))
KERNELS_suse=	$(sort $(KERNELS_suse72) $(KERNELS_suse73) $(KERNELS_suse80))

.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
	( \
	  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/*

mbsi_rel:
	ssh -n v 'd=/var/www/mbsi.ca/cnxtlindrv/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/mbsi.ca/cnxtlindrv/hsf/archive/hsflinmodem-$(HSFLINUXVERSION)
	ssh -n v 'cd /var/www/mbsi.ca/cnxtlindrv/hsf && tar xzf archive/hsflinmodem-$(HSFLINUXVERSION)/hsflinmodem-$(HSFLINUXVERSION).tar.gz'

