###############################################################################
# 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: glibc 3676 2009-10-10 22:09:43Z owes $
#                                                                             #
###############################################################################

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

include Config

PKG_NAME   = glibc
VER        = 2.7
HOST_ARCH  = all
OTHER_SRC  = yes

THISAPP    = glibc-$(VER)
DL_FILE    = $(THISAPP).tar.bz2
DL_FROM    = $(URL_GNU)/glibc
DIR_APP    = $(DIR_SRC)/$(THISAPP)

ifeq "$(MACHINE)" "i486"
# These flags will make i486 builds xen-friendly
CFLAGS     = -O2 -mno-tls-direct-seg-refs
# Force glibc to be 486 compatible
OPTIONS    = --host=i486-pc-linux-gnu --build=i486-pc-linux-gnu
else
CFLAGS     =
OPTIONS    =
endif

CXXFLAGS   =
TARGET     = $(DIR_INFO)/$(STAGE_ORDER)_$(STAGE)/$(THISAPP)

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

objects = $(DL_FILE) \
	glibc-libidn-$(VER).tar.gz

$(DL_FILE)				= $(DL_FROM)/$(DL_FILE)
glibc-libidn-$(VER).tar.gz		= $(URL_GNU)/glibc/glibc-libidn-$(VER).tar.gz

$(DL_FILE)_MD5				= 065c5952b439deba40083ccd67bcc8f7
glibc-libidn-$(VER).tar.gz_MD5		= d62b1ae215b1acf31fe1def7fc8d61a9

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) : $(patsubst %,$(DIR_DL)/%,$(objects))
	@$(PREBUILD)
	@rm -rf $(DIR_APP) $(DIR_SRC)/glibc-build && cd $(DIR_SRC) && tar -jxf $(DIR_DL)/$(DL_FILE)
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_PTR-MANGLE_alpha.patch
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_ssp-error_alpha.patch
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_notimestamp.patch
	cd $(DIR_APP) && patch -Np1 -i $(DIR_PATCHES)/$(THISAPP)_fetestexcept-fix.patch

	@mkdir $(DIR_SRC)/glibc-build

ifeq "$(MACHINE)" "i486"
	cd $(DIR_SRC)/glibc-build && echo "CFLAGS += -march=i486 -mtune=generic" > configparms
endif

ifeq "$(MACHINE)" "sparc"
	cd $(DIR_SRC)/glibc-build && echo "ac_cv_host=sparcv9-unknown-linux-gnu" > config.cache
endif

ifeq "$(STAGE)" "toolchain"
	cd $(DIR_SRC)/glibc-build && ../$(THISAPP)/configure	--prefix=/$(TOOLS_DIR) \
								--disable-profile \
								--enable-add-ons \
								--enable-kernel=$(REQUIRED_KERNEL) \
								--with-binutils=/$(TOOLS_DIR)/bin \
								--without-gd \
								--with-headers=/$(TOOLS_DIR)/include \
								--without-selinux \
								--cache-file=config.cache \
								$(OPTIONS)

	cd $(DIR_SRC)/glibc-build && make PARALLELMFLAGS=-j$(PARALLELISM)
	mkdir -v /$(TOOLS_DIR)/etc
	touch /$(TOOLS_DIR)/etc/ld.so.conf
	cd $(DIR_SRC)/glibc-build && make install

ifeq "$(RUNNING_TEST)" "yes"
	-cd $(DIR_SRC)/glibc-build; make -j 1 -k check &> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
endif
endif

ifeq "$(STAGE)" "base"
	cd $(DIR_APP) && tar zxf $(DIR_DL)/glibc-libidn-$(VER).tar.gz
	cd $(DIR_APP) && mv glibc-libidn-$(VER) libidn
	# We don't install the vi_VN locale, but there's a bug with it and bash, so disable it just to be safe
	cd $(DIR_APP) && sed -i '/vi_VN.TCVN/d' localedata/SUPPORTED
	cd $(DIR_APP) && sed -i 's|libs -o|libs -L/usr/lib -Wl,-dynamic-linker=$(LINKER) -o|' \
			scripts/test-installation.pl
	# We don't install any other shells than bash, but just in case, force ldd to use bash
	cd $(DIR_APP) && sed -i 's|@BASH@|/bin/bash|' elf/ldd.bash.in
	cd $(DIR_SRC)/glibc-build && ../$(THISAPP)/configure	--prefix=/usr \
								--disable-profile \
								--enable-add-ons \
								--enable-kernel=$(REQUIRED_KERNEL) \
								--libexecdir=/usr/lib/glibc \
								--cache-file=config.cache \
								$(OPTIONS)

	cd $(DIR_SRC)/glibc-build && make PARALLELMFLAGS=-j$(PARALLELISM)
	touch /etc/ld.so.conf
	cd $(DIR_SRC)/glibc-build && make install

	# Creating the locales
	install -d /usr/lib/locale
	cd $(DIR_SRC)/glibc-build && localedef -i en_US -f UTF-8 en_US.utf8
	cd $(DIR_SRC)/glibc-build && localedef -i en_US -f ISO-8859-1 en_US
	cd $(DIR_SRC)/glibc-build && localedef -i en_GB -f UTF-8 en_GB.utf8
	cd $(DIR_SRC)/glibc-build && localedef -i en_GB -f ISO-8859-1 en_GB

	# Configuring timezone
	cp -v --remove-destination /usr/share/zoneinfo/GMT /etc/localtime

	# Now copy some files we'll need later when we reduce glibc for the installer
	cd $(DIR_SRC)/glibc-build && install -dv				/usr/lib/libc_pic
	cd $(DIR_SRC)/glibc-build && install --mode=0644 libc_pic.a		/usr/lib/libc_pic/libc_pic.a
	cd $(DIR_SRC)/glibc-build && install --mode=0644 libc.map		/usr/lib/libc_pic/libc_pic.map
	cd $(DIR_SRC)/glibc-build && install --mode=0644 dlfcn/libdl_pic.a	/usr/lib/libc_pic/libdl_pic.a
	cd $(DIR_SRC)/glibc-build && install --mode=0644 libdl.map		/usr/lib/libc_pic/libdl_pic.map
	cd $(DIR_SRC)/glibc-build && install --mode=0644 elf/soinit.os		/usr/lib/libc_pic/soinit.o
	cd $(DIR_SRC)/glibc-build && install --mode=0644 elf/sofini.os		/usr/lib/libc_pic/sofini.o

ifeq "$(RUNNING_TEST)" "yes"
	-cd $(DIR_SRC)/glibc-build; make -j 1 -k check &> $(DIR_TEST)/$(THISAPP)-$(STAGE_ORDER).log
endif
endif

	# Cleanup
	@rm -rf $(DIR_APP) $(DIR_SRC)/glibc-build
	@$(POSTBUILD)
