#!/bin/sh
# post-installation script for the glibc libraries

# The libraries are packaged and installed in a bogus  'incoming' directory. If the
# directory /lib/incoming exists (notice absolute path), then the installation is occurring
# on a running system and care must be taken upgrading to the new libs.
if [ -x /sbin/ldconfig -a -d /libSFX/incoming ] ; then
	# First create copies of the incoming libraries:
	( cd /libSFX/incoming
		for file in * ; do
			if [ ! -r ../${file}.incoming ]; then
				cp -a $file ../${file}.incoming
			fi
		done
	)
	# Then switch to them all at once:
	/sbin/ldconfig -l /libSFX/*.incoming 2> /dev/null
	# Then, rename and switch to the permanent copies one by one
	( cd /libSFX
		for file in *.incoming ; do
			rm -f `basename $file .incoming`
			cp -a $file `basename $file .incoming`
			/sbin/ldconfig -l `basename $file .incoming`
			rm -f $file
		done
		# Then remove the temporary directory -its' absence is used as a switch later
		rm -rf incoming
	)
else
	# Otherwise, just copy the libs into the real location and continue
	( cd libSFX/incoming
		for file in * ; do
			cp -a $file ..
		done
	)
fi

# Create a link to sln (statically-compiled 'ln')
( cd bin ; rm -rf sln )
( cd bin ; ln -sf ../sbin/sln sln )

# Handle config files, as needed:
config() {
	NEW="$1"
	OLD="`dirname $NEW`/`basename $NEW .new`"
	# Install *.new config file if old file is not present
	if [ ! -r $OLD ]; then
		mv $NEW $OLD
	elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then
		# If the *.new file is the same as the old one, remove the *.new one
		rm $NEW
	fi
	# Otherwise, leave the .new file
}
config etc/nscd.conf.new
config etc/profile.d/glibc.sh.new
# But don't leave this one hanging around
rm -f etc/profile.d/glibc.sh.new

# If there's still lib/incoming (notice relative path) -this is not an upgrade
# of a running system  and we should create links to the new libraries
if [ -d libSFX/incoming ] ; then
( cd libSFX ; rm -rf libnss_nis.so.2 )
( cd libSFX ; ln -sf libnss_nis-VERSION.so libnss_nis.so.2 )
( cd libSFX ; rm -rf libm.so.6 )
( cd libSFX ; ln -sf libm-VERSION.so libm.so.6 )
( cd libSFX ; rm -rf libnss_files.so.2 )
( cd libSFX ; ln -sf libnss_files-VERSION.so libnss_files.so.2 )
( cd libSFX ; rm -rf libresolv.so.2 )
( cd libSFX ; ln -sf libresolv-VERSION.so libresolv.so.2 )
( cd libSFX ; rm -rf libnsl.so.1 )
( cd libSFX ; ln -sf libnsl-VERSION.so libnsl.so.1 )
( cd libSFX ; rm -rf libutil.so.1 )
( cd libSFX ; ln -sf libutil-VERSION.so libutil.so.1 )
( cd libSFX ; rm -rf libnss_compat.so.2 )
( cd libSFX ; ln -sf libnss_compat-VERSION.so libnss_compat.so.2 )
( cd libSFX ; rm -rf libthread_db.so.1 )
( cd libSFX ; ln -sf libthread_db-1.0.so libthread_db.so.1 )
( cd libSFX ; rm -rf libnss_hesiod.so.2 )
( cd libSFX ; ln -sf libnss_hesiod-VERSION.so libnss_hesiod.so.2 )
( cd libSFX ; rm -rf libanl.so.1 )
( cd libSFX ; ln -sf libanl-VERSION.so libanl.so.1 )
( cd libSFX ; rm -rf libcrypt.so.1 )
( cd libSFX ; ln -sf libcrypt-VERSION.so libcrypt.so.1 )
( cd libSFX ; rm -rf libBrokenLocale.so.1 )
( cd libSFX ; ln -sf libBrokenLocale-VERSION.so libBrokenLocale.so.1 )
( cd libSFX ; rm -rf ld-linux.so.2 )
( cd libSFX ; ln -sf ld-VERSION.so ld-linux.so.2 )
( cd libSFX ; rm -rf libdl.so.2 )
( cd libSFX ; ln -sf libdl-VERSION.so libdl.so.2 )
( cd libSFX ; rm -rf libnss_dns.so.2 )
( cd libSFX ; ln -sf libnss_dns-VERSION.so libnss_dns.so.2 )
( cd libSFX ; rm -rf libpthread.so.0 )
( cd libSFX ; ln -sf libpthread-VERSION.so libpthread.so.0 )
( cd libSFX ; rm -rf libnss_nisplus.so.2 )
( cd libSFX ; ln -sf libnss_nisplus-VERSION.so libnss_nisplus.so.2 )
( cd libSFX ; rm -rf libc.so.6 )
( cd libSFX ; ln -sf libc-VERSION.so libc.so.6 )
( cd libSFX ; rm -rf librt.so.1 )
( cd libSFX ; ln -sf librt-VERSION.so librt.so.1 )
( cd libSFX ; rm -rf libcidn.so.1 )
( cd libSFX ; ln -sf libcidn-VERSION.so libcidn.so.1 )

# now finally get rid of bogus incoming path 
( rm -rf lib/incoming )
fi

# Create or refresh some links in usr/lib
( cd usr/libSFX ; rm -rf libnss_nisplus.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libnss_nisplus.so.2 libnss_nisplus.so )
( cd usr/libSFX ; rm -rf libresolv.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libresolv.so.2 libresolv.so )
( cd usr/libSFX ; rm -rf libnsl.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libnsl.so.1 libnsl.so )
( cd usr/libSFX ; rm -rf libnss_hesiod.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libnss_hesiod.so.2 libnss_hesiod.so )
( cd usr/libSFX ; rm -rf libnss_files.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libnss_files.so.2 libnss_files.so )
( cd usr/libSFX ; rm -rf libm.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libm.so.6 libm.so )
( cd usr/libSFX ; rm -rf libutil.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libutil.so.1 libutil.so )
( cd usr/libSFX ; rm -rf libnss_compat.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libnss_compat.so.2 libnss_compat.so )
( cd usr/libSFX ; rm -rf libthread_db.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libthread_db.so.1 libthread_db.so )
( cd usr/libSFX ; rm -rf libcrypt.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libcrypt.so.1 libcrypt.so )
( cd usr/libSFX ; rm -rf libBrokenLocale.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libBrokenLocale.so.1 libBrokenLocale.so )
( cd usr/libSFX ; rm -rf libdl.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libdl.so.2 libdl.so )
( cd usr/libSFX ; rm -rf libnss_dns.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libnss_dns.so.2 libnss_dns.so )
( cd usr/libSFX ; rm -rf librt.so )
( cd usr/libSFX ; ln -sf ../../libSFX/librt.so.1 librt.so )
( cd usr/libSFX ; rm -rf libanl.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libanl.so.1 libanl.so )
( cd usr/libSFX ; rm -rf libnss_nis.so )
( cd usr/libSFX ; ln -sf ../../libSFX/libnss_nis.so.2 libnss_nis.so )
