pkgname="glibc"
pkgver=2.42
sources=("${pkgname}-${pkgver}.tar.xz"
  "${pkgname}-${pkgver}-fhs-1.patch"
  "${pkgname}-${pkgver}/nsswitch.conf"
  "${pkgname}-${pkgver}/ld.so.conf"
  "${pkgname}-${pkgver}/tzdata2025b.tar.gz"
)
urls=("https://ftp.gnu.org/gnu/glibc/${sources[0]}"
  "https://www.linuxfromscratch.org/patches/lfs/12.4/${pkgname}-${pkgver}-fhs-1.patch"
  "https://gitea.phywyj.dynv6.net/wyj/LFS-PKGBUILDs/raw/branch/main/lfs/${pkgname}-${pkgver}/nsswitch.conf"
  "https://gitea.phywyj.dynv6.net/wyj/LFS-PKGBUILDs/raw/branch/main/lfs/${pkgname}-${pkgver}/ld.so.conf"
  "https://www.iana.org/time-zones/repository/releases/tzdata2025b.tar.gz"
)
md5sums=("23c6f5a27932b435cae94e087cb8b1f5"
  "9a5997c3452909b1769918c759eff8a2"
  "4caf37ca10aacf7f7bfabddf915e9d25"
  "79220f5a36182b7160e0cf567dc64129"
  "ad65154c48c74a9b311fe84778c5434f"
)
license=(GPL-2.0-or-later LGPL-2.1-or-later)

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  patch -Np1 -i "${distdir}/${sources[1]}"
  sed -e '/unistd.h/i #include <string.h>' \
      -e '/libc_rwlock_init/c\
    __libc_rwlock_define_initialized (, reset_lock);\
    memcpy (&lock, &reset_lock, sizeof (lock));' \
      -i stdlib/abort.c
}

src_build() {
  leaf_clear_flags
  mkdir -pv build
  cd build
  echo "rootsbindir=/usr/sbin" > configparms
  ../configure --prefix=/usr                            \
               --disable-werror                         \
               --enable-kernel=5.4                      \
               --enable-stack-protector=strong          \
               --disable-nscd                           \
               libc_cv_slibdir=/usr/lib
  make
}

_skip_test() {
  test=${1}
  file=${2}
  sed -i "/\b${test} /d" "${srcdir}/${file}"
}

src_check() {
  _skip_test tst-lchmod io/Makefile

  make check
}

src_install() {
  cd "${srcdir}/build"
  install -d "${pkgdir}/etc"
  touch "${pkgdir}/etc/ld.so.conf"
  sed '/test-installation/s@$(PERL)@echo not running@' -i ../Makefile
  make DESTDIR="${pkgdir}" install
  sed '/RTLDLIST=/s@/usr@@g' -i "${pkgdir}/usr/bin/ldd"

  install -m644 "${distdir}/${sources[2]}" "${pkgdir}/etc/nsswitch.conf"

  tar -xf "${distdir}/${sources[4]}"
  ZONEINFO=${pkgdir}/usr/share/zoneinfo
  mkdir -pv $ZONEINFO/{posix,right}
  for tz in etcetera southamerica northamerica europe africa antarctica  \
          asia australasia backward; do
      zic -L /dev/null   -d $ZONEINFO       ${tz}
      zic -L /dev/null   -d $ZONEINFO/posix ${tz}
      zic -L leapseconds -d $ZONEINFO/right ${tz}
  done
  cp -v zone.tab zone1970.tab iso3166.tab $ZONEINFO
  zic -d $ZONEINFO -p America/New_York
  unset ZONEINFO

  install -m644 "${distdir}/${sources[3]}" "${pkgdir}/etc/ld.so.conf"
}

src_preinstall() {
  install -vm755 "${pkgdir}"/usr/lib/*.so.* /usr/lib
}

src_postinstall() {
  localedef -i C -f UTF-8 C.UTF-8
  localedef -i cs_CZ -f UTF-8 cs_CZ.UTF-8
  localedef -i de_DE -f ISO-8859-1 de_DE
  localedef -i de_DE@euro -f ISO-8859-15 de_DE@euro
  localedef -i de_DE -f UTF-8 de_DE.UTF-8
  localedef -i el_GR -f ISO-8859-7 el_GR
  localedef -i en_GB -f ISO-8859-1 en_GB
  localedef -i en_GB -f UTF-8 en_GB.UTF-8
  localedef -i en_HK -f ISO-8859-1 en_HK
  localedef -i en_PH -f ISO-8859-1 en_PH
  localedef -i en_US -f ISO-8859-1 en_US
  localedef -i en_US -f UTF-8 en_US.UTF-8
  localedef -i es_ES -f ISO-8859-15 es_ES@euro
  localedef -i es_MX -f ISO-8859-1 es_MX
  localedef -i fa_IR -f UTF-8 fa_IR
  localedef -i fr_FR -f ISO-8859-1 fr_FR
  localedef -i fr_FR@euro -f ISO-8859-15 fr_FR@euro
  localedef -i fr_FR -f UTF-8 fr_FR.UTF-8
  localedef -i is_IS -f ISO-8859-1 is_IS
  localedef -i is_IS -f UTF-8 is_IS.UTF-8
  localedef -i it_IT -f ISO-8859-1 it_IT
  localedef -i it_IT -f ISO-8859-15 it_IT@euro
  localedef -i it_IT -f UTF-8 it_IT.UTF-8
  localedef -i ja_JP -f EUC-JP ja_JP
  localedef -i ja_JP -f SHIFT_JIS ja_JP.SJIS 2> /dev/null || true
  localedef -i ja_JP -f UTF-8 ja_JP.UTF-8
  localedef -i nl_NL@euro -f ISO-8859-15 nl_NL@euro
  localedef -i ru_RU -f KOI8-R ru_RU.KOI8-R
  localedef -i ru_RU -f UTF-8 ru_RU.UTF-8
  localedef -i se_NO -f UTF-8 se_NO.UTF-8
  localedef -i ta_IN -f UTF-8 ta_IN.UTF-8
  localedef -i tr_TR -f UTF-8 tr_TR.UTF-8
  localedef -i zh_CN -f GB18030 zh_CN.GB18030
  localedef -i zh_HK -f BIG5-HKSCS zh_HK.BIG5-HKSCS
  localedef -i zh_TW -f UTF-8 zh_TW.UTF-8
  localedef -i zh_CN -f UTF-8 zh_CN.UTF-8
}
