###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2011  IPFire Team <info@ipfire.org>                      #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

###############################################################################
# Definitions
###############################################################################

include Config

VERSUFIX=ipfire$(KCFG)

VER        = 2.2.1
TVER       = 2.2.1

DL_FILE    = dahdi-linux-$(VER).tar.gz
DL_FILE1   = dahdi-tools-$(TVER).tar.gz

THISAPP    = dahdi-$(VER)
DL_FROM    = $(URL_IPFIRE)
DIR_APP    = $(DIR_SRC)/dahdi-linux-$(VER)
DIR_APP1   = $(DIR_SRC)/dahdi-tools-$(TVER)

PROG       = dahdi
PAK_VER    = 2

DEPS       = ""


ifeq "$(KMOD)" "1"
	TARGET = $(DIR_INFO)/$(THISAPP)-kmod-$(KVER)-$(VERSUFIX)
else
	TARGET = $(DIR_INFO)/$(THISAPP)
endif


###############################################################################
# Top-level Rules
###############################################################################

objects = $(DL_FILE) $(DL_FILE1) dahdi-linux-2.2.1-firmware.tar.bz2

$(DL_FILE) = $(DL_FROM)/$(DL_FILE)
$(DL_FILE1) = $(DL_FROM)/$(DL_FILE1)
dahdi-linux-2.2.1-firmware.tar.bz2 = $(DL_FROM)/dahdi-linux-2.2.1-firmware.tar.bz2

$(DL_FILE)_MD5 = 46e4abbe743830045364fc716d293e88
$(DL_FILE1)_MD5 = 04e7097929851f744f777400125180db
dahdi-linux-2.2.1-firmware.tar.bz2_MD5 = ccc50ded1641cd619e8d3a4615a6ad19


install : $(TARGET)

check : $(patsubst %,$(DIR_CHK)/%,$(objects))

download :$(patsubst %,$(DIR_DL)/%,$(objects))

md5 : $(subst %,%_MD5,$(objects))

dist: 
	$(PAK)

###############################################################################
# Downloading, checking, md5sum
###############################################################################

$(patsubst %,$(DIR_CHK)/%,$(objects)) :
	@$(CHECK)

$(patsubst %,$(DIR_DL)/%,$(objects)) :
	@$(LOAD)

$(subst %,%_MD5,$(objects)) :
	@$(MD5)

###############################################################################
# Installation Details
###############################################################################

$(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)

	rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar zxf $(DIR_DL)/$(DL_FILE)
	cd $(DIR_SRC) && tar jxf $(DIR_DL)/dahdi-linux-2.2.1-firmware.tar.bz2
	cd $(DIR_APP) && make $(MAKETUNING) clean all KVERS=$(KVER)

ifeq "$(KMOD)" "1"
	cd $(DIR_APP) && make install-modules KVERS=$(KVER)-$(VERSUFIX)
	@rm -rf $(DIR_APP)
else
	cd $(DIR_APP) && make install-devices install-include \
		install-firmware install-xpp-firm KVERS=$(KVER)-$(VERSUFIX)
	@rm -rf $(DIR_APP)

	# remove previous generated symlink (if this a rebuild)
	rm -rf /etc/dahdi

	rm -rf $(DIR_APP1) && cd $(DIR_SRC) && tar zxf $(DIR_DL)/$(DL_FILE1)
	cd $(DIR_APP1) && ./configure --sysconfdir=/var/ipfire/asterisk
	cd $(DIR_APP1) && make $(MAKETUNING) clean all
	cd $(DIR_APP1) && make install

	# Supress Error output because this erase the log 
	cd $(DIR_APP1) && make config 2>/dev/null

	# remove build src
	@rm -rf $(DIR_APP1)

	# config stuff
	ln -sf ../init.d/dahdi /etc/rc.d/rc0.d/K30dahdi
	ln -sf ../init.d/dahdi /etc/rc.d/rc6.d/K30dahdi
	ln -sf ../init.d/dahdi /etc/rc.d/rc3.d/S15dahdi
	mv -f /etc/dahdi/genconf_parameters /var/ipfire/asterisk/dahdi/
	mv -f /etc/dahdi/init.conf /var/ipfire/asterisk/dahdi/
	mv -f /etc/dahdi/modules /var/ipfire/asterisk/dahdi/
	rmdir /etc/dahdi
	ln -s /var/ipfire/asterisk/dahdi /etc/dahdi

	# make init.d-script work with ipfire
	sed s/system=redhat/system=debian/ /etc/init.d/dahdi > /tmp/dahdi-temp
	mv /tmp/dahdi-temp /etc/init.d/dahdi && chmod 755 /etc/init.d/dahdi

	# add dummy-module (to get conference rooms work)
	echo "#Dummy" >> /etc/dahdi/modules
	echo "dahdi_dummy" >> /etc/dahdi/modules
endif
	@$(POSTBUILD)

