#!/bin/sh

#
# Copyright (c) 1993 by Panagiotis Tsirigotis
#

#
# $Id: libconf,v 1.1 93/11/26 10:42:45 panos Exp $
#

#
# XXX:	a lot of the autoconfiguration stuff would be much less verbose
#			if we could assume that /bin/sh supports functions. In a future
#			version we may choose to make that assumption, or assume that
#			there is also some other shell available (for example, ksh)
#			that supports functions.
#

script_name=`basename $0`
usage="Usage: $script_name [-libc pathname] [-v]"

#
# The $echo variable should have the pathname for an 'echo' command
# that understands the -n option.
#
echo=echo

inc=/usr/include
nm_defined_var=D
nm_defined_func=T

#
# Get the options, if any
#
while test $# -gt 0
do
	case "$1" in
		-*) option=$1 ; shift ;;
		*) echo $usage ; exit 1
	esac

	case "$option" in
		-libc)
            if test $# -eq 0 ; then
               $echo "$script_name: Argument missing. Exiting..."
               exit 1
            fi
            libc="$1"
            shift
            if test ! -r "$libc" ; then
               $echo "$script_name: File is not readable: $libc"
               exit 1
            fi
            ;;

		-v)	verbose=yes
				;;

		-*)	echo $usage ; exit 1
	esac
done


#######################################################################
# Get namelist of C library
#######################################################################
$echo "Obtaining name list of C library for autoconfiguration"

if test "$libc" = "" ; then
	if test -r /lib/libc.a ; then
		libc=/lib/libc.a
	else
		if test -r /usr/lib/libc.a ; then
			libc=/usr/lib/libc.a
		else
			$echo -n "Please enter the pathname of the C library -> "
			read libc
			if test "$libc" = "" -o ! -r "$libc" ; then
				$echo "$script_name: bad pathname. Exiting..."
				exit 1
			fi
		fi
	fi
fi
nmfile=/tmp/libc.nm.$$
nm $libc > $nmfile 2>/dev/null
if test $? -ne 0 ; then
	if test ! -s $nmfile ; then
		$echo "Couldn't get the namelist of C library. Exiting..."
		rm -f $nmfile
		exit 1
	else     # name list file is not empty
		$echo "WARNING: The 'nm' command returned a non-zero exit status"
		$echo "WARNING: so the namelist obtained from the C library may be inco
mplete"
		$echo "Continuing with determination of configuration flags"
	fi
fi


#######################################################################
#
# TIMER library configuration.
#
#######################################################################

#
# Determine supported signal handing
#
lookfor=sigaction
if test "$verbose" ; then $echo "Looking for $lookfor" ; fi
found=`grep $lookfor $nmfile | egrep "$nm_defined_func"`
if test "$found" ; then
	signal_handling=posix
else
	lookfor=sigvec
	if test "$verbose" ; then $echo "Looking for $lookfor" ; fi
	found=`grep $lookfor $nmfile | egrep "$nm_defined_func"`
	if test "$found" ; then
		signal_handling=bsd
	else
		signal_handling=simple
	fi
fi


#
# Check for setitimer(2)
#
lookfor=setitimer
if test "$verbose" ; then $echo "Looking for $lookfor" ; fi
found=`grep $lookfor $nmfile | egrep "$nm_defined_func"`
if test "$found" ; then
	case "$signal_handling" in
		posix)	timer_defs= ;;
		bsd)		timer_defs=-DNO_POSIX_SIGS ;;
		simple)	no_timers=yes
	esac
else
	no_timers=yes
fi


#######################################################################
#
# XLOG library configuration.
#
#######################################################################

#
# Check for syslog(3)
#
lookfor=syslog
if test "$verbose" ; then $echo "Looking for $lookfor" ; fi
found=`grep $lookfor $nmfile | egrep "$nm_defined_func"`
if test "$found" = "" ; then v=-DNO_SYSLOG ; else v= ; fi
xlog_defs="$xlog_defs $v"

#
# Look for syslog.h
#
v=
if test -r $inc/sys/syslog.h ; then
	v=-DSYSLOG_IN_SYS
	if test -r $inc/syslog.h ; then
		cmp -s $inc/syslog.h $inc/sys/syslog.h
		if test $? -eq 0 ; then v= ; fi
	fi
fi
xlog_defs="$xlog_defs $v"


#######################################################################
#
# MISC library configuration.
#
#######################################################################

#
# Check for new directory types
#
if test -r $inc/dirent.h ; then v= ; else v=-DOLD_DIR ; fi
misc_defs="$misc_defs $v"

#
# Check for ftw(3)
#
if test -r $inc/ftw.h ; then v= ; else v=-D__FTWX_NO_FTW ; fi
misc_defs="$misc_defs $v"


#######################################################################
#
# SIO library configuration.
#
# Note that we do not try to find out if the system actually supports
# a generic mmap system call (i.e. an mmap capable of mapping regular
# files).
#
#######################################################################

lookfor=on_exit
if test "$verbose" ; then $echo "Looking for $lookfor" ; fi
found=`grep $lookfor $nmfile | egrep "$nm_defined_func"`
if test "$found" ; then v=-DHAS_ONEXIT ; else v= ; fi
sio_defs="$sio_defs $v"

lookfor=atexit
if test "$verbose" ; then $echo "Looking for $lookfor" ; fi
found=`grep $lookfor $nmfile | egrep "$nm_defined_func"`
if test "$found" ; then v=-DHAS_ATEXIT ; else v= ; fi
sio_defs="$sio_defs $v"

lookfor=memcpy
if test "$verbose" ; then $echo "Looking for $lookfor" ; fi
found=`grep $lookfor $nmfile | egrep "$nm_defined_func"`
if test "$found" ; then v=-DHAS_MEMOPS ; else v= ; fi
sio_defs="$sio_defs $v"

lookfor=bcopy
if test "$verbose" ; then $echo "Looking for $lookfor" ; fi
found=`grep $lookfor $nmfile | egrep "$nm_defined_func"`
if test "$found" ; then v=-DHAS_BCOPY ; else v= ; fi
sio_defs="$sio_defs $v"

lookfor=isatty
if test "$verbose" ; then $echo "Looking for $lookfor" ; fi
found=`grep $lookfor $nmfile | egrep "$nm_defined_func"`
if test "$found" ; then has_isatty=-DHAS_ISATTY ; else has_isatty= ; fi
sio_defs="$sio_defs $has_isatty"

rm -f $nmfile

#
# Inform the user about our findings.
#
sio_defs=`echo $sio_defs`
if test "$sio_defs" ; then
	echo
	$echo "For the SIO library, set the following preprocessor flags:"
	$echo "$sio_defs"
	if test "$has_isatty" = "" ; then
		echo "Your system does not have the isatty(3) function which is"
		echo "required by the SIO library. A replacement can be provided"
		echo "by determining if your system supports BSD or System V tty"
		echo "handling. In the former case, use the flag -DHAS_BSDTTY, and"
		echo "in the latter, use the flag -DHAS_SYSVTTY"
	fi
fi

xlog_defs=`echo $xlog_defs`
echo
if test "$xlog_defs" ; then
	$echo "For the XLOG library, set the following preprocessor flags:"
	$echo "$xlog_defs"
else
	echo "No flags need to be set for the XLOG library"
fi

misc_defs=`echo $misc_defs`
echo
if test "$misc_defs" ; then
	$echo "For the MISC library, set the following preprocessor flags:"
	$echo "$misc_defs"
else
	echo "No flags need to be set for the MISC library"
fi

if test "$no_times" = "" ; then
	timer_defs=`echo $timer_defs`
	echo
	if test "$timer_defs" ; then
		$echo "For the TIMER library, set the following preprocessor flags:"
		$echo "$timer_defs"
	else
		echo "No flags need to be set for the TIMER library"
	fi
else
	echo "Your system does not support the necessary facilities needed"
	echo "by the timer library, so that library cannot be compiled"
fi
