#!/bin/sh

#
# (c) Copyright 1992 by Panagiotis Tsirigotis
# All rights reserved.
#

#
# $Id: compile-src,v 5.6 1993/01/06 16:59:22 panos Exp $
#

script_name=`basename $0`

if test "$script_name" != "$0" -a "./$script_name" != "$0" ; then
   echo This script must be executed from the top level directory 
   exit 1
fi

#
# misc needs to precede sio in the list of libraries
#
lib_names="sio misc fsma pset xlog str"

catch_sigs="2 15"
inc=/usr/include

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

		-os)
				if test $# -eq 0 ; then
					echo "Argument missing. Exiting..."
					exit 1
				fi
				osversion=$1 ;
				shift
				;;
		
		-auto)
				autoconfig=yes
				;;

		-sf|-cf)
				if test $# -eq 0 ; then
					echo "Missing argument to $option. Exiting..."
					exit 1
				fi
					
				if test "$option" = "-sf" ; then
					v=yes
				else
					v=no
				fi

				specified_flags=yes
				flag=$1
				shift

				#
				# The first 2 lines are for xinetd. The 2nd line is related to 
				# signal handling.
				# The 3rd line has to do with the man pages (xinetd and libraries)
				# The 4th line is for the libraries
				#
				case "$flag" in
					old_wait|no_rpc|no_termios|no_posix_types)	eval $flag=$v ;;
					no_posix_sigs|no_sigvec|no_siglist)				eval $flag=$v ;;
					has_SB)													eval $flag=$v ;;
					old_dir|no_ftw|no_syslog)							eval $flag=$v ;;
					*)	echo "Bad flag name: $flag. Exiting..." ; exit 1
				esac
				;;

		-verbose)
				verbose=yes
				;;
		#
		# THIS IS THE SCRIPT DOCUMENTATION
		#
		-help)
			echo "Usage: $script_name [options]"
			echo "Options:"
			echo "   -libc: specifies the location of the C library"
			echo "   -os osname: specifies the OS name and version"
			echo "	-auto: automatic configuration"
			echo "	-verbose: display info about configuration"
			echo "Possible arguments to -os:"
			echo "        sunos4, ultrix4"
			echo "   -sf flag or -cf flag:"
			echo "         set or clear flags that specify what facilities"
			echo "         are available from the operating system"
			echo "Possible arguments to -sf and -cf:"
			echo "         old_wait, no_rpc, no_termios, no_posix_types,"
			echo "         no_posix_sigs, no_sigvec, no_siglist, has_SB,"
			echo "         old_dir, no_ftw, no_syslog"
			echo "The -sf/-cf flags can be specified multiple times."
			echo "You can use the -os option to specify an operating system"
			echo "and then you can use -sf/-cf for further mods."
			exit 2
			;;

		*) echo "Bad argument: $1. Exiting..." ; exit 1
	esac
done

if test "$autoconfig" -a "$osversion" ; then
	echo "Only one of -auto, -os can be specified"
	exit 1
fi

if test ! "$osversion" -a ! "$autoconfig" ; then
	echo "I need to know the operating system you are using and the version."
	echo "I know about"
	echo "   SunOS 4.x (sunos4)"
	echo "   Ultrix 4.x (ultrix4)"
	echo "If your OS is among these, use the name in the parentheses, otherwise"
	echo "just hit <return> and I will try to figure out how to properly"
	echo -n "configure xinetd for your system --> "
	read osversion
fi

#
# This if statement checks if we have a known osversion.
# If we don't, we set the autoconfig flag.
#
if test "$osversion" ; then
	case $osversion in
		sunos4|ultrix4) autoconfig= ;;
		*) echo "I don't know anything about this OS. Exiting..." ; exit 1
	esac
else
	autoconfig=yes
fi

#
# Get namelist of C library
#
if test "$autoconfig" ; then
	echo "Obtaining name list of your C library"

	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 give me the pathname of your C library -> "
				read libc
				if test ! "$libc" -o ! -r "$libc" ; then
					echo "Bad pathname. Exiting..."
					exit 1
				fi
			fi
		fi
	fi
	nmfile=/tmp/libc.nm.$$
	nm $libc > $nmfile 2>/dev/null
	if test $? -ne 0 ; then
		echo "Couldn't get the namelist of your C library. Exiting..."
		exit 1
	fi
fi

if test "$specified_flags" -o "$autoconfig" ; then
	lib_defs=
	xinetd_defs=

	#
	# Determine supported signal handing
	#
	case "$no_posix_sigs" in
		yes)	v=-DNO_POSIX_SIGS
				case "$no_sigvec" in
					yes)	w=-DNO_SIGVEC ;;
					no)	w= ;;
					*)		if test "$autoconfig" ; then
								if test "$verbose" ; then echo "Looking for sigvec" ; fi
								found=`grep sigvec $nmfile | grep T`
								if test ! "$found" ; then w="-DNO_SIGVEC" ; else w= ; fi
							else
								w=
							fi
				esac
				v="$v $w"
				;;
		no)	v= ;;
		*)		if test "$autoconfig" ; then
					if test "$verbose" ; then echo "Looking for sigaction" ; fi
					found=`grep sigaction $nmfile | grep T`
					if test "$found" ; then
						v=
					else
						if test "$verbose" ; then echo "Looking for sigvec" ; fi
						found=`grep sigvec $nmfile | grep T`
						if test "$found" ; then 
							v="-DNO_POSIX_SIGS"
						else
							v="-DNO_POSIX_SIGS -DNO_SIGVEC"
						fi
					fi
				else
					v=
				fi
	esac
	xinetd_defs="$xinetd_defs $v"

	#
	# Check for sys_siglist
	#
	case "$no_siglist" in
		yes)	v=-DNO_SIGLIST ;;
		no)	v= ;;
		*)
				if test "$autoconfig" ; then
					if test "$verbose" ; then echo "Looking for sys_siglist" ; fi
					found=`grep sys_siglist $nmfile | grep D`
					if test ! "$found" ; then v=-DNO_SIGLIST ; else v= ; fi
				else
					v=
				fi
	esac
	xinetd_defs="$xinetd_defs $v"

	#
	# Check for RPC
	#
	case "$no_rpc" in
		yes)	v=-DNO_RPC ;;
		no)	v= ;;
		*)
				if test "$autoconfig" ; then
					if test "$verbose" ; then echo "Looking for RPC" ; fi
					found=`grep rpcent $inc/netdb.h`
					if test ! "$found" ; then v=-DNO_RPC ; else v= ; fi
				else
					v=
				fi
	esac
	xinetd_defs="$xinetd_defs $v"

	#
	# Check if this system supports the old wait
	#
	case "$old_wait" in
		yes)	v=-DOLD_WAIT ;;
		no)	v= ;;
		*)
			if test "$autoconfig" ; then
				if test "$verbose" ; then echo "Looking for WEXITSTATUS" ; fi
				found=`grep WEXITSTATUS $inc/sys/wait.h`
				if test ! "$found" ; then v=-DOLD_WAIT ; else v= ; fi
			else
				v=
			fi
	esac
	xinetd_defs="$xinetd_defs $v"

	#
	# Check for pid_t
	# 
	case "$no_posix_types" in
		yes)	v=-DNO_POSIX_TYPES ;;
		no)	v= ;;
		*)
			if test "$autoconfig" ; then
				if test "$verbose" ; then echo "Looking for pid_t" ; fi
				found=`cd $inc/sys ; grep pid_t *.h | grep typedef`
				if test ! "$found" ; then v=-DNO_POSIX_TYPES ; else v= ; fi
			else
				v=
			fi
	esac
	xinetd_defs="$xinetd_defs $v"

	#
	# Check for termios
	#
	case "$no_termios" in
		yes)	v=-DNO_TERMIOS ;;
		no)	v= ;;
		*)
			if test "$autoconfig" ; then
				if test -r $inc/sys/termios.h -o -r $inc/termios.h ; then
					v=
				else
					v=-DNO_TERMIOS
				fi
			else
				v=
			fi
	esac
	xinetd_defs="$xinetd_defs $v"

	#
	# Check for new directory types
	#
	case "$old_dir" in
		yes)	v=-DOLD_DIR ;;
		no)	v= ;;
		*)
				if test "$autoconfig" ; then
					if test -r $inc/dirent.h ; then
						v=
					else
						v=-DOLD_DIR
					fi
				else
					v=
				fi
	esac
	lib_defs="$lib_defs $v"

	#
	# Check for ftw(3)
	#
	case "$no_ftw" in
		yes)	v=-D__FTWX_NO_FTW ;;
		no)	v= ;;
		*)
				if test "$autoconfig" ; then
					if test -r $inc/ftw.h ; then 
						v=
					else
						v=-D__FTWX_NO_FTW
					fi
				else
					v=
				fi
	esac
	lib_defs="$lib_defs $v"

	#
	# Check for syslog(3)
	#
	case "$no_syslog" in
		yes)	v=-DNO_SYSLOG ;;
		no)	v= ;;
		*)
				if test "$autoconfig" ; then
					if test "$verbose" ; then echo "Looking for syslog" ; fi
					found=`grep syslog $nmfile | grep T`
					if test ! "$found" ; then v=-DNO_SYSLOG ; else v= ; fi
				else
					v=
				fi
	esac
	lib_defs="$lib_defs $v"
fi

if test "$autoconfig" ; then
	rm -f $nmfile
fi

#
# Manually supply the link to the right configuration file.
#
rm -f libs/sio/sioconfig.h
case "$osversion" in
	sunos4) ln -s Config/sunos4.1-config.h libs/sio/sioconfig.h ;;
	ultrix4) ln -s Config/ultrix4.1-config.h libs/sio/sioconfig.h ;;
	*)
		ans=
		while [ "$ans" != "sysv" -a "$ans" != "bsd" ] 
		do
			echo "Is your OS based on System V or on BSD 4.x ? I am really"
			echo -n "interested in terminal handling. You can say sysv or bsd -->"
			read ans
			case $ans in
				sysv) ln -s Config/sysv-config.h libs/sio/sioconfig.h ;;
				bsd) ln -s Config/bsd43-config.h libs/sio/sioconfig.h ;;
				*) echo "Please say either 'sysv' or 'bsd'"
			esac
		done
esac

PWD=`pwd`

incdir=$PWD/libs/include
mandir=$PWD/libs/man
libdir=$PWD/libs/lib

#
# Only Suns undestand .SB in man pages, so if we are not dealing with
# a Sun we replace .SB with .B
#
if test "$osversion" = "sunos4" ; then
	modify_manpages=
else
	case "$has_SB" in
		yes) modify_manpages= ;;
		no)  modify_manpages=yes ;;
		*)
		echo -n "Can your machine handle .SB in a man page ? (if you are not sure, say no) -->"
		read x
		if test "$x" = "n" -o "$x" = "no" ; then
			modify_manpages=yes
		else
			modify_manpages=
		fi
	esac
fi

if test "$modify_manpages" ; then
	#
	# We can't take any interrupts while processing the manpages
	#
	trap 'interrupt_occured=yes' $catch_sigs

	echo "Replacing .SB in library man pages with .B (if a man page is changed"
	echo "the original will be kept by appending .orig to its name)"
	for i in $lib_names
	do
		cd $PWD/libs/$i
		for i in *.3
		do
			mv $i $i.orig											# save the original
			sed 's/[.]SB/.B/' $i.orig > $i					# do the replacement
			cmp -s $i $i.orig										# are they the same ?
			if test $? -eq 0 ; then rm $i.orig ; fi		# if yes, remove the copy
		done
		if test "$interrupt_occured" = "yes" ; then
			echo "Interrupt: quiting"
			exit 1
		fi
	done

	trap $catch_sigs		# no more trapping
fi

#
# Make the libraries
#
mvars="DEBUG=-O MANDIR=$mandir INCLUDEDIR=$incdir LIBDIR=$libdir"
for i in $lib_names
do
	cd $PWD/libs/$i
	echo Making library: $i
	make $mvars DEFS="$lib_defs" install
	if test $? -ne 0 ; then
		echo "Failed to create library: $i"
		exit 2
	fi
done

#
# Now make xinetd
# We remove the xinetd executable, if there is one, in case the libraries
# have changed
#
cd $PWD/xinetd
rm -f xinetd
mvars="DEBUG=-O INCLUDEDIR=-I$incdir LDFLAGS=-L$libdir"
make "DEFS=$xinetd_defs" $mvars
if test $? -ne 0 ; then
	echo Failed to create xinetd
	exit 2
fi

if test "$modify_manpages" ; then

	trap 'interrupt_occured=yes' $catch_sigs

	echo "Replacing .SB in xinetd man page with .B"
	i=xinetd.1
	mv $i $i.orig
	sed 's/[.]SB/.B/' $i.orig > $i

	if test "$interrupt_occured" = "yes" ; then
		echo "Interrupt: quiting"
		exit 1
	fi
	trap $catch_sigs		# no more trapping
fi

