#!/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 lib/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
    # /lib, 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
    # /lib 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


# Handle config files:
config() {
  NEW="$1"
  OLD="$(dirname $NEW)/$(basename $NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "$(cat $OLD | md5sum)" = "$(cat $NEW | md5sum)" ]; then # toss the redundant copy
    rm $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config etc/nscd.conf.new


# 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 lib ; rm -rf libm.so.6 )
( cd lib ; ln -sf libm-2.42.so libm.so.6 )
( cd lib ; rm -rf libnss_files.so.2 )
( cd lib ; ln -sf libnss_files-2.42.so libnss_files.so.2 )
( cd lib ; rm -rf libresolv.so.2 )
( cd lib ; ln -sf libresolv-2.42.so libresolv.so.2 )
( cd lib ; rm -rf libutil.so.1 )
( cd lib ; ln -sf libutil-2.42.so libutil.so.1 )
( cd lib ; rm -rf libnss_compat.so.2 )
( cd lib ; ln -sf libnss_compat-2.42.so libnss_compat.so.2 )
( cd lib ; rm -rf libthread_db.so.1 )
( cd lib ; ln -sf libthread_db-2.42.so libthread_db.so.1 )
( cd lib ; rm -rf libnss_hesiod.so.2 )
( cd lib ; ln -sf libnss_hesiod-2.42.so libnss_hesiod.so.2 )
( cd lib ; rm -rf libanl.so.1 )
( cd lib ; ln -sf libanl-2.42.so libanl.so.1 )
( cd lib ; rm -rf libcrypt.so.1 )
( cd lib ; ln -sf libcrypt1-2.42.so libcrypt.so.1 )
( cd lib ; rm -rf libcrypt.so.2 )
( cd lib ; ln -sf libcrypt2-2.42.so libcrypt.so.2 )
( cd lib ; rm -rf libBrokenLocale.so.1 )
( cd lib ; ln -sf libBrokenLocale-2.42.so libBrokenLocale.so.1 )
( cd lib ; rm -rf ld-linux.so.2 )
( cd lib ; ln -sf ld-linux-2.42.so ld-linux.so.2 )
( cd lib ; rm -rf ld-linux-x86-64.so.2 )
( cd lib ; ln -sf ld-linux-x86-64-2.42.so ld-linux-x86-64.so.2 )
( cd lib ; rm -rf libdl.so.2 )
( cd lib ; ln -sf libdl-2.42.so libdl.so.2 )
( cd lib ; rm -rf libnss_dns.so.2 )
( cd lib ; ln -sf libnss_dns-2.42.so libnss_dns.so.2 )
( cd lib ; rm -rf libpthread.so.0 )
( cd lib ; ln -sf libpthread-2.42.so libpthread.so.0 )
( cd lib ; rm -rf libc.so.6 )
( cd lib ; ln -sf libc-2.42.so libc.so.6 )
( cd lib ; rm -rf librt.so.1 )
( cd lib ; ln -sf librt-2.42.so librt.so.1 )
fi

( cd bin ; rm -rf sln )
( cd bin ; ln -sf /sbin/sln sln )
( cd lib ; rm -rf libcrypt.so.1 )
( cd lib ; ln -sf libcrypt.so.1.1.0 libcrypt.so.1 )
( cd lib ; rm -rf libcrypt.so.2 )
( cd lib ; ln -sf libcrypt.so.2.0.0 libcrypt.so.2 )
( cd usr/bin ; rm -rf ld.so )
( cd usr/bin ; ln -sf ../../lib/ld-linux.so.2 ld.so )
( cd usr/doc/glibc-2.44 ; rm -rf COPYING.LIB )
( cd usr/doc/glibc-2.44 ; ln -sf COPYING.LESSERv2 COPYING.LIB )
( cd usr/doc/libxcrypt-4.5.2 ; rm -rf README )
( cd usr/doc/libxcrypt-4.5.2 ; ln -sf README.md README )
( cd usr/doc/libxcrypt-4.5.2 ; rm -rf TODO )
( cd usr/doc/libxcrypt-4.5.2 ; ln -sf TODO.md TODO )
( cd usr/lib ; rm -rf libBrokenLocale.so )
( cd usr/lib ; ln -sf ../../lib/libBrokenLocale.so.1 libBrokenLocale.so )
( cd usr/lib ; rm -rf libanl.so )
( cd usr/lib ; ln -sf ../../lib/libanl.so.1 libanl.so )
( cd usr/lib ; rm -rf libc_malloc_debug.so )
( cd usr/lib ; ln -sf ../../lib/libc_malloc_debug.so.0 libc_malloc_debug.so )
( cd usr/lib ; rm -rf libcrypt.so )
( cd usr/lib ; ln -sf ../../lib/libcrypt.so.2 libcrypt.so )
( cd usr/lib ; rm -rf libm.so )
( cd usr/lib ; ln -sf ../../lib/libm.so.6 libm.so )
( cd usr/lib ; rm -rf libnss_compat.so )
( cd usr/lib ; ln -sf ../../lib/libnss_compat.so.2 libnss_compat.so )
( cd usr/lib ; rm -rf libnss_db.so )
( cd usr/lib ; ln -sf ../../lib/libnss_db.so.2 libnss_db.so )
( cd usr/lib ; rm -rf libnss_hesiod.so )
( cd usr/lib ; ln -sf ../../lib/libnss_hesiod.so.2 libnss_hesiod.so )
( cd usr/lib ; rm -rf libresolv.so )
( cd usr/lib ; ln -sf ../../lib/libresolv.so.2 libresolv.so )
( cd usr/lib ; rm -rf libthread_db.so )
( cd usr/lib ; ln -sf ../../lib/libthread_db.so.1 libthread_db.so )
( cd usr/lib ; rm -rf libxcrypt.a )
( cd usr/lib ; ln -sf libcrypt.a libxcrypt.a )
( cd usr/lib ; rm -rf libxcrypt.so )
( cd usr/lib ; ln -sf ../../lib/libcrypt.so.2 libxcrypt.so )
( cd usr/lib/pkgconfig ; rm -rf libcrypt.pc )
( cd usr/lib/pkgconfig ; ln -sf libxcrypt.pc libcrypt.pc )
