# 
# Copyright (C) 2006 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=qoscom
PKG_VERSION:=1.02
PKG_RELEASE:=16


PKG_BUILD_DIR:=$(KERNEL_BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION).$(PKG_RELEASE)
PKG_INSTALL_DIR:=$(PKG_BUILD_DIR)/ipkg-install


PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).$(PKG_RELEASE).tar.gz
PKG_CAT:=zcat

include $(INCLUDE_DIR)/package.mk

define KernelPackage/qoscom
  CATEGORY:=Mindspeed Packages
  DEFAULT:=y
  DEPENDS:=@TARGET_comcerto1000
  TITLE:=Comcerto 1000 QoS tools 
  DESCRIPTION:= QoS package for Comcerto 1000 
  URL:=http://www.mindspeed.com/
  MAINTAINER:=Mindspeed Technologies
  FILES:= $(PKG_BUILD_DIR)/qoscom.$(LINUX_KMOD_SUFFIX)
endef

#If Development, we should download the version from our CVS instead of doing the generic process
ifeq ($(CONFIG_DEV_QOSCOM), y)

ifeq ($(CONFIG_DEV_QOSCOM_CVSBRANCH), "")
define Build/Prepare
	pushd "$(KERNEL_BUILD_DIR)" && \
	cvs -z9 -d:pserver:$(CONFIG_ACTIVEDEV_CVSUSERNAME)@$(CONFIG_ACTIVEDEV_CVSROOTPATH):/prj/msacvs/enterprise checkout -d $(PKG_NAME)-$(PKG_VERSION).$(PKG_RELEASE) $(CONFIG_DEV_QOSCOM_MODULENAME) &&\
	popd
	@if [ -d ./patches -a "$$$$(ls ./patches | wc -l)" -gt 0 ]; then \
		$(PATCH) $(PKG_BUILD_DIR) ./patches; \
	fi
endef

else
define Build/Prepare
	pushd "$(KERNEL_BUILD_DIR)" && \
	cvs -z9 -d:pserver:$(CONFIG_ACTIVEDEV_CVSUSERNAME)@$(CONFIG_ACTIVEDEV_CVSROOTPATH):/prj/msacvs/enterprise checkout -d $(PKG_NAME)-$(PKG_VERSION).$(PKG_RELEASE) -r $(CONFIG_DEV_QOSCOM_CVSBRANCH) $(CONFIG_DEV_QOSCOM_MODULENAME) &&\
	popd
	@if [ -d ./patches -a "$$$$(ls ./patches | wc -l)" -gt 0 ]; then \
		$(PATCH) $(PKG_BUILD_DIR) ./patches; \
	fi
endef

endif
endif


define Build/Configure
	(cd $(PKG_BUILD_DIR)/lib; rm -rf config.{cache,status} ; \
                $(TARGET_CONFIGURE_OPTS) \
                CFLAGS="$(TARGET_CFLAGS)" \
                CPPFLAGS="-I$(STAGING_DIR)/usr/include" \
                LDFLAGS="-L$(STAGING_DIR)/usr/lib" \
                ./configure \
                --target=$(GNU_TARGET_NAME) \
                --host=$(GNU_TARGET_NAME) \
                --build=$(GNU_HOST_NAME) \
                --program-prefix="" \
                --program-suffix="" \
                --prefix=/usr \
                --exec-prefix=/usr \
                --bindir=/usr/bin \
                --datadir=/usr/share \
                --includedir=/usr/include \
                --infodir=/usr/share/info \
                --libdir=/usr/lib \
                --libexecdir=/usr/lib \
                --localstatedir=/var \
                --mandir=/usr/share/man \
                --sbindir=/usr/sbin \
                --sysconfdir=/etc \
                $(DISABLE_NLS) \
                $(DISABLE_LARGEFILE) \
                --enable-shared \
                --enable-static \
	);
endef

define Build/Compile
	$(call Build/Compile/Default, KERNEL_SOURCE="$(LINUX_DIR)")
	
	$(MAKE) -C $(PKG_BUILD_DIR)/lib \
		CCOPT="$(TARGET_CFLAGS)" \
		DESTDIR="$(PKG_INSTALL_DIR)" all install
endef

define Build/InstallDev
	install -d -m0755 $(STAGING_DIR)/usr/lib
	install -d -m0755 $(STAGING_DIR)/usr/include
	$(CP) $(PKG_BUILD_DIR)/qos_fops.h $(STAGING_DIR)/usr/include/
	$(CP) $(PKG_BUILD_DIR)/lib/include/libqos.h $(STAGING_DIR)/usr/include/
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/* $(STAGING_DIR)/usr/lib/
endef

define Build/UninstallDev
	$(RM) $(STAGING_DIR)/usr/include/qos_fops.h
	$(RM) $(STAGING_DIR)/usr/include/libqos.h
	$(RM) $(STAGING_DIR)/usr/lib/libqos.*
endef

define KernelPackage/qoscom/install
	install -d -m0755 $(1)/usr/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/libqos.so* $(1)/usr/lib/
	install -d -m0755 $(1)/etc/init.d
	install -m0755  ./files/qoscom.init $(1)/etc/init.d/qoscom
endef

$(eval $(call KernelPackage,qoscom))
