# Template file for 'eglibc'
pkgname=eglibc
_distver="2.15.1"
_datesvn="20120525"
version="${_distver}.${_datesvn}"
revision=2
wrksrc="eglibc-${_distver}_${_datesvn}"
depends="base-files eglibc-locales>=$version"
makedepends="bison perl"
provides="glibc-${_distver}"
replaces="glibc>=0"
softreplace=yes
subpackages="eglibc-locales eglibc-devel eglibc-nscd"
nostrip_files="ld-$version.so libc-$version.so libpthread-$version.so libthread_db-1.0.so"
conf_files="/etc/rpc /etc/ld.so.conf /etc/nsswitch.conf"
homepage="http://www.eglibc.org"
short_desc="Embedded GLIBC"
maintainer="Juan RP <xtraeme@gmail.com>"
license="GPL-3, BSD"
distfiles="http://xbps.nopcode.org/distfiles/${pkgname}-${_distver}_${_datesvn}.tar.xz"
checksum=66999ad696cf62077e7b0342987c9bff266e440c9fede4fbdf56043bc8f6a56b
long_desc="
 Embedded GLIBC (EGLIBC) is a variant of the GNU C Library (GLIBC) that is
 designed to work well on embedded systems. EGLIBC strives to be source and
 binary compatible with GLIBC. EGLIBC's goals include reduced footprint,
 configurable components, better support for cross-compilation and cross-testing."

do_configure() {
	mkdir build && cd build
	../configure ${CONFIGURE_SHARED_ARGS} --enable-stackguard-randomization \
		--enable-bind-now --enable-kernel=2.6.27 --enable-multi-arch \
		--without-cvs --without-gd --disable-profile \
		--libdir=/usr/lib --libexecdir=/usr/lib
}

do_build() {
	cd build && make ${makejobs}
}

do_install() {
	# Do not install stuff into lib64 dirs.
	vmkdir lib
	cd ${DESTDIR} && ln -sf lib lib64 && cd $wrksrc

	# Create DESTDIR/etc/ld.so.conf
	vmkdir etc/ld.so.conf.d
	echo "include /etc/ld.so.conf.d/*.conf" > ${DESTDIR}/etc/ld.so.conf

	cd build
	make install_root=${DESTDIR} install

	# create /etc/default/libc-locales
	vinstall ${FILESDIR}/libc-locales 644 etc/default
	cp ${wrksrc}/localedata/SUPPORTED ${wrksrc}/build
	sed -i	-e "s|/| |g" \
		-e 's|\\| |g' \
		-e "s|SUPPORTED-LOCALES=||" \
		-e "s|^|#|g" ${wrksrc}/build/SUPPORTED
	cat ${wrksrc}/build/SUPPORTED >> ${DESTDIR}/etc/default/libc-locales

	# ldd is a bash script, so make it run as such.
	. $XBPS_HELPERSDIR/replace-interpreter.sh
	replace_interpreter bash ${DESTDIR}/usr/bin/ldd

	# On x86_64, add dynamic linker's 32bit version to ldd.
	if [ "$XBPS_MACHINE" = "x86_64" ]; then
		rtldlist="/lib/ld-linux.so.2 /lib/ld-linux-x86-64.so.2"
		sed -i "s|^RTLDLIST.*$|RTLDLIST=\"$rtldlist\"|" \
			${DESTDIR}/usr/bin/ldd
	fi
	install -m644 ${FILESDIR}/nsswitch.conf ${DESTDIR}/etc
	echo "/usr/local/lib" > ${DESTDIR}/etc/ld.so.conf.d/usr_local.conf
	chmod 644 ${DESTDIR}/etc/ld.so.conf.d/usr_local.conf
	# Remove unused files.
	rm -f ${DESTDIR}/etc/ld.so.cache
	rm -f ${DESTDIR}/etc/localtime
	rm -f ${DESTDIR}/lib64
	# Remove timezone stuff, provided by tzdata.
	rm -f ${DESTDIR}/usr/sbin/zic
	rm -f ${DESTDIR}/usr/sbin/zdump
	rm -f ${DESTDIR}/usr/bin/tzselect
	rm -rf ${DESTDIR}/usr/share/zoneinfo
	# Modify some files to not have the same hash than in glibc pkg.
	for f in ${conf_files} /etc/ld.so.conf.d/usr_local.conf \
		/usr/lib/gconv/gconv-modules; do
		echo "# $pkgname-$version" >> ${DESTDIR}${f}
	done

	# Switch to /usr.
	vmkdir usr/sbin
	vmove "lib/*" usr/lib
	vmove "sbin/*" usr/sbin
}
