description="The Glibc package contains the main C library."
url="https://www.gnu.org/software/libc/"

packager="Grat-OS Team"

gccversion=13.2.0

name=glibc
version=2.39
release=4

source=(http://www.linuxfromscratch.org/patches/downloads/$name/glibc-$version-fhs-1.patch
        http://ftp.gnu.org/gnu/$name/$name-$version.tar.xz
        glibc.locales.supported)

_ARCH=$(uname -m)
case ${_ARCH} in
  x86_64)
    CFLAGS+=" -march=x86-64-v2"
  ;;
esac

build() {

  cd glibc-$version

  sed -e '/NOTIFY_REMOVED)/s/)/ \&\& data.attr != NULL)/' \
      -i sysdeps/unix/sysv/linux/mq_notify.c

  # Some of the Glibc programs use the non-FHS compilant /var/db directory to store their runtime data. Apply the following patch to make such programs store their runtime data in the FHS-compliant locations
  patch -Np1 -i ../glibc-$version-fhs-1.patch

  case $(uname -m) in
    x86_64) 
      mkdir -p $PKG/usr/lib64
      GCC_INCDIR=/usr/lib/gcc/x86_64-pc-linux-gnu/$gccversion/include
      ln -s ../lib/ld-linux-x86-64.so.2 $PKG/usr/lib64
      ln -s ../lib/ld-linux-x86-64.so.2 $PKG/usr/lib64/ld-lsb-x86-64.so.3
    ;;
  esac

  mkdir -v build
  cd       build

  echo "rootsbindir=/usr/sbin" > configparms

  ../configure --prefix=/usr                            \
              --with-bugurl=https://code.grat-os.fr/Grat-OS/ \
              --disable-werror                          \
              --enable-kernel=6.1                       \
              --enable-stack-protector=strong           \
              --enable-static-pie                       \
              --disable-profile                         \
              libc_cv_slibdir=/usr/lib

  make || make -j1

  # make -k check 2>&1 | tee glibc-check-log
  # grep Error glibc-check-log

  make install_root=$PKG install

  mkdir -p $PKG/etc
  cp -v ../nscd/nscd.conf $PKG/etc/nscd.conf
  mkdir -pv $PKG/var/cache/nscd

  touch $PKG/etc/ld.so.conf

  mkdir -pv $PKG/usr/lib/locale

  cp $SRC/glibc.locales.supported \
  ../localedata/SUPPORTED

  make install_root=$PKG localedata/install-locales

  cat > $PKG/etc/nsswitch.conf << "EOF"
# Begin /etc/nsswitch.conf

passwd: files
group: files
shadow: files

hosts: mymachines mdns_minimal [NOTFOUND=return] resolve [!UNAVAIL=return] files myhostname dns
networks: files

protocols: files
services: files
ethers: files
rpc: files

# End /etc/nsswitch.conf
EOF

  cat > $PKG/etc/ld.so.conf << "EOF"
# Begin /etc/ld.so.conf
/usr/local/lib
/opt/lib

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

  mkdir $PKG/etc/ld.so.conf.d

  rm -f $PKG/usr/share/info/dir.gz
  rm -f $PKG/usr/share/info/dir
}
