#!/bin/sh
# Copyright (C) 2002, 2005  Slackware Linux, Inc.
# Copyright 2005, 2006, 2011, 2012, 2018, 2026  Patrick J. Volkerding, Sebeka, Minnesota, USA
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Swap glibc on the fly.
#
# If we're on a running system we have to handle this _very_ carefully.  :-)
# The tricks involved here get trickier every time...

# Install the glibc libraries.
# Try to protect with flock against another installpkg/removepkg running
# ldconfig while we're doing the on-the-fly install:
mkdir -p run/lock/pkgtools
( flock 9 || exit 11
  ( cd lib64/incoming

    # Everything's going files these days. Otherwise the well-loved Easy Anti
    # Cheat thinks that we're tampering with the system. These have been
    # symlinks since the beginning, but we better go along with the times. The
    # first thing we'll do is make sure that if any of these are symlinks in
    # /lib64, make them into files.
    for library in * ; do
      if [ -L ../${library} ]; then
        ( cd .. ; mv $(readlink ${library}) ${library} )
      fi
    done

    # OK, we've made sure all of those are files, not symlinks, and gotten rid
    # of the most recent set of linked files (e.g. libc-2.42.so) that was in
    # use. But, in case there were other old glibc versions installed in your
    # /lib64 for some reason, we'll remove them now to avoid warnings from ldconfig.
    # If you really don't want to do this for some reason, create a file
    # /etc/glibc-nowipe.
    if [ ! -r ../../etc/glibc-nowipe ]; then
      ( cd ..
        for library in ld-linux libBrokenLocale libanl libc libc_malloc_debug libdl libm libmvec libnsl libnss_compat libnss_db libnss_dns libnss_files libnss_hesiod libpthread libresolv librt libthread_db libutil ; do
          rm -f ${library}-*2.??.so
        done
      )
    fi

    # Since libcrypt remains symlinked, we do need to ensure there are no old
    # versions that might try to take over the link:
    rm -f ../libcrypt*-2.??.so

    # Send them home:
    mv * ..

    # Get rid of the incoming directory:
    cd ..
    rmdir incoming

  )

) 9> run/lock/pkgtools/ldconfig.lock


# At some point I put this "dead code" inside a conditional, but the package
# system recognized these links as installed and owned by the package.
# This painted me into a corner -- if this block is dropped then removepkg
# removes the links and crashes the system. So we'll keep it around to protect
# these locations from removal.
if false ; then
( cd lib64 ; rm -rf libm.so.6 )
( cd lib64 ; ln -sf libm-2.42.so libm.so.6 )
( cd lib64 ; rm -rf libnss_files.so.2 )
( cd lib64 ; ln -sf libnss_files-2.42.so libnss_files.so.2 )
( cd lib64 ; rm -rf libresolv.so.2 )
( cd lib64 ; ln -sf libresolv-2.42.so libresolv.so.2 )
( cd lib64 ; rm -rf libutil.so.1 )
( cd lib64 ; ln -sf libutil-2.42.so libutil.so.1 )
( cd lib64 ; rm -rf libnss_compat.so.2 )
( cd lib64 ; ln -sf libnss_compat-2.42.so libnss_compat.so.2 )
( cd lib64 ; rm -rf libthread_db.so.1 )
( cd lib64 ; ln -sf libthread_db-2.42.so libthread_db.so.1 )
( cd lib64 ; rm -rf libnss_hesiod.so.2 )
( cd lib64 ; ln -sf libnss_hesiod-2.42.so libnss_hesiod.so.2 )
( cd lib64 ; rm -rf libanl.so.1 )
( cd lib64 ; ln -sf libanl-2.42.so libanl.so.1 )
( cd lib64 ; rm -rf libcrypt.so.1 )
( cd lib64 ; ln -sf libcrypt1-2.42.so libcrypt.so.1 )
( cd lib64 ; rm -rf libcrypt.so.2 )
( cd lib64 ; ln -sf libcrypt2-2.42.so libcrypt.so.2 )
( cd lib64 ; rm -rf libBrokenLocale.so.1 )
( cd lib64 ; ln -sf libBrokenLocale-2.42.so libBrokenLocale.so.1 )
( cd lib64 ; rm -rf ld-linux.so.2 )
( cd lib64 ; ln -sf ld-linux-2.42.so ld-linux.so.2 )
( cd lib64 ; rm -rf ld-linux-x86-64.so.2 )
( cd lib64 ; ln -sf ld-linux-x86-64-2.42.so ld-linux-x86-64.so.2 )
( cd lib64 ; rm -rf libdl.so.2 )
( cd lib64 ; ln -sf libdl-2.42.so libdl.so.2 )
( cd lib64 ; rm -rf libnss_dns.so.2 )
( cd lib64 ; ln -sf libnss_dns-2.42.so libnss_dns.so.2 )
( cd lib64 ; rm -rf libpthread.so.0 )
( cd lib64 ; ln -sf libpthread-2.42.so libpthread.so.0 )
( cd lib64 ; rm -rf libc.so.6 )
( cd lib64 ; ln -sf libc-2.42.so libc.so.6 )
( cd lib64 ; rm -rf librt.so.1 )
( cd lib64 ; ln -sf librt-2.42.so librt.so.1 )
fi

( cd lib64 ; rm -rf libcrypt.so.1 )
( cd lib64 ; ln -sf libcrypt.so.1.1.0 libcrypt.so.1 )
( cd lib64 ; rm -rf libcrypt.so.2 )
( cd lib64 ; ln -sf libcrypt.so.2.0.0 libcrypt.so.2 )
