### this file is sourced not run
PKGVER=2.19
PKGBUILD=1
PKGARCH=x86_64

TARBALL=glibc-2.19.tar.xz
MD5SUM=e26b8cc666b162f999404b03970f14e4
SRC_URL=http://distro.ibiblio.org/fatdog/source/710/clfs-packages-3.0.0.tar
BUNDLE=clfs-packages-3.0.0.tar

SLACKREQ=
SLACKDESC="glibc: glibc $PKGVER (Core system library)  
glibc:  
glibc: The Glibc package contains the main C library. This library provides 
glibc: the basic routines for allocating memory, searching directories, 
glibc: opening and closing files, reading and writing files, string handling, 
glibc: pattern matching, arithmetic, and so on.  
glibc: 
glibc: This is the 64-bit version of glibc.
glibc: 
glibc: 
glibc:
"

### override download for LFS sources
# $1-src-dir $2-tarball 
pkg_download() {
	echo downloading $BUNDLE for $TARBALL
	wget -P $1 $SRC_URL
	tar -xf $1/$BUNDLE --strip-components=1 -C $1 
	rm $1/$BUNDLE
}

### default pkg_prepare
### default pkg_package
### build
pkg_build() {
	cd /tmp/glibc*
	echo applying patches... &&
	
	# Debian 2.19-18+deb8u3 patches for mainly to address CVE-2015-7547 
	for p in \
		glibc-2.19-git-updates.patch \
		glibc-2.19-resolv-failure.patch \
		glibc-2.19-CVE-2015-7547.patch;
	do
		patch -Np1 -i ../pkg/$p || return 1
	done &&	
	
	# apply patches (LFS-7.1 for glibc-2.14.1, also applicable for LFS 7.4)
	#DL=/lib/ld-linux-armhf.so.3
	#sed -i "s|libs -o|libs -L/usr/lib -Wl,-dynamic-linker=$DL -o|" \
	#	scripts/test-installation.pl &&

	# CLFS 3.0.0 adjust linker patch
	LINKER=$(readelf -l /tools/bin/bash | sed -n 's@.*interpret.*/tools\(.*\)]$@\1@p') &&
	sed -i "s|libs -o|libs -L/usr/lib64 -Wl,-dynamic-linker=${LINKER} -o|" \
	  scripts/test-installation.pl &&
	unset LINKER &&
	sed -i 's/\\$$(pwd)/`pwd`/' timezone/Makefile
	
	# build dir
	mkdir -v ../glibc-build
	cd ../glibc-build
	
	# extra CFLAGS for default native environment
	#echo 'CFLAGS += -mfloat-abi=hard -mfpu=vfpv3-d16 -march=armv7-a -mcpu=cortex-a8 -mtune=cortex-a8 -pipe -O3' > /tmp/glibc-build/configparms
	
	# use /usr/lib64
	echo "slibdir=/lib64" >> configparms
	
	CC="gcc ${BUILD64}" CXX="g++ ${BUILD64}" \
		../glibc-2.19/configure --prefix=/usr \
		--disable-profile --enable-kernel=2.6.32 \
		--libexecdir=/usr/lib64/glibc --libdir=/usr/lib64 \
		--enable-obsolete-rpc &&
		
	#sh &&
	make $MAKEFLAGS &&
	#sh &&
	touch /etc/ld.so.conf &&
	make install &&
	rm -v /usr/include/rpcsvc/*.x &&
	#sh &&
	cp -v ../glibc-2.19/nscd/nscd.conf /etc/nscd.conf &&
	mkdir -pv /var/cache/nscd  &&
		
	mkdir -pv /usr/lib64/locale &&
	localedef -i en_US -f ISO-8859-1 --no-archive en_US &&
	localedef -i en_US -f UTF-8 --no-archive en_US.UTF-8 &&
	localedef -i en_AU -f ISO-8859-1 --no-archive en_AU &&
	localedef -i en_AU -f UTF-8 --no-archive en_AU.UTF-8 &&
	cp -v ../glibc-2.19/localedata/SUPPORTED /usr/share/i18n &&

	# generate nsswitch.conf
	cat << "EOF" > /etc/nsswitch.conf &&
# Begin /etc/nsswitch.conf

passwd: files
group: files
shadow: files

hosts: files dns
networks: files

protocols: files
services: files
ethers: files
rpc: files
# End /etc/nsswitch.conf
EOF

	# now generate a proper ld.so.conf
	cat << "EOF" > /etc/ld.so.conf &&
# Begin /etc/ld.so.conf
/usr/local/lib
/usr/local/lib64
/opt/lib
/opt/lib64

# Add an include directory
include /etc/ld.so.conf.d/*.conf

EOF
	mkdir -pv /etc/ld.so.conf.d &&

	pkg_build_slackdesc
} 
