#!/bin/sh
#############################################################################
# File:		configure
#
# Author:	Rhett "Jonzy" Jones <jonzy@cc.utah.edu>
#		With bits and pieces from other peoples configuration schemes.
#
# Date:		June 2, 1994
#
# Modified:	June 9, 1994, by Rhett "Jonzy" Jones.
#		Maes@elec.ucl.ac.be suggested checking for the major
#		release when attempting to determine if we need to
#		define -DSOLARIS2 and -DSVR4, so thats what I did.
#
#		June 11, 1994, by Rhett "Jonzy" Jones.
#		Major implemenatation change to support AIX.
#
#		June 12, 1994, by Rhett "Jonzy" Jones.
#		More changes to support BSD.
#
#		June 15, 1994, by Rhett "Jonzy" Jones.
#		Found I might not be able to use stdarg.h if both stdarg.h
#		and varargs.h exist.  Added code to compile sample code
#		using both includes to determine which include to use.
#		IRIX had to have the includes in a certain order.
#		Thank you sbrown@cs.utah.edu for your assistance.
#
#		June 16, 1994, by Rhett "Jonzy" Jones.
#		IRIX cannot use -lsocket as a link flag, and if the currnet
#		version of IRIX is 4.? the -cckr compiler flag is required.
#		Thank you steve@cs.utah.edu for your assistance.
#
#		June 17, 1994, by Rhett "Jonzy" Jones.
#		Mucho changes to support NeXT versions 2.1 and 3.1
#		Thank you patrick@nightingale.nurs.utah.edu for giving me
#		access to your NeXT machines.
#
#		June 24, 1994, by Rhett "Jonzy" Jones.
#		Added the test to determine if /bin/touch exists and inform
#		the user if it resides in a different location.
#
#		June 28, 1994, by Rhett "Jonzy" Jones.
#		Added support for Linux.
#
#		June 29, 1994, by Rhett "Jonzy" Jones.
#		This file now creates jughead.conf.h from jughead.conf.h.dist
#		with properly configured values for CATCOMMAND, RMCOMMAND,
#		SORTCOMMAND, and TOUCHCOMMAND.  configure now has less
#		things the user must do port jughead.
#
#		July 2, 1994, by Rhett "Jonzy" Jones.
#		When determining the pathway to cat, rm, sort, and touch,
#		the man page is no longer considered an option.  This was
#		added to ensure the man page is not used for sites that
#		have the execution bit set on the man page.  Hmm.
#
#		July 6, 1994, by Rhett "Jonzy" Jones.
#		fixed a bug when configuring for NeXT 3.1 and added support
#		for NeXT 3.2.
#
#		July 22, 1994, by Rhett "Jonzy" Jones.
#		Fixed the the problem of using "echo -n" on machines
#		that do not support the -n flag but support the \c flag.
#		Thank you Alan.Thew@liverpool.ac.uk for letting me know
#		I need to fix the problem.
#
#		August 10, 1994, by Rhett "Jonzy" Jones.
#		A/UX needs to have _POSIX_SOURCE defined so jughead won't
#		dump core using fprintf() by using fputs() instead.
#
#		August 16, 1994, by Rhett "Jonzy" Jones.
#		Commented out all checks for and references to _POSIX_SOURCE
#		which is are longer needed.
#
#		September 15, 1994, by Rhett "Jonzy" Jones.
#		Added the ability for the script to continue if errors
#		were encountered and could not determine which libraries
#		to link to.
#
#		September 16, 1994, by Rhett "Jonzy" Jones.
#		Gave better support for NeXT 3.0
#		Thank you pgr@ramandu.ext.vt.edu
#
#		September 17, 1994, by Rhett "Jonzy" Jones.
#		If the system call "whereis" does not exist then assume
#		cat, rm, sort, and touch reside in /bin.  Implemented for
#		SCO system support.
#
#		September 25, 1994, by Rhett "Jonzy" Jones.
#		Commented out the use of "&& test -x $i" when attempting to
#		locate cat, rm, sort, and touch.
#
#		September 27, 1994, by Rhett "Jonzy" Jones.
#		Removed everything related to "rm".
#
#		November 14, 1994, by Rhett "Jonzy" Jones.
#		Fixed an introduced problem when configuring for the NeXT.
#		Thanks patrick@nightingale.nurs.utah.edu for letting me
#		use your machine, and scottv@amhrt.org and
#		troesch@malahat.library.uvic.ca for bringing the problem
#		to my attention.
#
#		November 15, 1994, by Rhett "Jonzy" Jones with assistance
#		from paul@openage.com.  Gave better support for SCO.
#
#		November 19, 1994, by Rhett "Jonzy" Jones.
#		Some script cleaning up, and even better support for SCO.
#
# Description:	This is an auto configure script to determine what libraries
#		and various compiler flags are needed, to figure out what
#		type of value vsprintf() returns, creates the header file
#		"sprint.h" for use in "utils.h", creates the 
#		"jughead.conf.h file" and the Makefile for jughead.
#		This script also does some consistency checks and informs
#		the user if anything looks out of place.
#		This isn't the prettiest code in the world, but then this
#		code is intended to assist folks with little knowledge in
#		porting jughead to their particular platform.
#
# Notes:	This script has been tested and is known to function
#		correctly on the following platforms `uname -mrsv`:
#			4.3bsd 2.0 B 9000/375
#			AIX 2 3 000007401C00
#			A/UX 3.0 SVR2 mc68040
#			HP-UX A.08.00 B 9000/845
#			HP-UX A.09.00 D 9000/850
#			IRIX 4.0.5 06151813 IP6
#			IRIX 5.1.1.2 11021902 IP22
#			Linux 1.1.0 #1 Tue May 31 15:42:59 MDT 1994 i386
#			openage 2 3.2 i386
#			SunOS 4.1.3 2 sun4
#			SunOS 4.1.3 1 sun4m
#			SunOS 5.1 Generic sun4m
#			ULTRIX 4.4 0 RISC
#		And machines without `uname -mrsv':
#			NeXT 2.1
#			NeXT 3.0
#			NeXT 3.1
#			NeXT 3.2
#
# Copyright:	Copyright 1994, University of Utah Computer Center.
#		This source may be freely distributed as long as this copyright
# 		notice remains intact, and is in no way used for any monetary
# 		gain, by any institution, business, person, or persons.
#############################################################################

# Get the DESTDIR and CC macro from Makefile.dist
DESTDIR=`sed -e "/^DESTDIR/ !d" Makefile.dist | awk '{ print $3 }'`
CC=`sed -e "/^CC/ !d" Makefile.dist | awk '{ print $3 }'`

# These are the files this script creates
sprint_h="sprint.h"			# The header file for use with vsprintf()
Makefile="Makefile"			# The jughead Makefile
jughead_conf_h="jughead.conf.h"		# The jughead.conf.h file

# These variables will change if the platform is a NeXT 3.[12] or Linux
STDARG_H="/usr/include/stdarg.h"	# The standard location for stdarg.h
VARARGS_H="/usr/include/varargs.h"	# The standard location for varargs.h

LINUX_STDARG_H=""				# Does not exist on Linux
LINUX_VARARGS_H="/usr/include/sys/varargs.h"	# Linux location for varargs.h

NEXT_STDARG_H="/usr/include/ansi/stdarg.h"	# NeXT 3.[12] location of stdargs.h
NEXT_VARARGS_H="/usr/include/bsd/varargs.h" 	# NeXT 3.[12] location of varargs.h


# This contains the standard location of includes needed by jughead
INCLUDES="/usr/include/arpa/inet.h /usr/include/ctype.h /usr/include/fcntl.h /usr/include/libc.h /usr/include/netdb.h /usr/include/netinet/in.h /usr/include/pwd.h /usr/include/setjmp.h /usr/include/signal.h /usr/include/stdio.h /usr/include/stdlib.h /usr/include/string.h /usr/include/sys/file.h /usr/include/sys/param.h /usr/include/sys/socket.h /usr/include/sys/socketvar.h /usr/include/sys/stat.h /usr/include/sys/types.h /usr/include/sys/wait.h /usr/include/time.h /usr/include/unistd.h"

# This contains the NeXT 3.1 location of includes needed by jughead
# NOTE: This does contain <arpa/inet.h> because I could not find the
# location of this include.  These includes are used instead of
# INCLUDES only if we're configuring for a NeXT version 3.1 or 3.2
# See "Does your system have uname ... "
ALTINCLUDES="/usr/include/ansi/ctype.h /usr/include/ansi/setjmp.h /usr/include/ansi/signal.h /usr/include/ansi/stdio.h /usr/include/ansi/string.h /usr/include/ansi/time.h /usr/include/bsd/fcntl.h /usr/include/bsd/libc.h /usr/include/bsd/netdb.h /usr/include/bsd/pwd.h /usr/include/bsd/netinet/in.h /usr/include/bsd/sys/file.h /usr/include/bsd/sys/param.h /usr/include/bsd/sys/socket.h /usr/include/bsd/sys/socketvar.h /usr/include/bsd/sys/stat.h /usr/include/bsd/sys/types.h /usr/include/bsd/sys/wait.h"

# This contains locations of possible C compilers
COMPILERS="/bin/cc /usr/bin/cc /usr/ucb/cc /usr/local/bin/gcc /usr/local/gnu/bin/gcc cc gcc /export/home/sparc-sun-solaris2/opt/gnu/bin/gcc"

# This contains locations of the possible libraries jughead may need
THELIBDIRS="/lib /lib/386 /usr/5lib /usr/lib /usr/local/lib /usr/ucblib /usr/ucb/lib"

# Thes contains the possible link flags jughead may need
LFLAGS="-ldir -lgen -linet -lmalloc -lnet -lnsl -lnsl_i -lnsl_s -lsockdns -lsocket -lsun -lx"

DFLAGS=""		# Our initial source defines
CFLAGS=""		# The initial compiler flags
CFLAGS1=-O		# The optimize compiler flag
LFLAGS1=-s
PROG=testing		# Testing program we compile and execute
ERRORS=the.errors	# File containing any errors encountered
OUTPUT=/dev/null	# Where to direct any output
NEXT=""			# The NEXT define flag is on a NeXT
LINUX=""		# The LINUX define flag is on Linux
SCO=""			# The SCO define flag is on SCO
nextVersion=""		# The NeXT system version
lostIncludes=""		# Our initial list of unlocatable includes
CAT=""			# The system cat command for jughead.conf.h
SORT=""			# The system sort command for jughead.conf.h
TOUCH=""		# The system touch command for jughead.conf.h
N=""			# echo uses -n for no line feed?
C=""			# echo uses \c for no line feed?
			# One of /usr/5bin/echo /usr/bin/echo for echo testing?

########################################################
echo "Determining the type of no line-feed flag your echo uses"
echo "just a test" > junk.me
echo -n "line 2"  >> junk.me
echo "line 3"     >> junk.me
if [ `wc -l junk.me | awk '{ print $1 }'` = "2" ]
	then
	echo "looks like echo uses -n"
	N="-n"
else
	echo "just a test" > junk.me
	echo "line 2\c"   >> junk.me
	echo "line 3"     >> junk.me
	if [ `wc -l junk.me | awk '{ print $1 }'` = "2" ]
		then
		echo "looks like echo uses \\c"
		C="\c"
	fi
fi
rm junk.me

if test -f $jughead_conf_h -o -f $Makefile -o -f $sprint_h
	then
	echo \"configure\" will rewrite $jughead_conf_h, $Makefile, and $sprint_h
	echo $N "Do you want me to continue [y] -> $C"
	read CONTINUE
	if [ "$CONTINUE" = "" -o "$CONTINUE" = 'y' ]
		then
		:
	else
		exit
	fi
fi

echo $N "Checking if the default compiler exists ... $C"
if test ! -f $CC
	then
	echo nope
	echo "$CC does not exist. You may need to edit CC in Makefile.config"
	echo "Hang on I'm going to check if another compiler is available"
else
	echo yep
fi

echo $N "Does your system have uname ... $C"
uname >$OUTPUT
if [ $? = 1 ]
        then
        echo $N "Hmm, is this a NeXT? ... $C"
        if test -s /usr/lib/NextStep/software_version
                then
                NEXT="-DNEXT"
                nextVersion=`head -1 /usr/lib/NextStep/software_version`
                echo "yep version $nextVersion"
		if [ "$nextVersion" = "3.0" -o "$nextVersion" = "3.1" -o "$nextVersion" = "3.2" ]
			then
			STDARG_H=$NEXT_STDARG_H
			VARARGS_H=$NEXT_VARARGS_H 
			INCLUDES=$ALTINCLUDES
		fi
        else
                echo "nope"
                echo "I'll continue attempting to configure though"
        fi 
else
	echo cool

	echo $N "Is this some version of BSD ... $C"
	if uname -s | grep bsd >$OUTPUT
	        then
	        echo yep
	        DFLAGS=$DFLAGS" -DBSD"
	else
	        echo nope
	fi
 
	echo $N "Is this some version of IRIX ... $C"
	if uname -s | grep IRIX >$OUTPUT
	        then
	        echo yep
	        irix="yep"
	        if uname -r | grep "^4" >$OUTPUT
	                then
	                echo "   you even need -cckr"
	                DFLAGS=$DFLAGS" -cckr"
	        fi
	else
	        echo nope
	        irix="nope"
	fi

	echo $N "Is this some version of Linux ... $C"
	if uname -s | grep -i Linux >$OUTPUT
	        then
	        echo yep
		LINUX="-DLINUX"
		STDARG_H=LINUX_STDARG_H
		VARARGS_H=LINUX_VARARGS_H 
	else
	        echo nope
	fi

	echo $N "Is this some version of SCO ... $C"
	if [ -d /etc/copyrights ]
		then
		if ls /etc/copyrights | grep -i sco >$OUTPUT
			then
			echo yep
			SCO="-DSCO"
		else
			echo nope
		fi
	else
		echo nope
	fi

	echo $N "Checking if we need -DSOLARIS2 -DSVR4 ... $C"
	if test `uname -s` = "SunOS"
		then
		if uname -r | grep "^5" >$OUTPUT
			then
			echo yep
			DFLAGS=$DFLAGS" -DSOLARIS2 -DSVR4"
		else
			echo nope
		fi
	else
		echo nope
	fi
fi

echo Checking existance of some needed include files
for i in $INCLUDES
	do
	if test ! -f $i
		then
		if [ "$i" = "/usr/include/sys/socketvar.h" ]
			then
			if [ "$LINUX" = "" -o "$SCO" = "" ] ;
				then
				lostIncludes=$lostIncludes" $i"
			fi
		elif [ "$i" = "/usr/include/libc.h" ]
			then
			if [ "$NEXT" = "-DNEXT" ] ;
				then
				lostIncludes=$lostIncludes" $i"
			fi
		elif [ "$i" = "/usr/include/stdlib.h" ]
			then
			if [ "$NEXT" = "" ] ;
				then
				lostIncludes=$lostIncludes" $i"
			fi
                elif [ "$i" = "/usr/include/unistd.h" ]
                        then
                        if [ "$NEXT" = "" ] ;
                                then
                                lostIncludes=$lostIncludes" $i"
                        fi 
		else
			lostIncludes=$lostIncludes" $i"
		fi
	fi
	done
if [ "$lostIncludes" != "" ]
        then
        echo "WARNING: cannot find the following include files:"
        for i in $lostIncludes
                do
                echo "         $i"
                done
fi 

echo $N "Creating a C source file for testing ... $C"
echo "main() { return 0; }" > $PROG.c
echo $PROG.c created

echo $N "Does your compiler look okay ... $C"
cc=""
for a in $COMPILERS
	do
	echo $a $CFLAGS1 $PROG.c -o $PROG $LFLAGS1 >>$ERRORS
	test -z "$cc" -a ! -z "$a" &&
	($a $CFLAGS1 $PROG.c -o $PROG $LFLAGS1) >> $ERRORS 2>&1 && cc="$a"
	echo "::::" >>$ERRORS
	done
if test -z "$cc"
	then
	echo nope
	echo "I'm bailing out - You might want to look at $ERRORS"
	echo If you know you have a C compiler you can edit \"COMPILERS\" in configure
	echo "and please let jonzy@cc.utah.edu know the pathway to your C compiler."
	exit 1
fi
/bin/rm -f $ERRORS $PROG.o $PROG
echo "yep using $cc"

echo $N "Checking if we need to include malloc.h ... $C"
cat >$PROG.c <<TESTSOURCE
#include <malloc.h>
main()
{ char *c; c = (char *)malloc(5); }
TESTSOURCE
if $cc -c $CFLAGS1 $PROG.c >$OUTPUT 2>&1
        then
        echo yep
        DFLAGS=$DFLAGS" -DNEED_MALLOC_H"
else
        echo nope
fi

echo $N "Creating another C source file for more testing ... $C"
cat >$PROG.c <<TESTSOURCE
#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
main()
{ struct stat buf; return !&buf; }
TESTSOURCE
echo $PROG.c rewritten

#### Commented out - may be needed in the future. ###
#echo $N "Checking if we need -D_POSIX_SOURCE ... $C"
#if $cc -c $CFLAGS1 $PROG.c >$OUTPUT 2>&1
#	then
#	if uname -s | grep A/UX >$OUTPUT
#	        then
#	        echo yep
#	        DFLAGS=$DFLAGS" -D_POSIX_SOURCE"
#	else if uname -mrsv | grep i386 >$OUTPUT
#		then
#		echo yep
#		DFLAGS=$DFLAGS" -D_POSIX_SOURCE"
#	else
#	        echo nope
#	fi
#else
#	echo yep
#	/bin/rm $PROG.o
#	$cc -c $CFLAGS1 -D_POSIX_SOURCE $PROG.c >$OUTPUT 2>&1 &&
#	DFLAGS=$DFLAGS" -D_POSIX_SOURCE"
#fi

echo $N "Does fgrep work correctly? ... $C"
FGREP="fgrep"
if test \^jughead = "`echo 'jughead' | grep -F '^jughead' 2>&1`"
	then
	FGREP="grep -F"
fi
echo jughead | $FGREP jughead >$OUTPUT
result1=$?
echo juhgead | $FGREP jonzy   >$OUTPUT
result2=$?
if test $result1 != 0 -o $result2 = 0
	then
	echo nope
	echo \"$FGREP\" does not return a proper value.
	echo "please let jonzy@cc.utah.edu know about this problem"
	echo "There is nothing more I can do so I\`m quiting."
	exit 1
fi
echo yep

echo $N "Should we use the -lc link flag ... $C"
test -f $PROG.o || $cc $DFLAGS $CFLAGS $CFLAGS1 $PROG.c >$OUTPUT 2>&1
$cc $DFLAGS $CFLAGS $CFLAGS1 $PROG.o -o $PROG $LFLAGS1 -lc >$ERRORS 2>&1
if test -s $ERRORS
	then
	echo nope
#	echo "main(){}" > testing.c
	$cc $PROG.c -o $PROG.o
else
	echo yep
	LFLAGSC=" -lc"
fi

echo $N "Checking which libraries to use ... $C"
firstPass=yes
while /bin/rm -f $PROG
$cc $DFLAGS $CFLAGS $CFLAGS1 $PROG.o -o $PROG $LFLAGS $LFLAGSC $LFLAGS1 >$ERRORS 2>&1
test $firstPass = yes -o ! -f $PROG
do
	firstPass=no
	set dummy $LFLAGS
	shift
	NEWLFLAGS=""
	for a in $LFLAGS dummy
	do
	if [ "$irix" = "yep" -a "$a" = "-lsocket" ] ; then
		echo phew
		echo $N "  With IRIX do not use -lsocket $C" 
	elif test dummy != $a
		then
		lib=`expr $a : '-l\(.*\)'`
		if $FGREP lib$lib $ERRORS >$OUTPUT ||
		$FGREP -e $a $ERRORS >$OUTPUT ||
		grep "[\"']$lib['\"]" $ERRORS >$OUTPUT
			then
			:
		else
			OLDIFS="$IFS"; IFS=":$IFS"
			found=no
			for libpath in $THELIBDIRS $LD_LIBRARY_PATH
				do
				set $libpath/*lib$lib[A-Z.]*
				test -f $1 && found=yes
				done
			IFS="$OLDIFS"
			test yes = $found && NEWLFLAGS="$NEWLFLAGS $a"
		fi
	fi
	done
	if test a"$LFLAGS" = a"$NEWLFLAGS"
		then
		if [ "$SCO" = "-DSCO" ]
			then
			echo "hmm, SCO aye"
			LFLAGS="-lmalloc -lsocket -lx -lc"
		else
			echo "Yipes!"
			echo
			echo "I could not determine which libraries you need"
			echo "You can abort now to look at the file $ERRORS"
			echo $N "Would you like me to continue [y] -> $C"
			read CONTINUE
			if [ "$CONTINUE" = "" -o "$CONTINUE" = 'y' ]
				then
				break
			else
				exit
			fi
		fi
	fi
	LFLAGS="$NEWLFLAGS"
done
if test -s $ERRORS
        then
        echo "Errors have been encountered, but I'll see what I can do"
	LFLAGS=""
else
	echo "got'em"
fi

/bin/rm -f $PROG.o $PROG.c $PROG
LFLAGS="$LFLAGS$LFLAGSC"
CC=$cc
echo "Here are the needed compiler flags I found:"
echo "CC	=$CC"
echo "DFLAGS	=$DFLAGS $NEXT $LINUX $SCO"
echo "CFLAGS	=$CFLAGS"
echo "LFLAGS	=$LFLAGS"

echo $N "Generating the source file for testing ... $C"
cat >$PROG.c << TESTSOURCE
#include <stdio.h>
#include <ctype.h>
#ifdef STDARG
# 	include <stdarg.h>
#else
# 	include <varargs.h>
# 	define USE_VARARGS_H
#endif 
#ifdef USE_VARARGS_H
 char *Mysprint(va_alist)
	va_dcl
{
	char		*format;	/* Format of the arguments. */
#else /* We are using <stdarg.h>. */
 char *Mysprint(char *format,...)
{
#endif /* Using <stdarg.h>. */
	static char	str[1024];	/* The string we send to the system. */
#ifdef VSPRINTF_RETURNS_STR
	char		*result;	/* The result of calling vsprintf. */
#else /* vsprintf() returns an int. */
	int		result;		/* The result of calling vsprintf. */
#endif /* vsprintf() returns an int. */
	va_list		ap;		/* The arguments beyond 'format'. */

#ifdef USE_VARARGS_H
	va_start(ap);
	format = va_arg(ap,char *);
#else  /* We are using <stdarg.h>. */
	va_start(ap,format);
#endif  /* Using <stdarg.h>. */

	result = vsprintf(str,format,ap);
	va_end(ap);

	(void)fprintf(stdout,"Checking if vsprintf() returns ");

#ifdef VSPRINTF_RETURNS_STR
	(void)fprintf(stdout,"char * ...\n");
	if (strncmp(str,(char *)result,strlen(str)))
		{
		(void)fprintf(stdout," ... vsprintf() failed returning a (char *).\n");
		exit(-1);
		}
#else /* vsprintf() returns an int. */
	(void)fprintf(stdout,"an int ...\n");
	if (result < 0)
		{
		(void)fprintf(stdout," ... vsprintf failed returning an (int).\n");
		exit(-2);
		}
#endif /* vsprintf() returns an int. */

	return(str);
}	/* Mysprint */
main(argc,argv)
	int	argc;
	char	*argv[];
{
	if (Mysprint("Testing %s and %s\n","int","string"))
#ifdef VSPRINTF_RETURNS_STR
		(void)fprintf(stdout,"vsprintf() looks like it returns a char *\n");
#else
		(void)fprintf(stdout,"vsprintf() looks like it returns an int\n");
#endif
	return(0);

}	/* main */
TESTSOURCE
echo $PROG.c created

echo $N "Determining wheather to use stdarg.h or varargs.h ... $C"
if test -f $STDARG_H
	then
	if test -f $VARARGS_H
		then
		echo found both
		$CC -DSTDARG $CFLAGS -o $PROG $PROG.c $LFLAGS 1>$ERRORS 2>&1
		if test -s $ERRORS
			then
			$CC $CFLAGS -o $PROG $PROG.c $LFLAGS 1>$ERRORS 2>&1
			if test -s $ERRORS
				then
				echo "   using stdarg.h"
				include2use=stdarg.h
			else
				echo "   using varargs.h"
				include2use=varargs.h
			fi
		else
			echo "   using stdarg.h"
			include2use=stdarg.h
		fi 
	else
		echo using stdarg.h
		include2use=stdarg.h
	fi
else
	if test -f /usr/include/varargs.h
		then
		echo "Hmm non-ANSI C huh"
		echo "   stdarg.h not found but I did find varargs.h"
		include2use=varargs.h
	else
		echo Yipes
		echo Neither stdarg.h or varargs.h found aborting
		exit 1
	fi
fi

echo $N "Generating the include file ... $C"
echo "/*****************************************************************************"	 >  $sprint_h
echo " * File:	$sprint_h"								 >> $sprint_h
echo " *"										 >> $sprint_h
echo " * Author:	Rhett "Jonzy" Jones"						 >> $sprint_h
echo " *		jonzy@cc.utah.edu"						 >> $sprint_h
echo " *"										 >> $sprint_h
echo " * Date:	`date`"									 >> $sprint_h
echo " *"										 >> $sprint_h
echo " * Modifed:	Everytime you run make config."					 >> $sprint_h
echo " *"										 >> $sprint_h
echo " * Description:	Header file to use with utils.c.  This file is automaticly"	 >> $sprint_h
echo " *		generated with make config and is used so we include the"	 >> $sprint_h
echo " *		correct header file.  The reason for this header file is"	 >> $sprint_h
echo " *		on some systems vsprintf() returns an int and on others"	 >> $sprint_h
echo " *		vsprintf() returns a char *.  This is a kludge but it works."	 >> $sprint_h
echo " *"										 >> $sprint_h
echo " * Bugs:	No known bugs."								 >> $sprint_h
echo " *"										 >> $sprint_h
echo " * Copyright:	Copyright 1993, 1994, University of Utah Computer Center."	 >> $sprint_h
echo " *		This source may be freely distributed as long as this copyright" >> $sprint_h
echo " * 		notice remains intact, and is in no way used for any monetary"	 >> $sprint_h
echo " * 		gain, by any institution, business, person, or persons."	 >> $sprint_h
echo " *"										 >> $sprint_h
echo " ****************************************************************************/"	 >> $sprint_h
echo ""											 >> $sprint_h
if [ "$include2use" = "varargs.h" ] ; then
	echo "#if(1)	/* 1 if vsprintf() needs varargs.h, 0 otherwise. */"		 >> $sprint_h
	echo "#	include <varargs.h>"							 >> $sprint_h
	echo "#	define USE_VARARGS_H"							 >> $sprint_h
	echo "#else"									 >> $sprint_h
	echo "#	include <stdarg.h>"							 >> $sprint_h
	echo "#endif"									 >> $sprint_h
	iflag=""
else
	echo "#if(0)	/* 1 if vsprintf() needs varargs.h, 0 otherwise. */"		 >> $sprint_h
	echo "#	include <varargs.h>"							 >> $sprint_h
	echo "#	define USE_VARARGS_H"							 >> $sprint_h
	echo "#else"									 >> $sprint_h
	echo "#	include <stdarg.h>"							 >> $sprint_h
	echo "#endif"									 >> $sprint_h
	iflag="-DSTDARG"
fi
echo ""											 >> $sprint_h
echo $sprint_h created

echo "Attempting to determine what value vsprintf() returns."
$CC $iflag -o testint $PROG.c # 1>$ERRORS 2>&1
if test -s $ERRORS
	then
	echo "Does not return an int lets try a char *"
	$CC $iflag -DVSPRINTF_RETURNS_STR -o testchar $PROG.c 1>$ERRORS 2>&1
	if test -s $ERRORS
		then
		echo "Does not return a char * either you may want to look at $ERRORS"
		returnType=int
	else
		returnType=char
	fi
	testchar
else
	returnType=int
fi
testint
echo "return type I am using for vsprintf() is $returnType"
echo If this causes problems see the \"#define VSPRINTF_RETURNS_STR\" line in $sprint_h
if [ "$returnType" = "char" ] ; then
	echo "#define VSPRINTF_RETURNS_STR  /* Uncommented if vsprintf() returns a char * */"	>> $sprint_h
else
	echo "/*#define VSPRINTF_RETURNS_STR  /* Uncommented if vsprintf() returns a char * */"	>> $sprint_h
fi
echo "" >> $sprint_h
echo $sprint_h contains which include to use and proper use of \"VSPRINTF_RETURNS_STR\"

echo $N "Checking if you can do \"make install\" ... $C"
if test -f /etc/group -a -r /etc/group
	then
	if grep wheel /etc/group >$OUTPUT	# BSD, SunOS, standard unix
		then
		grp=wheel
	elif grep system /etc/group >$OUTPUT	# AIX
		then
		grp=system
	elif grep sys /etc/group >$OUTPUT	# IRIX
		then
		grp=sys
	else
		echo $N "no wheel, sys, or system? $C"
		grp=bloink
	fi 
	if grep $grp /etc/group | grep `whoami` >$OUTPUT 2>&1
		then
		echo looks like you have rootability
		rootAccess=1
	else
		echo nope
		echo Does not look like you have root access
		rootAccess=0
	fi 
else
	echo I cannot determine if you have root access
	rootAccess=0
fi

if test ! -d $DESTDIR
	then
	echo The destination directory $DESTDIR does not exist
	rootAccess=0
fi

if [ "$rootAccess" = "0" ] ; then
	echo You will need to edit DESTDIR in Makefile to either
	echo install jughead or run jughead as a search engine
else
	echo Cool - you can even do a \"make install\" as root
fi

echo $N "Doing some cleaning up ... $C"
rm -f core testint testchar a.out $PROG.c $PROG.o $PROG $ERRORS
echo done

echo $N "Creating $Makefile ... $C"
sed -e "/^# CONFIGURE - stop/,$ d" Makefile.dist						>  $Makefile
echo "CC	= $CC"										>> $Makefile
echo "DFLAGS	= $DFLAGS $NEXT $LINUX $SCO"							>> $Makefile
echo "CFLAGS	= \$(JFLAGS) \$(DFLAGS) \$(NOWARNINGS) \$(OPTIMIZATION) \$(PROTOTYPES) $CFLAGS"	>> $Makefile
echo "LFLAGS	= $LFLAGS"									>> $Makefile
sed -e "/^# CONFIGURE - stop/,$ !d" Makefile.dist						>> $Makefile
echo $Makefile created

echo $N "Do you have which and whereis ... $C"
which whereis >$OUTPUT 2>&1
if [ $? = 1 ]
	then
	echo nope
	echo "I'm guessing cat, sort, and touch reside in /bin"
	CAT=/bin/cat
	SORT=/bin/sort
	TOUCH=/bin/touch
else
	echo yep
	echo $N "Okay where is cat, sort, and touch ... $C"
	for i in `whereis cat`
		do
		if echo $i | grep "/" | grep -v "[.]" >$OUTPUT # && test -x $i
			then
			CAT=$i
			fi
		done
	for i in `whereis sort`
		do
		if echo $i | grep "/" | grep -v "[.]" >$OUTPUT # && test -x $i
			then
			SORT=$i
			fi
		done
	for i in `whereis touch`
		do
		if echo $i | grep "/" | grep -v "[.]" >$OUTPUT # && test -x $i
			then
			TOUCH=$i
			fi
		done
	echo "found them"
fi

echo $N "Creating $jughead_conf_h ... $C"
sed -e "/^[/][*] CONFIGURE - stop/,$ d" jughead.conf.h.dist				>  $jughead_conf_h
echo "/* CONFIGURE - stop,  don't (re)move this line, configure needs it. */"		>> $jughead_conf_h
echo "#define CATCOMMAND	\"$CAT %s >> %s\""					>> $jughead_conf_h
echo "#define SORTCOMMAND	\"$SORT -d -f -u -o %s -t'	' -T /tmp +1.0 %s\""	>> $jughead_conf_h
echo "#define TOUCHCOMMAND	\"$TOUCH -c %s\""					>> $jughead_conf_h
echo $jughead_conf_h created
echo
echo Quickly checking some default values in $jughead_conf_h
if hostname | grep "[.]" >$OUTPUT
        then
        :
else
        echo "WARNING: HOSTDOMAIN  in $jughead_conf_h may be wrong"
fi
if test -d /tmp -a -w /tmp
        then
        :
else
        echo "WARNING: TMPFILENAME in $jughead_conf_h needs to use something other than /tmp" 
fi

########################################################
echo
echo Here are the particulars for \"$jughead_conf_h\":
echo "cat   = $CAT"
echo "sort  = $SORT"
echo "touch = $TOUCH"
echo Note: All defines in \"$jughead_conf_h\" can be overriden via \"jughead.conf\".

echo
echo Here are the particulars for \"$Makefile\":
echo "CC=$CC"
echo "DFLAGS=$DFLAGS $NEXT $LINUX"
echo "CFLAGS=\$(JFLAGS) \$(DFLAGS) \$(NOWARNINGS) \$(OPTIMIZATION) \$(PROTOTYPES) $CFLAGS"
echo "LFLAGS=$LFLAGS"
echo

echo Here are the particulars for \"$sprint_h\":
echo "#include <$include2use>"
if [ "$returnType" = "int" ] ; then
	echo "vsprintf() returns an $returnType"
else
	echo "vsprintf() return a $returnType *"
fi
echo

echo You can abort now to edit \"jughead.conf.h\", \"$Makefile\", or \"$sprint_h\".
echo Don\'t forget to look at the \"veronica.ctl\" file.
echo $N "Would you like me to continue and run \"make\" [y] -> $C"
read CONTINUE
if [ "$CONTINUE" = "" -o "$CONTINUE" = 'y' ] ; then
	make
else
	exit 1
fi

if [ $? = 1 ]
	then
	echo Hmm, it looks like make had problems.  Try \"make\" now.
	echo
fi
