#
# 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
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: clean install uninstall
clean install uninstall::
	@for subdir in $(SUBDIRS); do \
		$(MAKE) -C $$subdir $@; \
	done

install:: $(HSFLIBDIR) LICENSE
	$(INSTALL) -m 444 LICENSE $(HSFLIBDIR)

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

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

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

TARDIST_EXCLUDES= *,v hsflinmodem*.tar.gz

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

HSFTEMPDIST=/tmp/hsfdist/hsflinmodem-$(HSFLINUXVERSION)
.PHONY: tardist
tardist: clean hsflinmodem.spec
	rm -rf $(HSFTEMPDIST)
	$(MKDIR) -p $(HSFTEMPDIST)
	find . -depth -print | grep -v '^\.\/packages\/' | cpio -pdm $(HSFTEMPDIST)
	(cd /tmp/hsfdist && tar $(patsubst %, --exclude '%', $(TARDIST_EXCLUDES)) -czf - hsflinmodem-$(HSFLINUXVERSION)) > 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
RPMBUILDARGS+=	--target i586

.PHONY: rpmdist
rpmdist: tardist $(RPMDIRS)
	rpm -ta $(RPMBUILDARGS) hsflinmodem-$(HSFLINUXVERSION).tar.gz

hsflinmodem.spec: hsflinmodem.spec.in CHANGES
	( \
	  sed \
		-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" \
		< $< ; \
	  sed -e 's/^\([A-Z]\)/* \1/' -e 's/^	\* /	- /' < CHANGES \
	) > $@
	chmod 644 hsflinmodem.spec

clean::
	rm -f hsflinmodem.spec

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

