###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2018  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

PKG_NAME   = collectd
VER        = 4.10.9

THISAPP    = collectd-$(VER)
DL_FILE    = $(THISAPP).tar.bz2
DL_FROM    = $(URL_IPFIRE)
DIR_APP    = $(DIR_SRC)/$(THISAPP)
TARGET     = $(DIR_INFO)/$(THISAPP)
PROG       = collectd

DEPS       = ""

CFLAGS    += -Wno-error=deprecated-declarations

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

objects = $(DL_FILE)

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

$(DL_FILE)_MD5 = 980dd3387508f9ad209df04a6f7a126c

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 jxf $(DIR_DL)/$(DL_FILE)
	$(UPDATE_AUTOMAKE)
	cd $(DIR_APP) && find . -name "Makefile.*" | xargs sed -e "s/-Werror//g" -i
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0001-src-utils_mount.h-Add-stdio.h.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0002-Don-t-notify-continuously-when-MySQL-slave-SQL-threa.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0003-curl_xml.c-avoid-using-uninitalized-variable-in-erro.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0004-interface.c-FreeBSD-10-support.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0005-Revert-curl_xml.c-avoid-using-uninitalized-variable-.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0006-network-set_thread_cbs-so-we-initialize-the-right-th.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0007-apache-plugin-Call-curl_global_init-from-the-init-fu.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0008-network-comment-libgcrypt-initalization-process.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0009-Call-curl_global_init-in-_init-of-plugins-using-curl.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0010-indent-wh_init-to-be-consistent-with-the-rest-of-the.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0011-Configparser-when-we-alocate-an-empty-list-we-also-n.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0012-don-t-assume-pkg-config-is-in-PATH.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0013-add-missing-backticks-which-broke-the-build.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0014-snmp-free-snmp_pdu-struct-allocated-by-snmp_pdu_crea.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0015-curl_xml-plugin-Fixed-tautological-pointer-compariso.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0016-Add-support-for-OpenVPN-2.3.0-status-files.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0017-openvpn-plugin-Don-t-signal-an-error-when-no-clients.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0018-openvpn-Remove-boguous-file-handler-check.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0019-openvpn-Ignore-not-fully-established-connections.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0020-openvpn-Make-read-functions-robust-like-in-8516f9abb.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0021-openvpn-Fix-copy-and-paste-error.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/0022-openvpn-Change-data-type-from-COUNTER-to-DERIVE.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd/silence-openvpn-errors.patch
	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd-4.10.9-xfs-compile-fix.patch
	cd $(DIR_APP) && autoreconf -vfi
	cd $(DIR_APP)/libltdl && autoreconf -vfi
	cd $(DIR_APP) && ./configure --prefix=/usr --localstatedir=/var \
		--disable-{apple_sensors,csv,ipvs,mbmon,memcached,mysql} \
		--disable-{netlink,nginx,nut,perl,serial,snmp,tape,vserver,xmms} \
		--disable-{perl,serial,snmp,tape,vserver,xmms} \
		--enable-{apcups,battery,cpu{,freq},df,disk,dns,email,entropy,exec,hddtemp} \
		--enable-{interface,iptables,irq,load,logfile,memory,multimeter} \
		--enable-{network,nfs,ntpd,ping,processes,rrdtool,sensors,swap,syslog} \
		--enable-{tcpconns,unixsock,users,wireless} \
		--with-librrd=/usr/share/rrdtool-1.2.30
	cd $(DIR_APP) && make install #collectd-4 does not support parallel build
	cp -vf $(DIR_SRC)/config/collectd/collectd.* /etc/
	mv /etc/collectd.vpn /var/ipfire/ovpn/collectd.vpn
	chown nobody.nobody /var/ipfire/ovpn/collectd.vpn
	ln -f -s ../var/ipfire/ovpn/collectd.vpn /etc/collectd.vpn
	ln -f -s ../init.d/collectd /etc/rc.d/rc0.d/K50collectd 
	ln -f -s ../init.d/collectd /etc/rc.d/rc3.d/S29collectd
	ln -f -s ../init.d/collectd /etc/rc.d/rc6.d/K50collectd 
	@rm -rf $(DIR_APP)
	@$(POSTBUILD)
