#!/bin/sh
# Copyright (C) 1995, 1996 by Sven Berkvens (sven@stack.nl)
#
# This script attemps to guess as many of the config.h settings as possible.
# This is handy, but not always entirely successful. I still need to
# experiment with GNU's autoconf someday, but I haven't found the time yet.

# You might need to edit some "defines" below:

includedir="/usr/include"
tmpfile="/tmp/autodetect.$$.c"
cc="`awk '/^CC.*=.*$/ {print $3}' < Makefile`"

# Check if echo understand -n (only HP/UX doesn't seem to I far as I've heard)

if echo -n 'Testing echo... ' 2>/dev/null
then
	echo 'Done (-n is accepted)'
	echoarg="-n"
else
	echo "Echo does not support the -n flag (don't worry)"
	echoarg=""
fi

# Delete the temporary file

rm -fr "$tmpfile"

# Make a function that will return 0 if the temporary file can
# be compiled. If not, return 1.

checkcompile() {
	if "$cc" "$tmpfile" -o "$tmpfile".out > /dev/null 2>&1
	then
		rm -f "$tmpfile"
		echo "Yes!" >&2
		return 0
	else
		rm -f "$tmpfile"
		echo "Nope..." >&2
		return 1
	fi
}

# Print the defines

echo "The main include directory is: $includedir"
echo "The compiler that will be used is: $cc"

# Begin of script

rm -fr "$tmpfile"

if [ ! -f config.h.generic ]
then
	echo No config.h.generic found! >&2
	exit 1
fi

if [ -f config.h ]
then
	echo A config.h already exists. Renaming it to config.h.old. >&2
	mv config.h config.h.old || { echo Rename failed! >&2 ; exit 1 ; }
fi

echo Building config.h... >&2

while read define symbol rest
do
	if [ "$define" = "/*" -a "$symbol" = "No" ]
	then
		echo "$define $symbol $rest"
		cat
		continue
	fi
	if [ "$rest" != "" ]
	then
		case "$symbol" in
			PATH_*)
				prog="`echo "$symbol" | sed -e 's,^PATH_,,' | tr '[A-Z]_' '[a-z]/'`"
				found=0
				echo $echoarg "Searching for $prog... " >&2
				for p in `echo $PATH | tr ':' ' '`
				do
					if [ -x "$p"/"$prog" ]
					then
						echo "Found it in $p" >&2
						rest="\"$p/$prog\""
						found=1
						break
					fi
				done
				if [ "$found" = 0 ]
				then
					echo "Could not find it!" >&2
					define="#undef"
					rest=""
				fi
				;;
		esac
		echo "$define $symbol $rest"
		continue
	fi
	case "$define"
	in
		\#define | \#undef)
			case "$symbol"
			in
				HAVE_*_H)
					hfile="`echo "$symbol" | sed -e 's,^HAVE_,,' -e 's,_H$,,' | \
						tr '[A-Z]_' '[a-z]/'`".h
					if [ "$hfile" = strings.h -a -f "${includedir}/string.h" -a -f "${includedir}/strings.h" ]
					then
						echo $echoarg 'Checking whether you can use string.h and strings.h together... ' >&2
						echo '#include <string.h>' > "$tmpfile"
						echo '#include <strings.h>' >> "$tmpfile"
						echo 'main() { }' >> "$tmpfile"
						if checkcompile
						then
							echo '#define HAVE_STRINGS_H'
							echo 'Using string.h and strings.h (they go together)' >&2
							continue
						else
							echo '#undef HAVE_STRINGS_H'
							echo 'Using string.h instead of strings.h (you have both)' >&2
							continue
						fi
					fi
					echo $echoarg Checking for "$hfile"...' ' >&2
					if [ -f "${includedir}/${hfile}" ]
					then
						echo "#define $symbol $rest"
						echo Found >&2
					else
						echo "#undef $symbol $rest"
						echo Not found >&2
					fi
					;;

				NEED_SYS_ERRLIST_DECL)
					echo $echoarg "Checking if you can live without declaring sys_nerr... " >&2
					cat > "$tmpfile" << EOF
#include <errno.h>
#include <stdio.h>
main(){int temp = sys_nerr;}
EOF
					if checkcompile
					then
						echo "#undef $symbol $rest"
					else
						echo "#define $symbol $rest"
					fi
					;;

				NEED_DECL_ENVIRON)
					echo $echoarg "Checking if you can live without declaring environ... " >&2
					cat > "$tmpfile" << EOF
#include <unistd.h>
#include <stdio.h>
main(){char **temp = environ;}
EOF
					if checkcompile
					then
						echo "#undef $symbol $rest"
					else
						echo "#define $symbol $rest"
					fi
					;;

				NEED_OPTARG_AND_OPTIND)
					echo $echoarg "Checking if you can live without declaring optarg and optind... " >&2
					echo '#include <stdlib.h>' > "$tmpfile"
					if [ -f "$includedir"/getopt.h ]
					then
						echo '#include <getopt.h>' >> "$tmpfile"
					fi
					echo 'main() { int a = optind; }' >> "$tmpfile"
					if checkcompile
					then
						echo "#undef $symbol $rest"
					else
						echo "#define $symbol $rest"
					fi
					;;

				HAVE_*)
					func="`echo "$symbol" | sed -e 's,^HAVE_,,' | tr '[A-Z]' '[a-z]'`"
					echo $echoarg Checking if you have function "$func"'()... ' >&2
					echo 'main(){ '"$func"' (); }' > "$tmpfile"
					if checkcompile
					then
						echo "#define $symbol $rest"
					else
						echo "#undef $symbol $rest"
					fi
					;;

				NO*_T)
					name="`echo "$symbol" | sed -e 's,^NO,,' | tr '[A-Z]' '[a-z]'`"
					echo $echoarg Checking if you have type "$name"'... ' >&2
					echo '#include	<sys/types.h>' > "$tmpfile"
					echo '#include	<sys/stat.h>' >> "$tmpfile"
					echo "$name" 'temp; int main(){}' >> "$tmpfile"
					if checkcompile
					then
						echo "#undef $symbol $rest"
					else
						echo "#define $symbol $rest"
					fi
					;;

				NOPROTOS)
					echo $echoarg Checking if you can handle prototypes...' ' >&2
					echo "int temp(int); int main(){}" > "$tmpfile"
					if checkcompile
					then
						echo "#undef NOPROTOS $rest"
					else
						echo "#define NOPROTOS $rest"
					fi
					;;

				NOFORWARDS)
					echo $echoarg Checking if you can handle forwards...' ' >&2
					echo "int temp(); int main(){}" > "$tmpfile"
					if checkcompile
					then
						echo "#undef NOFORWARDS $rest"
					else
						echo "#define NOFORWARDS $rest"
					fi
					;;

				NONEWSTYLE)
					echo $echoarg Checking if you can handle new style decls...' ' >&2
					echo 'int main(int argc, char *argv[]){}' > "$tmpfile"
					if checkcompile
					then
						echo "#undef NONEWSTYLE $rest"
					else
						echo "#define NONEWSTYLE $rest"
					fi
					;;

				NOSTATIC)
					echo $echoarg Checking if you have static...' ' >&2
					echo 'static int temp; int main(){}' > "$tmpfile"
					if checkcompile
					then
						echo "#undef NOSTATIC $rest"
					else
						echo "#define NOSTATIC $rest"
					fi
					;;

				NOCONST)
					echo $echoarg Checking if you have const...' ' >&2
					echo 'const int temp; int main(){}' > "$tmpfile"
					if checkcompile
					then
						echo "#undef NOCONST $rest"
					else
						echo "#define NOCONST $rest"
					fi
					;;

				NOEXTERN)
					echo $echoarg Checking if you have extern...' ' >&2
					echo 'extern int printf(); int main(){}' > "$tmpfile"
					if checkcompile
					then
						echo "#undef NOEXTERN $rest"
					else
						echo "#define NOEXTERN $rest"
					fi
					;;

				NOVOID)
					echo $echoarg Checking if you have void...' ' >&2
					echo 'void main(){}' > "$tmpfile"
					if checkcompile
					then
						echo "#undef NOVOID $rest"
					else
						echo "#define NOVOID $rest"
					fi
					;;

				SYS_TIME_WITH_TIME)
					echo $echoarg Checking if you can handle time.h and sys/time.h together...' ' >&2
					cat << EOF > "$tmpfile"
#include	<sys/types.h>
#include	<sys/time.h>
#include	<time.h>
main(){}
EOF
					if checkcompile
					then
						echo "#define SYS_TIME_WITH_TIME"
					else
						echo "#undef SYS_TIME_WITH_TIME"
					fi
					;;

				THISDOMAIN)
					echo $echoarg "Autodetecting this domain's name... " >&2
					domain="`hostname | sed 's/^[^.]*//'`"
					if [ "$domain" = "." -o "$domain" = "" ]
					then
						domain=."`domainname`"
					fi
					if [ "$domain" = "." -o "$domain" = ".(none)" ]
					then
						domain=."`uname -n | sed 's/^[^.]*//'`"
					fi
					if [ "$domain" = "." ]
					then
						echo "Failed... Check config.h" >&2
						echo "$define $symbol $rest"
					else
						echo "$domain" >&2
						echo "#define THISDOMAIN "'"'"$domain"'"'
					fi
					;;

				*)
					echo "No autodetect possible for $symbol"... See config.h. >&2
					echo "$define $symbol $rest"
					;;
			esac
			;;

		*)
			echo "$define $symbol $rest"
			;;
	esac
done < config.h.generic > config.h

echo "Done! Please check config.h to correct any wrong guesses..." >&2
echo "Also check the Makefile: you might need to adjust CC, CFLAGS and LDFLAGS"
exit 0
