###############################################################################
# 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: perl 2229 2008-12-27 07:45:45Z gespinasse $
#                                                                             #
###############################################################################

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

include Config

PKG_NAME   = perl
VER        = 5.8.8
HOST_ARCH  = all
OTHER_SRC  = yes

THISAPP    = perl-$(VER)
DL_FILE    = $(THISAPP).tar.bz2
DL_FROM    = http://www.cpan.org/src
DIR_APP    = $(DIR_SRC)/$(THISAPP)
TARGET     = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(THISAPP)

PATCH1     = $(THISAPP)-libc-2.patch
# PATCH2 is now include in source tree
PATCH2     = $(THISAPP)-no_page_h-1.patch
# PATCH3 - combined CVE-2007-5116 and CVE-2008-1927
PATCH3     = $(THISAPP)-security_fixes-1.patch

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

objects = $(DL_FILE) \
	$(PATCH1) \
	$(PATCH3)

$(DL_FILE)			= $(DL_FROM)/$(DL_FILE)
$(PATCH1)			= $(URL_LFS)/$(PKG_NAME)/$(PATCH1)
$(PATCH3)			= $(URL_LFS)/$(PKG_NAME)/$(PATCH3)

$(DL_FILE)_MD5			= a377c0c67ab43fd96eeec29ce19e8382
$(PATCH1)_MD5			= 3bf8aef1fb6eb6110405e699e4141f99
$(PATCH3)_MD5			= 918a8f41d9e4fe1e272381e58be71497

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 jxf $(DIR_DL)/$(DL_FILE)

	cd $(DIR_APP) && patch -Np1 -i $(DIR_DL)/$(PATCH1)
	cd $(DIR_APP) && patch -Np1 -i $(DIR_DL)/$(PATCH3)
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(PATCH2)				# Change on linux-2.6.25 and later headers
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_notimestamp.patch	# So anyone has same md5 for libperl

ifeq "$(STAGE)" "toolchain"
	# fix an incompatibility with gcc-4.2.3:
	cd $(DIR_APP) && mv -v makedepend.SH makedepend.SH.orig; sed 's/command /command[ -]/' makedepend.SH.orig > makedepend.SH
	cd $(DIR_APP) && ./configure.gnu	--prefix=/$(TOOLS_DIR) \
						-Dstatic_ext='Data/Dumper IO Fcntl POSIX' \
						-Dmyhostname=localhost
	cd $(DIR_APP) && make perl utilities
	cd $(DIR_APP) && cp -v perl pod/pod2man /$(TOOLS_DIR)/bin
	cd $(DIR_APP) && mkdir -pv /$(TOOLS_DIR)/lib/perl5/$(VER)
	cd $(DIR_APP) && cp -Rv lib/* /$(TOOLS_DIR)/lib/perl5/$(VER)
endif

ifeq "$(STAGE)" "base"

	# fix an incompatibility with gcc-4.2.3:
	cd $(DIR_APP) && sed -i 's/command /command[ -]/' makedepend.SH
	cd $(DIR_APP) && ./configure.gnu	--prefix=/usr \
						-Dman1dir=/usr/share/man/man1 \
						-Dman3dir=/usr/share/man/man3 \
						-Dpager="/usr/bin/less -isR" \
						-Dd_dosuid \
						-Ui_db \
						-Ui_gdbm \
						-Ui_ndbm \
						-Ui_dbm \
						-Dmyhostname=localhost \
						-Uoptimize \
						-Duseshrplib \
						-Darchname=$(MACHINE)-linux
	cd $(DIR_APP) && make
	cd $(DIR_APP) && make install

ifeq "$(RUNNING_TEST)" "yes"
	# needed to pass lib/Net/t/hostname test
	sed -i "s/localhost/localhost $$(hostname)/" /etc/hosts
	cd $(DIR_APP) && make -j 1 test &> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
	sed -i "s/localhost.*/localhost/" /etc/hosts # no more needed after the tests
endif
endif

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