###############################################################################
# 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: libgd 7155 2013-11-21 11:56:36Z dotzball $
#                                                                             #
###############################################################################

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

include Config

PKG_NAME   = libgd
VER        = 2.1.0
PATCHLEVEL = 3
HOST_ARCH  = all
OTHER_SRC  = yes

THISAPP    = libgd-$(VER)
DL_FILE    = $(PKG_NAME)2_$(VER).orig.tar.xz
DL_FROM    = $(URL_DEBIAN)/libg/$(PKG_NAME)2
#DL_FROM    = http://www.libgd.org/releases
DIR_APP    = $(DIR_SRC)/$(THISAPP)
TARGET     = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(THISAPP)-$(PATCHLEVEL)

PATCH1     = $(PKG_NAME)2_$(VER)-$(PATCHLEVEL).debian.tar.gz

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

objects = $(DL_FILE) $(PATCH1)

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

$(DL_FILE)_MD5	= 03588159bf4faab9079849c8d709acc6
$(PATCH1)_MD5	= eb6ba21a1b948f16a5461c940eae7faa

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 xf $(DIR_DL)/$(DL_FILE)
	cd $(DIR_APP) && tar xf $(DIR_DL)/$(PATCH1)
	# here patch names starting with 2 are specific to debian (see README) (none actually)
	cd $(DIR_APP) && for patch in `cat debian/patches/series | grep -v '^2'` ; do \
		patch -Np1 -i debian/patches/$$patch; \
	done

	# GD_GIF GD_GIFANIM GD_OPENPOLYGON are not used by squid-graph.
	# May try to disable them if that translate to a size win (actually some code changes are required to win size).
	# cd $(DIR_APP) && sed -i -e 's/GD_GIF GD_GIFANIM GD_OPENPOLYGON//' configure
	# Disable fontconfig in case of libgd rebuild
	cd $(DIR_APP) && ./configure --prefix=/usr --disable-static --without-fontconfig
	cd $(DIR_APP) && make -j $(PARALLELISM)

ifeq "$(RUNNING_TEST)" "yes"
	# Better tests are required before to try to use them
	# fontconfigtest slightly modified could work if compiled
	#cd $(DIR_APP) && (./gddemo; ./gdtest demoin.png) &> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
endif

	cd $(DIR_APP) && make install

	rm /usr/lib/libgd.la
	rm -f $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/GD-*
	@rm -rf $(DIR_APP)
	@$(POSTBUILD)
