# Description: The C library used in the GNU system
# URL: http://www.gnu.org/software/libc/
# Maintainer: Alexander Zhirov

name=glibc
version=2.36
release=6
_kernel_version=6.1.63

source=(
	https://ftp.gnu.org/gnu/glibc/glibc-$version.tar.xz
    https://www.kernel.org/pub/linux/kernel/v6.x/linux-$_kernel_version.tar.xz
    glibc-$version-6.patch
    hosts
	resolv.conf
	nsswitch.conf
	host.conf
	ld.so.conf
    locale-gen
	locale.gen.in
)

build() {
    # install kernel headers
    make -C $SRC/linux-$_kernel_version mrproper
    make -C $SRC/linux-$_kernel_version INSTALL_HDR_PATH=$PKG/usr headers_install
    chown root:root $PKG/usr

    patch -p1 -d $SRC/$name-${version:0:4} -i $SRC/$name-$version-6.patch

    mkdir $SRC/build
    cd $SRC/build

    export libc_cv_slibdir=/lib

    $SRC/$name-$version/configure \
        --prefix=/usr \
        --libdir=/usr/lib \
        --libexecdir=/usr/lib \
        --with-headers=$PKG/usr/include \
        --enable-kernel=4.9 \
        --enable-stack-protector=strong \
        --enable-bind-now \
        --enable-multi-arch \
        --disable-profile \
        --disable-werror \
        --without-gd
    make
    #make check
    make install_root=$PKG install

    install -m 0644 $SRC/$name-${version:0:4}/nscd/nscd.conf $PKG/etc
    install -d $PKG/var/db/nscd

    install -m 0644 $SRC/{hosts,resolv.conf,nsswitch.conf,host.conf,ld.so.conf} $PKG/etc

    ln -sf ../usr/share/zoneinfo/UTC $PKG/etc/localtime
    install -d $PKG/etc/ld.so.conf.d $PKG/usr/lib/locale
    touch $PKG/etc/ld.so.cache

    install -Dm755 $SRC/locale-gen $PKG/usr/sbin/locale-gen
    install -Dm644 $SRC/locale.gen.in $PKG/etc/locale.gen
    sed -e '1,3d' -e 's|/| |g' -e 's|\\| |g' -e 's|^|#|g' \
        $SRC/$name-$version/localedata/SUPPORTED >> $PKG/etc/locale.gen

    LC_ALL=C ./elf/ld.so --library-path . $PKG/usr/bin/localedef \
        --force --quiet \
        --inputfile=$SRC/$name-$version/localedata/locales/C \
        --charmap=$SRC/$name-$version/localedata/charmaps/UTF-8 \
        $PKG/usr/lib/locale/C.UTF-8 || true

    rm -r $PKG/usr/share/locale \
        $PKG/usr/bin/{tzselect,zdump} \
        $PKG/usr/sbin/zic

    install -Dm644 $SRC/$name-$version/intl/locale.alias \
        $PKG/usr/share/locale/locale.alias

    find $PKG -name "*install.cmd" -delete
    find $PKG -name ".\install" -delete
}