###############################################################################
# This file is part of the IPCop Firewall.                                    #
#                                                                             #
# IPCop 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 2 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# IPCop 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 IPCop; if not, write to the Free Software                        #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA    #
#                                                                             #
# Makefiles are based on LFSMake, which is                                    #
# Copyright (C) 2002 Rod Roard <rod@sunsetsystems.com>                        #
#                                                                             #
# Modifications by:                                                           #
# ??-12-2003 Mark Wormgoor < mark@wormgoor.com>                               #
#          - Modified Makefile for IPCop build                                #
#                                                                             #
# $Id: capi4k-utils 7639 2014-08-25 13:04:21Z owes $
#                                                                             #
###############################################################################

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

include Config

PKG_NAME   = capi4k-utils
VER        = 2005-07-18
HOST_ARCH  = all
OTHER_SRC  = yes

THISAPP    = $(PKG_NAME)-$(VER)
DL_FILE    = $(THISAPP).tar.gz
DL_FROM    = ftp://ftp.in-berlin.de/pub/capi4linux
DIR_APP    = $(DIR_SRC)/capi4k-utils
TARGET     = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(THISAPP)

PPPVERSIONS:=$(shell grep --max-count=1 VER ppp | cut -d '=' -f2 | cut -d ' ' -f2)

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

objects = $(DL_FILE)

$(DL_FILE) = $(DL_FROM)/$(DL_FILE)

$(DL_FILE)_MD5 = c745759b6b3d64e19763727176648cdf

install : $(TARGET)

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

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

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

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

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

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

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

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

$(TARGET) : $(firstword $(MAKEFILE_LIST)) $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
	@rm -rf $(DIR_APP) && cd $(DIR_SRC) && tar zxf $(DIR_DL)/$(DL_FILE)

	cd $(DIR_APP) && patch -Np0 -i $(DIR_PATCHES)/capi4k-utils-20050718-msg2str-safety.diff
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/capi4k-utils-20050718-size_t.patch
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_ppp-2.4.7.patch
	# Fix a typo (?) in capiinit configure and set default prefix to /usr
	cd $(DIR_APP)/capiinit && sed -i -e 's/^CONFIG_SBINDIR.*=/CONFIG_SBINDIR=/' \
					-e 's/^ac_default_prefix=.*/ac_default_prefix=\/usr/' configure
	# Fix malloc and free implicit declaration
	cd $(DIR_APP)/capi20 && sed -i -e 's:include <sys/time.h>:include <sys/time.h>\n#include <sys/types.h>\n#include <stdlib.h>:' capi20.h
	# Fix undefined reference to `__stack_chk_fail_local'
	cd $(DIR_APP)/pppdcapiplugin && sed -i -e 's/ -nostdlib//' Rules.make

	# A few fixes borrowed from Gentoo
	# Do we need that? : patch capiinit/capiinit.c to look also in /lib/firmware
	# cd $(DIR_APP) && sed -i -e "s:\(\"/lib/firmware/isdn\",\):\1 \"/lib/firmware\",:g" capiinit/capiinit.c
	# Patch all Makefile.* and Rules.make to use our CFLAGS
	cd $(DIR_APP) && sed -i -e "s:^\(CFLAGS.*\)-O2:\1$(CFLAGS):g" */Makefile.* */Rules.make
	# respect LDFLAGS : add + before =
	cd $(DIR_APP) && sed -i -e "s:^LDFLAGS\s\(\s*\)=:LDFLAGS+\1=:g" {capiinfo,capiinit,capifax,rcapid,avmb1}/Makefile* pppdcapiplugin/Rules.make

	cd $(DIR_APP)/avmb1 && ./configure && make install

	cd $(DIR_APP)/capiinit && ./configure && make install

	# --disable-static remove libcapi20.a, not libcapi20dyn.a needed for plugins
	cd $(DIR_APP)/capi20 && ./configure --disable-static && make install

	cd $(DIR_APP)/pppdcapiplugin && make install PPPVERSIONS=$(PPPVERSIONS)
	cd $(DIR_APP)/capiinfo && ./configure && make install

	@rm -rf $(DIR_APP)
	@$(POSTBUILD)
