#!/bin/sh
#
# makeshlib - installs a shareable libresolv_pic.a into SunOS 4.x libc.so
#
# by Chris Davis <ckd@kei.com> 
# based on code by Piete Brooks <pb@cl.cam.ac.uk>
#              and Dave Morrison <drmorris@mit.edu>
#
# $Id: makeshlib,v 1.4 1994/07/19 22:51:24 vixie Exp $
#
# set these by hand, or as an environment var, or let script figure them out
#bindtree=/usr/obj/local/bind
#SHLIBDIR=/usr/lib/shlib.etc
thisdir=`pwd`

# these should not need changing
junkobjs=gethostent.o

# try to find the BIND tree if it's not set
case "x${bindtree}" in
'x')
	echo 'Trying to find BIND tree...'
	if [ -d "${thisdir}/shres" ]; then
		bindtree=$thisdir
	elif [ -d "${thisdir}/../shres" ]; then
		bindtree="$thisdir/../"
	fi
	;;
esac

case "x${bindtree}" in
'x')
	echo "I can't find the bind tree, and you didn't set \$bindtree."
	echo "Please do so, and try again."
	exit 1
esac

# make sure patch is available
if patch < /dev/null > /dev/null 2>&1
then
	echo "Good, you have patch."
else
	echo "Sorry, you need Larry Wall's 'patch' program for this."
	echo "Get the GNU version from prep.ai.mit.edu, anon ftp."
	exit 1
fi

# cd into the Sun shared library build directory
cd ${SHLIBDIR-/usr/lib/shlib.etc} || exit 1

# Extract and setup the ucb libc
echo 'Setting up the UCB universe libc.so build directory'
if [ ! -d tmp.ucb ]; then
	rm -f tmp.ucb
	mkdir tmp.ucb
fi
cd tmp.ucb || exit 1
rm -rf *
ar x ../libc_pic.a || exit 1
test -f __.SYMDEF && rm __.SYMDEF
for f in *.; do
	mv $f ${f}o
done
rm -f $junkobjs

# Sun's inet_addr.o and possible getnetent.o are clobbered in this
ar x $bindtree/shres/libresolv_pic.a
test -f __.SYMDEF && rm __.SYMDEF

cd ..
# Extract and setup the sysv libc
echo 'Setting up the SysV universe libc.so build directory'
if [ ! -d tmp.s5 ]; then
	rm -f tmp.s5
	mkdir tmp.s5
fi
cd tmp.s5 || exit 1
rm -rf *
ar x ../libcs5_pic.a || exit 1
test -f __.SYMDEF && rm __.SYMDEF
for f in *.; do
	mv $f ${f}o
done
rm -f $junkobjs

# Sun's inet_addr.o and possible getnetent.o are clobbered in this
ar x $bindtree/shres/libresolv_pic.a
test -f __.SYMDEF && rm __.SYMDEF

# Back to the make directory
cd ..

# Add the resolver objects to the lorder sparc listing (uncool hack)
grep gethnamaddr.o lorder-sparc >/dev/null 2>&1 ||
        patch < $bindtree/shres/sun-lorder-sparc.patches &&
        echo 'patched lorder-sparc'

# Fix broken Makefile
# only half needed for 4.1.3 with patch 100890-08+
grep '\./objsort' Makefile >/dev/null 2>&1 ||
        patch < $bindtree/shres/sun-Makefile.patches &&
	echo 'patched shlib Makefile'

# Make the sys5 shared libc
rm -f tmp
ln -s tmp.s5 tmp
time make libcs5.so

# Make the ucb shared libc
rm -f tmp
ln -s tmp.ucb tmp
time make libc.so
