#
# 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

PKG_NAME:=asterisk
PKG_VERSION:=1.6.2.9
PKG_RELEASE:=1


PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=http://downloads.digium.com/pub/asterisk/releases/
PKG_MD5SUM:==1f947d951c419b8039d53a6e6168fd69
PKG_CAT:=zcat

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
PKG_INSTALL_DIR:=$(PKG_BUILD_DIR)/ipkg-install

#PKG_BUILDDEP:=libncurses libpthread kmod-matisse
PKG_BUILDDEP:=libopenssl libncurses libpopt libpthread zlib

include $(INCLUDE_DIR)/package.mk

############### Default Config file##############
ifeq ($(CONFIG_LINUX_2_6_COMCERTO100_ferouter),y)
	CONFIG_FILE:=./files/asterisk.uci.fer
endif

ifeq ($(CONFIG_LINUX_2_6_COMCERTO100_packet-iad),y)
	CONFIG_FILE:=./files/asterisk.uci.iad
endif

ifeq ($(CONFIG_LINUX_2_6_COMCERTO100_router),y)
	CONFIG_FILE:=./files/asterisk.uci.iad
endif

ifeq ($(CONFIG_LINUX_2_6_COMCERTO100_asic),y)
	CONFIG_FILE:=./files/asterisk.uci.iad
endif

ifeq ($(CONFIG_LINUX_2_6_COMCERTO50_packet-iad),y)
	CONFIG_FILE:=./files/asterisk.uci.iad
endif

ifeq ($(CONFIG_LINUX_2_6_COMCERTO50_router),y)
	CONFIG_FILE:=./files/asterisk.uci.iad
endif

ifeq ($(CONFIG_LINUX_2_6_COMCERTO1000),y)
	TARGET_NODE:=m83xxx0
	CONFIG_FILE:=./files/asterisk.uci.iad
else
	TARGET_NODE:=m821xx0
endif

define Package/asterisk16/Default
  SECTION:=net
  CATEGORY:=IP-PBX
  DESCRIPTION:=\
	Asterisk is a complete PBX in software. It provides all of the features \\\
	you would expect from a PBX and more. Asterisk does voice over IP in three \\\
	protocols, and can interoperate with almost all standards-based telephony \\\
	equipment using relatively inexpensive hardware.
  URL:=http://www.asterisk.org/
  MAINTAINER:=Dennis Cheng <dennis_cheng@draytek.com>
endef


define Package/asterisk16
  $(call Package/asterisk16/Default)
  #DEPENDS:=+libncurses +libpthread +kmod-matisse
  DEPENDS:=+libopenssl +libncurses +libpopt +libpthread +zlib @!PACKAGE_asterisk
  TITLE:=Complete open source PBX
endef

define Package/asterisk16-voicemail
  $(call Package/asterisk16/Default)
  DEPENDS:=+asterisk16
  TITLE:=Voicemail support
  DESCRIPTION+=\\\
\\\
	This package contains voicemail related modules for Asterisk.
endef

define Package/asterisk16-voicemail/conffiles
/etc/asterisk/voicemail.conf
endef

define Package/asterisk16-chan-iax2
$(call Package/asterisk16/Default)
  TITLE:=IAX support
  DEPENDS:= +asterisk16
  DESCRIPTION+=\\\
\\\
 This package provides IAX support to Asterisk.
endef

define Package/asterisk16-chan-iax2/conffiles
/etc/asterisk/iax.conf
/etc/asterisk/iaxprov.conf
endef

define Package/asterisk16-chan-mspd
$(call Package/asterisk16/Default)
  TITLE:=MSPD channel support
  DEPENDS:= +asterisk16
  DESCRIPTION+=\\\
\\\
 This package provides MSPD channel support to Asterisk.
endef

define Package/asterisk16-chan-mspd/conffiles
/etc/asterisk/mspd.conf
endef

#define Package/asterisk16-chan-local
#$(call Package/asterisk16/Default)
#  TITLE:=chan_local support
#  DEPENDS:= +asterisk16
#  DESCRIPTION+=\\\
#\\\
# This package provides chan_local support to Asterisk.
#endef


define Package/asterisk16-cdr
$(call Package/asterisk16/Default)
  TITLE:=CDR support
  DEPENDS:= +asterisk16
  DESCRIPTION+=\\\
\\\
 This package provides Call Detail Record support to Asterisk.  
endef


define Package/asterisk16-cdr/conffiles
/etc/asterisk/cdr.conf
/etc/asterisk/cdr_custom.conf
/etc/asterisk/cdr_manager.conf
/etc/asterisk/cdr_odbc.conf
/etc/asterisk/cdr_pgsql.conf
/etc/asterisk/cdr_tds.conf
endef


define Package/asterisk16-res-musiconhold
$(call Package/asterisk16/Default)
  TITLE:=MOH support
  DEPENDS:= +asterisk16
  DESCRIPTION+=\\\
\\\
 This package provides Music On Hold support to Asterisk. 
endef

define Package/asterisk16-res-musiconhold/conffiles
/etc/asterisk/musiconhold.conf
endef

define Package/asterisk16-sounds
$(call Package/asterisk16/Default)
  TITLE:=Sound files
  DEPENDS:= +asterisk16
  DESCRIPTION+=\\\
\\\
 This package contains sound files for Asterisk.
endef

define Package/asterisk16-chan-gtalk
$(call Package/asterisk16/Default)
  TITLE:=GTalk support
  DEPENDS:= +asterisk16 +libiksemel
endef

define Package/asterisk16-chan-gtalk/description
$(call Package/asterisk16/Default/description)
 This package provides the channel chan_gtalk and res_jabber for GTalk
 support to Asterisk.
endef

#define Package/asterisk16-app-system
#$(call Package/asterisk16/Default)
#  TITLE:=System exec support
#  DEPENDS:= +asterisk16
#  DESCRIPTION+=\\\
#\\\
# This package provides support for executing system commands in Asterisk.  
#endef


EXTRA_CFLAGS:= -I$(STAGING_DIR)/usr/include -I$(STAGING_DIR)/include -DDALI
EXTRA_LDFLAGS:= -L$(STAGING_DIR)/usr/lib -L$(STAGING_DIR)/lib



ifeq ($(CONFIG_DEV_ASTERISK), y)

define Build/Prepare
	pushd "$(BUILD_DIR)" && \
	cvs -z9 -d:pserver:$(CONFIG_ACTIVEDEV_CVSUSERNAME)@$(CONFIG_ACTIVEDEV_CVSROOTPATH):/prj/msacvs/enterprise checkout -d $(PKG_NAME)-$(PKG_VERSION) -r $(CONFIG_DEV_ASTERISK_CVSBRANCH) $(CONFIG_DEV_ASTERISK_MODULENAME) && \
	popd
endef

endif

define Build/Configure
	-rm $(PKG_BUILD_DIR)/menuselect.makeopts
	$(call Build/Configure/Default,--disable-xmldoc,$(SITE_VARS))
endef



define Build/Compile
	$(MAKE) -C "$(PKG_BUILD_DIR)" \
		include/asterisk/version.h \
		include/asterisk/buildopts.h defaults.h \
		makeopts.embed_rules
	ASTCFLAGS="$(EXTRA_CFLAGS) -DLOW_MEMORY $(TARGET_CFLAGS)" \
	ASTLDFLAGS="$(EXTRA_LDFLAGS)" \
	$(MAKE) -C "$(PKG_BUILD_DIR)" \
		CROSS_ARCH="Linux" \
		CROSS_COMPILE="$(TARGET_CROSS)" \
		PROC="$(ARCH)" \
		ASTVARLIBDIR="/usr/lib/asterisk" \
		NOISY_BUILD="1" \
		DEBUG="" \
		OPTIMIZE="" \
		DESTDIR="$(PKG_INSTALL_DIR)" \
		BUILD_FROM_OPENWRT="y" \
		all install samples
	$(SED) 's|/var/lib/asterisk|/usr/lib/asterisk|g' $(PKG_INSTALL_DIR)/etc/asterisk/musiconhold.conf
	rm -f $(PKG_INSTALL_DIR)/etc/asterisk/*.old
endef


define Package/asterisk16/install
	install -d -m0755 $(1)/etc/asterisk
	rm -rf $(1)/usr/sbin/astgenkey
	(cd $(1)/etc/asterisk; \
		rm -f 	*odbc* *mysql* *postgres* *pgsql* *voicemail* *adsi* *oss* *alsa* \
			*festival* *modem* *meetme* *phone* *tds* *vofr* *rpt* *vpb* \
			*zapata*; \
		rm -f bluetooth.conf ; \
		rm -f mgcp.conf ; \
		rm -f skinny.conf ; \
		rm -f dundi.conf ; \
		rm -f sip.conf ; \
		rm -f extensions.conf ; \
		rm -f mspd.conf ; \
	)	
	for f in asterisk extensions features \
		logger manager modules \
		sip sip_notify rtp; do \
		$(CP) $(PKG_INSTALL_DIR)/etc/asterisk/$$$$f.conf $(1)/etc/asterisk/ ; \
	done
	install -d -m0755 $(1)/usr/lib/asterisk/modules
	for f in app_dial app_echo app_playback app_macro \
		chan_sip \
		codec_ulaw codec_gsm \
		format_gsm format_pcm format_wav format_wav_gsm \
		pbx_config \
		func_strings func_timeout func_callerid func_logic; do \
		$(CP) $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/$$$$f.so $(1)/usr/lib/asterisk/modules/ ; \
	done
	install -d -m0755 $(1)/usr/sbin
	install -m0755 $(PKG_INSTALL_DIR)/usr/sbin/asterisk $(1)/usr/sbin/
	install -d -m0755 $(1)/etc/default
	install -m0644  ./files/asterisk.default $(1)/etc/default/asterisk
	install -d -m0755 $(1)/etc/init.d
	install -m0755  ./files/asterisk.init $(1)/etc/init.d/asterisk
	install -m0644 ./files/asterisk.conf $(1)/etc/asterisk/
	install -m0644 ./files/indications.conf $(1)/etc/asterisk/
	install -m0644 ./files/features.conf $(1)/etc/asterisk/
	install -m0644 ./files/modules.conf $(1)/etc/asterisk/
	install -m0644 ./files/musiconhold.conf $(1)/etc/asterisk/
	install -m0644 ./files/rtp.conf $(1)/etc/asterisk/
	install -m0644 ./files/voicemail.conf $(1)/etc/asterisk/	
	install -d -m0755 $(1)/etc/config
	install -m0755  $(CONFIG_FILE) $(1)/etc/config/asterisk
	install -d -m0755 $(1)/lib/asterisk
	# change device node according to the target
	install -m0755  ./files/configure_asterisk $(1)/lib/asterisk/configure_asterisk
	sed -i 's/csmfile=\/dev[^][^]*/csmfile=\/dev\/'$(TARGET_NODE)'/' $(1)/lib/asterisk/configure_asterisk
	# change device node according to the target
	install -m0755  ./files/configure_asterisk_pri $(1)/lib/asterisk/configure_asterisk_pri
	sed -i 's/csmfile=\/dev[^][^]*/csmfile=\/dev\/'$(TARGET_NODE)'/' $(1)/lib/asterisk/configure_asterisk_pri
	install -m0755  ./files/switch_to_pots_mode $(1)/lib/asterisk/switch_to_pots_mode
	install -m0755  ./files/switch_to_t1_mode $(1)/lib/asterisk/switch_to_t1_mode
	install -m0755  ./files/update_info $(1)/lib/asterisk/update_info
	
	#for MSPD driver
#	install -d -m0755 $(1)/etc/config
#	install -d -m0755 $(1)/lib/asterisk	

	# for asterisk queues
	install -m0755 $(PKG_INSTALL_DIR)/etc/asterisk/agents.conf $(1)/etc/asterisk/
	#chan_agent.so is already present
	install -m0755 $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/app_queue.so $(1)/usr/lib/asterisk/modules/
	install -m0755 $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/app_mixmonitor.so $(1)/usr/lib/asterisk/modules/
endef


define Package/asterisk16-voicemail/install
	install -d -m0755 $(1)/etc/asterisk
	install -m0644 $(PKG_INSTALL_DIR)/etc/asterisk/voicemail.conf $(1)/etc/asterisk/
	install -d -m0755 $(1)/usr/lib/asterisk/modules
	install -m0755 $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/*voicemail.so $(1)/usr/lib/asterisk/modules/
	install -m0755 $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/res_adsi.so $(1)/usr/lib/asterisk/modules/
#	install -d -m0755 $(1)/usr/lib/asterisk/sounds/
#	$(CP) $(PKG_INSTALL_DIR)/usr/lib/asterisk/sounds/en/vm-*.gsm $(1)/usr/lib/asterisk/sounds/
endef

define Package/asterisk16-chan-iax2/install
	install -d -m0755 $(1)/etc/asterisk
	install -m0644 $(PKG_INSTALL_DIR)/etc/asterisk/iax.conf $(1)/etc/asterisk/
	install -m0644 $(PKG_INSTALL_DIR)/etc/asterisk/iaxprov.conf $(1)/etc/asterisk/
	install -d -m0755 $(1)/usr/lib/asterisk/modules
	install -m0755 $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/chan_iax2.so $(1)/usr/lib/asterisk/modules/
endef

define Package/asterisk16-chan-mspd/install
	install -d -m0755 $(1)/etc/asterisk
	install -m0644 $(PKG_INSTALL_DIR)/etc/asterisk/mspd.conf $(1)/etc/asterisk/
	install -d -m0755 $(1)/usr/lib/asterisk/modules
	install -m0755 $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/chan_mspd.so $(1)/usr/lib/asterisk/modules/
endef

#define Package/asterisk16-chan-local/install
#	install -d -m0755 $(1)/usr/lib/asterisk/modules
#	install -m0755 $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/chan_local.so $(1)/usr/lib/asterisk/modules/
#endef

define Package/asterisk16-cdr/install
	install -d -m0755 $(1)/etc/asterisk
	install -m0644 $(PKG_INSTALL_DIR)/etc/asterisk/cdr*.conf $(1)/etc/asterisk/
	install -d -m0755 $(1)/usr/lib/asterisk/modules
	install -m0755 $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/*cdr*.so $(1)/usr/lib/asterisk/modules/
endef

define Package/asterisk16-res-musiconhold/install
	install -d -m0755 $(1)/etc/asterisk
	install -m0644 $(PKG_INSTALL_DIR)/etc/asterisk/musiconhold.conf $(1)/etc/asterisk/
	install -d -m0755 $(1)/usr/lib/asterisk/modules
	install -m0755 $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/res_musiconhold.so $(1)/usr/lib/asterisk/modules/
endef

define Package/asterisk16-sounds/install
	install -d -m0755 $(1)/usr/lib/asterisk/sounds/
	install -d -m0755 $(1)/usr/lib/asterisk/moh/
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/asterisk/sounds/en/* $(1)/usr/lib/asterisk/sounds/
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/asterisk/moh/manolo_camp-morning_coffee.wav $(1)/usr/lib/asterisk/moh/
#	rm -f $(1)/usr/lib/asterisk/sounds/vm-*
#	rm -f $(1)/usr/lib/asterisk/sounds/conf-*
endef

define Package/asterisk16-chan-gtalk/conffiles
/etc/asterisk/gtalk.conf
/etc/asterisk/jabber.conf
endef

define Package/asterisk16-chan-gtalk/install
	$(INSTALL_DIR) $(1)/etc/asterisk
	$(INSTALL_DATA) $(PKG_INSTALL_DIR)/etc/asterisk/gtalk.conf $(1)/etc/asterisk/
	$(INSTALL_DATA) $(PKG_INSTALL_DIR)/etc/asterisk/jabber.conf $(1)/etc/asterisk/
	$(INSTALL_DIR) $(1)/usr/lib/asterisk/modules
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/chan_gtalk.so $(1)/usr/lib/asterisk/modules/
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/res_jabber.so $(1)/usr/lib/asterisk/modules/
endef

#define Package/asterisk16-app-system/install
#	$(INSTALL_DIR) $(1)/usr/lib/asterisk/modules
#	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/app_system.so $(1)/usr/lib/asterisk/modules/
#endef

define BuildAsterisk16ModuleTemplate

  define Package/asterisk16-$(subst _,-,$(1))
  $$(call Package/asterisk16/Default)
    TITLE:=$(2) support
    DEPENDS:= +asterisk16
    DESCRIPTION:=This package provides support $(3) in Asterisk.
  endef

#  define Package/asterisk16-$(subst _,-,$(1))/description
#  $$(call Package/asterisk16/Default/description)
#    This package provides support $(3) in Asterisk.
#  endef

  define Package/asterisk16-$(subst _,-,$(1))/install
	$(INSTALL_DIR) $$(1)/usr/lib/asterisk/modules
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/lib/asterisk/modules/$(1).so $$(1)/usr/lib/asterisk/modules/
  endef

  $$(eval $$(call BuildPackage,asterisk16-$(subst _,-,$(1))))
endef

$(eval $(call BuildPackage,asterisk16))
$(eval $(call BuildPackage,asterisk16-voicemail))
$(eval $(call BuildPackage,asterisk16-sounds))
#$(eval $(call BuildPackage,asterisk16-app-meetme))
$(eval $(call BuildPackage,asterisk16-chan-iax2))
$(eval $(call BuildPackage,asterisk16-chan-mspd))
#$(eval $(call BuildPackage,asterisk16-chan-local))
$(eval $(call BuildPackage,asterisk16-cdr))
$(eval $(call BuildPackage,asterisk16-res-musiconhold))
#$(eval $(call BuildPackage,asterisk16-format-g729))
#$(eval $(call BuildPackage,asterisk16-chan-gtalk))
#$(eval $(call BuildPackage,asterisk16-app-system))
$(eval $(call BuildAsterisk16ModuleTemplate,app_authenticate,Authenticate,support for executing arbitrary authenticate commands))
$(eval $(call BuildAsterisk16ModuleTemplate,app_chanisavail,Channel availability check,support for checking if a channel is available))
$(eval $(call BuildAsterisk16ModuleTemplate,app_chanspy,Channel listen in,support for listening in on any channel))
$(eval $(call BuildAsterisk16ModuleTemplate,app_directed_pickup,Directed call pickup,support for directed call pickup))
$(eval $(call BuildAsterisk16ModuleTemplate,app_exec,Exec application,support for application execution))
$(eval $(call BuildAsterisk16ModuleTemplate,app_minivm,Minimal voicemail system,a voicemail system in small building blocks working together based on the Comedian Mail voicemail system))
$(eval $(call BuildAsterisk16ModuleTemplate,app_readexten,Extension to variable,a trivial application to read an extension into a variable))
$(eval $(call BuildAsterisk16ModuleTemplate,app_read,Variable read,a trivial application to read a variable))
$(eval $(call BuildAsterisk16ModuleTemplate,app_sayunixtime,Say Unix time,an application to say Unix time))
$(eval $(call BuildAsterisk16ModuleTemplate,app_sms,SMS,SMS support (ETSI ES 201 912 protocol 1)))
$(eval $(call BuildAsterisk16ModuleTemplate,app_stack,Stack applications, stack applications Gosub Return etc.))
$(eval $(call BuildAsterisk16ModuleTemplate,app_system,System exec,support for executing system commands))
$(eval $(call BuildAsterisk16ModuleTemplate,app_talkdetect,File playback with audio detect,for file playback with audio detect))
$(eval $(call BuildAsterisk16ModuleTemplate,app_waituntil,Sleep,support sleeping until the given epoch))
$(eval $(call BuildAsterisk16ModuleTemplate,app_while,While loop,a while loop implementation))
$(eval $(call BuildAsterisk16ModuleTemplate,chan_agent,Agents proxy channel, an implementation of agents proxy channel))
$(eval $(call BuildAsterisk16ModuleTemplate,chan_local,Local proxy channel, an implementation of local proxy channel))
$(eval $(call BuildAsterisk16ModuleTemplate,codec_ulaw,Signed linear to ulaw translation,translation between signed linear and ulaw codecs))
$(eval $(call BuildAsterisk16ModuleTemplate,codec_a_mu,Alaw to ulaw translation,translation between alaw and ulaw codecs))
#$(eval $(call BuildAsterisk16ModuleTemplate,codec_g726,Signed linear to G.726 translation,translation between signed linear and ITU G.726-32kbps codecs))
#$(eval $(call BuildAsterisk16ModuleTemplate,format_g726,G.726,support for headerless G.726 16/24/32/40kbps data format))
#$(eval $(call BuildAsterisk16ModuleTemplate,format_g729,G.729,support for raw headerless G729 data))
$(eval $(call BuildAsterisk16ModuleTemplate,format_sln,Raw slinear format,support for raw slinear format))
$(eval $(call BuildAsterisk16ModuleTemplate,format_sln16,Raw slinear 16 format,support for Raw slinear 16 format))
$(eval $(call BuildAsterisk16ModuleTemplate,func_db,Database interaction,functions for interaction with the database))
$(eval $(call BuildAsterisk16ModuleTemplate,func_devstate,Blinky lights control,functions for manually controlled blinky lights))
$(eval $(call BuildAsterisk16ModuleTemplate,func_vmcount,vmcount dialplan,a vmcount dialplan function))
$(eval $(call BuildAsterisk16ModuleTemplate,func_extstate,Hinted extension state,retrieving the state of a hinted extension for dialplan control))
$(eval $(call BuildAsterisk16ModuleTemplate,func_global,Global variable,global variable dialplan functions))
$(eval $(call BuildAsterisk16ModuleTemplate,func_shell,Shell,support for shell execution))
$(eval $(call BuildAsterisk16ModuleTemplate,pbx_ael,Asterisk Extension Logic,support for symbolic Asterisk Extension Logic))
$(eval $(call BuildAsterisk16ModuleTemplate,res_ael_share,Shareable AEL code,support for shareable AEL code mainly between internal and external modules))
$(eval $(call BuildAsterisk16ModuleTemplate,res_agi,Asterisk Gateway Interface,support for the Asterisk Gateway Interface extension))
$(eval $(call BuildAsterisk16ModuleTemplate,app_db,Support DBdel() application))
$(eval $(call BuildAsterisk16ModuleTemplate,app_playtones,Support PlayTones() application))