###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007  Michael Tremer & Christian Schmidt                      #
#                                                                             #
# 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

ifeq "$(XEN)" "1"
	VERSUFIX=ipfire-xen
else
	VERSUFIX=ipfire
endif

VER        = 2.2.0.2
TVER       = 2.2.0

DL_FILE    = dahdi-linux-$(VER)-with-vpmadt032-1.17.0.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    = 1

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.0.2-firmware.tar.bz2

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

$(DL_FILE)_MD5 = 5cbabd47cf780305e2cba6ccec42c754
$(DL_FILE1)_MD5 = a018f452f3851a312ff51705ac44de37
dahdi-linux-2.2.0.2-firmware.tar.bz2_MD5 = 7d7c774d214a528b49c9a08b5350a652
 

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)

ifeq "$(KMOD)" "1"
	rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar zxf $(DIR_DL)/$(DL_FILE)
	cd $(DIR_APP) && make $(MAKETUNING) clean all KVERS=$(KVER)
	cd $(DIR_APP) && make install-modules KVERS=$(KVER)-$(VERSUFIX)
	@rm -rf $(DIR_APP)
else
	rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar zxf $(DIR_DL)/$(DL_FILE)
	cd $(DIR_SRC) && tar jxf $(DIR_DL)/dahdi-linux-2.2.0.2-firmware.tar.bz2
	cd $(DIR_APP) && make $(MAKETUNING) clean all KVERS=$(KVER)
	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)

