# 
# Copyright (C) 2010-2014 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
# User space library and programs from mISDN V2.
#
# Here we provide these packages:
#   libmisdn	  - Helper library for using mISDN
#   misdn-capid	  - Daemon providing CAPI access to mISDN interfaces
#   misdn-tools   - Tools for mISDN debugging and interface name changing
#   misdn-bridge  - mISDN-layer-1 bridge control utility
#   misdnl1oipctr - mISDN-layer-1 over IP tunnel control utility
#
# Note that libmisdn is not used by other packages built by this file.
# libmisdn is provided to external packages like chan_lcr.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=misdnuser
PKG_VERSION:=20140921
PKG_RELEASE:=1

PKG_SOURCE_URL:=git://git.misdn.eu/mISDNuser.git
#PKG_SOURCE_VERSION=HEAD
PKG_SOURCE_VERSION:=ed6c40581e344e6baa948c9aee1c33cb33682f5b
PKG_SOURCE:=$(PKG_NAME)-$(PKG_SOURCE_VERSION).tar.bz2
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)

#PKG_LICENSE:=LGPLv2
#PKG_LICENSE_FILES:=COPYING


PKG_FIXUP:=autoreconf
PKG_INSTALL:=1


PKG_BUILD_DEPENDS += PACKAGE_libcapi20:libcapi20


include $(INCLUDE_DIR)/package.mk


# As this does not work well, do not use any dependency declararion from kmod-isdn or kmod-isdn-extern:
#MISDNUSER_EXTERNDEP:=+(PACKAGE_kmod-misdn-extern=n):kmod-misdn +(PACKAGE_kmod-misdn=n):kmod-misdn-extern


#
# Defaults for all packages
#

define Package/misdnuser/Default
  URL:=https://www.misdn.eu/
  MAINTAINER:=Arnold Schulz <theosch@gmx.net>
endef


#
# Package libmisdn provides the libmisdn.so shared library.
# Currently libmisdn is not used by any other package defined in _this_ Makefile.
#

define Package/libmisdn
  $(call Package/misdnuser/Default)
  CATEGORY:=Libraries
  SECTION:=lib
# SUBMENU:=Telephony
  TITLE:=mISDN v2 usermode library
  # Our autoconf/automake/libtool process specifies too many shared libs to use.
  # We try to get rid of extra references via the as-needed hack (see below).
  # But this does not work for shared libs. Neither libpthread.so nor libcapi20.so
  # are really required by libmisdn.so, but we must define dependencies here.
  # Otherwise we would get build errors.
  DEPENDS:=$(MISDNUSER_EXTERNDEP) +libpthread +PACKAGE_misdn-capid:libcapi20
endef

define Package/libmisdn/description
  This package contains the mISDN v2 usermode library, possibly needed by some
  other programs. Usually it will be selected automatically if required.

endef


#
# Package misdn-capid
#

define Package/misdn-capid
  $(call Package/misdnuser/Default)
  CATEGORY:=Network
  SECTION:=net
  SUBMENU:=ISDN
  TITLE:=CAPI daemon for mISDN interfaces
  # Our autoconf/automake/libtool process specifies too many shared libs to use.
  # We try to get rid of extra references via the as-needed hack (see below).
  # But this does not work for shared libs. Though libmisdn.so is not really
  # required by lib_capi_mod_misdn.so, we must define dependencies here.
  # Otherwise we would get build errors.
  DEPENDS:=$(MISDNUSER_EXTERNDEP) +libcapi20 +MISDN-CAPID_SOFTFAX:libspandsp +MISDN-CAPID_SOFTFAX:libtiff +libmisdn
endef


define Package/misdn-capid/description
  A user space daemon (mISDNcapid) which implements CAPI (Common ISDN Application
  Programming Interface) for the mISDN subsystem.

  This way applications written for CAPI can make use of mISDN hardware.

endef


define Package/misdn-capid/config
  config MISDN-CAPID_SOFTFAX
	bool    "Enable software fax processing"
	depends on PACKAGE_misdn-capid
	help
	  This enables the mISDNcapid daemon to process faxes by using libspandsp and libtiff.
endef                                              

                                              
#
# mISDN utility packages
#

# Defaults specific for mISDN utilities
define Package/misdnuser/DefaultUtil
  $(call Package/misdnuser/Default)
  CATEGORY:=Network
  SECTION:=net
  SUBMENU:=ISDN
  DEPENDS:=$(MISDNUSER_EXTERNDEP) +libpthread
endef



define Package/misdn-tools
  $(call Package/misdnuser/DefaultUtil)
  TITLE:=Tools for mISDN debugging and interface name changing
endef

define Package/misdn-tools/description
  This package contains some tools used mainly for debugging:
  misdn_cleanl2       - Wipe clean layer2 connection states
  misdn_log           - Monitor layer 1 primitives and capture D/E channel data
  misdn_rename        - Rename an mISDN device
  misdn_info          - List information of all available mISDN ports
  misdn_E1test        - (Guessed:) Test an mISDN primary rate interface (E1 only?)
  isdn_text2wireshark - Read D-channel trace from hex/text file into binary wireshark format
endef



define Package/misdn-bridge
  $(call Package/misdnuser/DefaultUtil)
  TITLE:=mISDN-layer-1 bridge control utility
endef

define Package/misdn-bridge/description
  This package provides the misdn_bridge user space utility for bridging
  two ISDN ports via layer 1.

endef



define Package/misdn-l1oipctrl
  $(call Package/misdnuser/DefaultUtil)
  TITLE:=ISDN-layer-1 over IP tunnel control utility
endef

define Package/misdn-l1oipctrl/description
  This package provides the l1oipctrl user space utility for managing
  virtual ISDN cards and establishing ISDN-Layer-1 over IP tunnels.

endef


#
#  Specialities for configuring:
#  - Explicitly define AF_ISDN, as it might not be defined in socket.h of current uClib
#  - Go around AC_FIND_FILES in configure.ac which doesn't work when cross compiling
#

CONFIGURE_ARGS += \
	--disable-example \
	--disable-gui \
	--disable-memleak_debug \
	$(if $(CONFIG_PACKAGE_misdn-capid),--enable-capi,--disable-capi) \
	$(if $(CONFIG_MISDN-CAPID_SOFTFAX),--enable-softdsp,--disable-dsp) \
 	--with-AF_ISDN=34


### Hack: most executables built here refer to unused shared libraries (libtool problem?)
### Try to get rid of this.
### Note: this does not help for shared libs needlessly referencing other shared libs.
TARGET_LDFLAGS += -Wl,--as-needed


#
#  Installation of files possibly required by other packages for
#  package building (i.e. not required at runtime).
#

define Build/InstallDev
	$(INSTALL_DIR)  $(1)/usr/include/mISDN  $(1)/usr/lib
	$(CP)  $(PKG_INSTALL_DIR)/usr/include/mISDN	    $(1)/usr/include/
	$(CP)  $(PKG_INSTALL_DIR)/usr/lib/libmisdn.*	    $(1)/usr/lib/
endef


# Assumed not longer neccessary (done automaticaly!?!)
#define Build/UninstallDev
#	rm -rf  $(1)/usr/include/mISDN		\
#		$(1)/usr/lib/libmisdn.*
#endef
        
        
#
#  Installation of runtime files.
#

define Package/libmisdn/install
	$(INSTALL_DIR)  $(1)/usr/lib
	$(CP)  $(PKG_INSTALL_DIR)/usr/lib/libmisdn.so*  $(1)/usr/lib/
endef


define Package/misdn-capid/conffiles
/etc/capi20.conf
/etc/init.d/mISDNcapid
endef

define Package/misdn-capid/install
	$(INSTALL_DIR)	$(1)/usr/sbin
	$(INSTALL_BIN)	$(PKG_INSTALL_DIR)/usr/sbin/mISDNcapid			$(1)/usr/sbin/
	$(INSTALL_DIR)	$(1)/usr/lib/capi
	$(CP)		$(PKG_INSTALL_DIR)/usr/lib/capi/lib_capi_mod_misdn.so* 	$(1)/usr/lib/capi/
	$(INSTALL_DIR)	$(1)/etc
	$(CP)		$(PKG_INSTALL_DIR)/etc/capi20.conf			$(1)/etc/capi20.conf
	$(INSTALL_DIR)	$(1)/etc/init.d
	$(INSTALL_BIN)	./files/mISDNcapid.init					$(1)/etc/init.d/mISDNcapid
endef


define Package/misdn-tools/install
	$(INSTALL_DIR)	$(1)/usr/sbin
	$(INSTALL_BIN)	$(PKG_INSTALL_DIR)/usr/sbin/misdn_cleanl2	$(1)/usr/sbin/
	$(INSTALL_BIN)	$(PKG_INSTALL_DIR)/usr/sbin/misdn_rename	$(1)/usr/sbin/
	$(INSTALL_DIR)	$(1)/usr/bin
	$(INSTALL_BIN)	$(PKG_INSTALL_DIR)/usr/bin/misdn_info		$(1)/usr/bin/
	$(INSTALL_BIN)	$(PKG_INSTALL_DIR)/usr/bin/misdn_log		$(1)/usr/sbin/
	$(INSTALL_BIN)	$(PKG_INSTALL_DIR)/usr/bin/isdn_text2wireshark	$(1)/usr/bin/
	$(INSTALL_BIN)	$(PKG_INSTALL_DIR)/usr/bin/misdn_E1test		$(1)/usr/sbin/
endef


define Package/misdn-bridge/install
	$(INSTALL_DIR)	$(1)/usr/bin
	$(INSTALL_BIN)	$(PKG_INSTALL_DIR)/usr/bin/misdn_bridge  $(1)/usr/bin/
endef


define Package/misdn-l1oipctrl/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/l1oipctrl  $(1)/usr/bin/
endef




$(eval $(call BuildPackage,libmisdn))
$(eval $(call BuildPackage,misdn-capid))
$(eval $(call BuildPackage,misdn-tools))
$(eval $(call BuildPackage,misdn-bridge))
$(eval $(call BuildPackage,misdn-l1oipctrl))

