#!/bin/sh
echo "Don't exec me!"
exit 0

CC=cc
CPP="$CC -E"
TAR=tar
LIBDIR=
#LIBDIR=-L/usr/local/lib
INCLUDEDIR=
#INCLUDEDIR=-I/usr/local/include

TMPSRC=tmptest.c
TMPEXE=tmptest
LIBS=
trap "rm -f $TMPSRC $TMPEXE; exit 0" 0 1 2 15
docc='$CC $TMPSRC -o $TMPEXE $INCLUDEDIR $LIBDIR $LIBS > /dev/null 2>&1'
docpp='$CPP $TMPSRC $INCLUDEDIR > $TMPEXE 2> /dev/null'
testcpp='$CPP $TMPSRC $INCLUDEDIR 2>&1 > /dev/null'

setheader() {
	echo "" > $TMPSRC
	if [ -n "$1" ]; then
		for i in $1""; do
			echo "#include <${i}.h>" >> $TMPSRC
		done;
	fi
}

compile() {
	(setheader "$2")
	echo "$3 int test() { $1; } int main() { exit(test()); }" >> $TMPSRC
	if eval $docc; then return 0; else return 1; fi
}

useheader() {
	echo "Checking for <$1.h>..." 1>&2
	(setheader "$1")
	if [ -z "`eval $testcpp`" ]; then ret=0; else ret=1; fi
	rm -f $TMPEXE
	return $ret
}

noheader() { if (useheader "$1"); then return 1; else return 0; fi }

inheader() {
	re=$1
	func=$3
	if [ -z "$3" ]; then
		echo "Checking for \"$1\" in <$2.h>..." 1>&2
	else
		echo "Checking for \"$3\" in <$2.h>..." 1>&2
		re="CONFIGURE\($re\)"
		func="CONFIGURE($func)"
	fi
	(setheader "$2")
	echo "test() { $func; } main() {}" >> $TMPSRC
	eval $docpp
	if egrep "$re" $TMPEXE 2>&1 > /dev/null; then ret=0; else ret=1; fi
	rm -f $TMPEXE
	return $ret
}

noinheader() { if (inheader "$1" "$2" "$3"); then return 1; else return 0; fi }

isexist() {
	echo "Checking for $1..." 1>&2
	if (compile "$1" "$2" "$3"); then ret=0; else ret=1; fi
	rm -f $TMPEXE
	return $ret
}

noexist() { if (isexist "$1" "$2" "$3"); then return 1; else return 0; fi }

havelib() {
	echo "Checking for -l$1..." 1>&2
	ORGLIBS=$LIBS
	LIBS="$LIBS -l$1"
	if (compile "$2" "$3" "$4"); then ret=0; else ret=1; fi
	LIBS=$ORGLIBS
	rm -f $TMPEXE
	return $ret
}


cat ./config.hin

for i in TARUSESPACE CPP7BIT TERMCAPLIB REGEXPLIB EXTENDLIB \
NOVOID NOUID_T DECLERRLIST PWNEEDERROR NOERRNO NOFILEMODE \
NOUNISTDH NOSTDLIBH NOTZFILEH USELEAPCNT USESELECTH USESYSDIRH USETIMEH \
USETERMIO USETERMIOS USEDIRECT SYSVDIRENT NODNAMLEN HAVETIMEZONE NOTMGMTOFF \
USESTATVFSH USESTATFSH USEVFSH USEMOUNTH USEFSDATA \
USEFFSIZE STATFSARGS \
USEMNTENTH USEMNTTABH USEGETFSSTAT USEMNTCTL \
USEMNTINFOR USEMNTINFO USEGETMNT USEGETFSENT \
USEVFCNAME USEFFSTYPE \
USERE_COMP USEREGCOMP USEREGCMP \
USERAND48 USESETENV NOSELECT NOVSPRINTF \
USEUTIME USEGETWD USETIMELOCAL USEMKTIME USEUNAME SIGARGINT SIGFNCINT \
GETTODARGS
do
	echo "#ifdef	$i"
	echo "#undef	$i"
	echo "#endif"
done
echo
echo "/*"
echo " *	OS Oriented Definitions"
echo " */"
echo


if [ " $LANG" = " ja_JP.EUC" ]; then
	echo "#ifndef	CODEEUC"
	echo "#define	CODEEUC"
	echo "#endif"
	if [ -d /usr/man/$LANG ]; then
		echo "#ifndef	USEMANLANG"
		echo "#define	USEMANLANG"
		echo "#endif"
	fi
elif [ " $LANG" = " ja_JP.SJIS" ]; then
	echo "#ifdef	CODEEUC"
	echo "#undef	CODEEUC"
	echo "#endif"
	if [ -d /usr/man/$LANG ]; then
		echo "#ifndef	USEMANLANG"
		echo "#define	USEMANLANG"
		echo "#endif"
	fi
fi

echo "Checking for tar(1)..." 1>&2
touch $TMPSRC
$TAR cfo $TMPEXE $TMPSRC 2> /dev/null
set dummy `$TAR tvf $TMPEXE 2> /dev/null`
if echo $3 | egrep '/' 2>&1 > /dev/null; then
	ORGIFS=$IFS
	IFS=/
	set dummy $3
	if expr "$2" \> 100 2>&1 > /dev/null; then
		echo "#define	TARUSESPACE"
	fi;
	IFS=$ORGIFS
fi
rm -f $TMPEXE

echo "Checking for cpp(1)..." 1>&2
echo '#define test "01"' | tr '01' '\376\376' > $TMPSRC
echo 'main() {char *cp=test;}' >> $TMPSRC
if [ -z "`eval $testcpp`" ]; then
	:
else
	echo "#define	CPP7BIT"
fi
rm -f $TMPEXE

lib=
if havelib termlib "char b[1024]; tgetent(b,\"T\")"; then
	lib=-ltermlib
elif havelib ncurses "char b[1024]; tgetent(b,\"T\")"; then
	lib=-lncurses
elif havelib termcap "char b[1024]; tgetent(b,\"T\")"; then
	lib=-ltermcap
elif havelib curses "char b[1024]; tgetent(b,\"T\")"; then
	lib=-lcurses
elif havelib "curses -ltermcap" "char b[1024]; tgetent(b,\"T\")"; then
	lib=-l"curses -ltermcap"
fi
if [ -n "$lib" ]; then
	echo "#define	TERMCAPLIB	\"$lib\""
	LIBS="$LIBS $lib"
fi

lib=
if noexist "getmntent(0)" "stdio mntent" \
&& havelib sun "getmntent(0)" "stdio mntent"; then
	lib="$lib -lsun"
fi
if noexist "getmntinfo_r(0,0,0,0)" "stdio sys/mount" \
&& havelib c_r "getmntinfo_r(0,0,0,0)" "stdio sys/mount"; then
	lib="$lib -lc_r"
fi
if [ -n "$lib" ]; then
	echo "#define	EXTENDLIB	\"$lib\""
	LIBS="$LIBS $lib"
fi

if noexist "void *p"; then
	echo "#define	NOVOID"
fi
if noinheader uid_t sys/types; then
	echo "#define	NOUID_T"
fi
if inheader sys_errlist "stdio errno"; then
	echo "#define	DECLERRLIST"
fi
if noexist errno errno; then
	echo "#define	NOERRNO"
fi
if noinheader ' *[0-7]+.*, *[0-7]+.*, *[0-7]+.*' sys/stat \
"S_IRUSR,S_IWGRP,S_IXOTH"; then
	echo "#define	NOFILEMODE"
fi
if noheader unistd; then
	echo "#define	NOUNISTDH"
fi
if noheader stdlib; then
	echo "#define	NOSTDLIBH"
fi
if noheader tzfile; then
	echo "#define	NOTZFILEH"
elif isexist "struct tzhead t; t.tzh_leapcnt" tzfile; then
	echo "#define	USELEAPCNT"
fi

if noinheader 'struct.*[^a-zA-Z_]fd_set' sys/types \
&& inheader 'struct.*[^a-zA-Z_]fd_set' sys/select; then
	echo "#define	USESELECTH"
fi
if noinheader ' *0*x*[0-9a-fA-F ]* *' sys/param DEV_BSIZE \
&& inheader ' *0*x*[0-9a-fA-F ]* *' sys/dir DEV_BSIZE; then
	echo "#define	USESYSDIRH"
fi
if inheader 'struct.*[^a-zA-Z_]tm[^a-zA-Z_]' "sys/types sys/time time" \
&& noinheader 'struct.*[^a-zA-Z_]tm[^a-zA-Z_]' "sys/types sys/time"; then
	echo "#define	USETIMEH"
fi
if noheader sgtty \
|| inheader 'TIOCFLUSH' sgtty "TIOCFLUSH"; then
	if noheader termio; then
		echo "#define	USETERMIOS"
	else
		echo "#define	USETERMIO"
	fi
fi
if noheader dirent; then
	echo "#define	USEDIRECT"
else
	if isexist "struct dirent d; d.d_ino" "sys/types dirent" \
	&& noexist "struct dirent d; d.d_fileno" "sys/types dirent"; then
		echo "#define	SYSVDIRENT"
	fi
	if noexist "struct dirent d; d.d_namlen" "sys/types dirent"; then
		echo "#define	NODNAMLEN"
	fi
fi
if (inheader 'extern.*[^a-zA-Z_]timezone' sys/time \
&& isexist "extern time_t timezone; timezone" "sys/types sys/time") \
|| (inheader 'extern.*[^a-zA-Z_]timezone' time \
&& isexist "extern time_t timezone; timezone" "sys/types time"); then
	echo "#define	HAVETIMEZONE"
fi
if (noexist "struct tm t; t.tm_gmtoff" "sys/types sys/time" \
&& noexist "struct tm t; t.tm_gmtoff" "sys/types time"); then
	echo "#define	NOTMGMTOFF"
fi


arg=2
if inheader 'struct.*[^a-zA-Z_]fs_data' sys/mount; then
	echo "#define	USEFSDATA"
elif inheader 'struct.*[^a-zA-Z_]statfs' sys/vfs; then
	echo "#define	USEVFSH"
elif inheader 'struct.*[^a-zA-Z_]statfs' sys/mount; then {
	echo "#define	USEMOUNTH"
	if inheader MOUNT_UFS sys/mount; then
		echo "#define	USEFFSIZE"
	elif isexist "statfs(0,0,0)" sys/mount; then arg=3; fi
}
elif inheader 'struct.*[^a-zA-Z_]statfs' "sys/types sys/statfs" \
&& { { isexist "struct statfs f; statfs(0,&f,0,0)" "sys/types sys/statfs" \
&& arg=4; } \
|| isexist "struct statfs f; statfs(0,&f)" "sys/types sys/statfs"; }; then
	echo "#define	USESTATFSH"
elif inheader 'struct.*[^a-zA-Z_]statvfs' sys/statvfs; then
	echo "#define	USESTATVFSH"
fi
if [ "$arg" != "2" ]; then
	echo "#define	STATFSARGS	$arg"
fi

if isexist "getmntent(0)" "stdio mntent"; then
	echo "#define	USEMNTENTH"
elif isexist "getmntent(0,0)" "stdio sys/mnttab"; then
	echo "#define	USEMNTTABH"
elif isexist "mntctl(0,0,0)"; then
	echo "#define	USEMNTCTL"
elif isexist "getmntinfo_r(0,0,0,0)"; then
	echo "#define	USEMNTINFOR"
elif isexist "getmntinfo(0,0)"; then {
	echo "#define	USEMNTINFO"
	if isexist "struct vfsconf c; getvfsbytype(0); return(*(c.vfc_name));" \
	"sys/param sys/mount"; then
		echo "#define	USEVFCNAME"
	elif isexist "struct statfs f; return(*(f.f_fstypename));" \
	"sys/param sys/mount"; then
		echo "#define	USEFFSTYPE"
	fi
}
elif isexist "getfsstat(0,0,0)"; then
	echo "#define	USEGETFSSTAT"
elif isexist "getmnt(0,0,0,0,0)"; then
	echo "#define	USEGETMNT"
elif isexist "getfsent()" \
&& inheader 'char.*[^a-zA-Z_]fs_mntops' fstab; then
	echo "#define	USEGETFSENT"
fi


lib=
if isexist "extern char *re_comp(); re_comp(0)" ctype \
|| { havelib compat "extern char *re_comp(); re_comp(0)" ctype \
&& lib=-lcompat; }; then
	echo "#define	USERE_COMP"
elif isexist "regex_t *re; regfree(re)" "sys/types regex"; then
	echo "#define	USEREGCOMP"
elif isexist "extern char *regcmp(); regcmp(0,0)" ctype \
|| { havelib gen "extern char *regcmp(); regcmp(0,0)" ctype && lib=-lgen; } \
|| { havelib PW "extern char *regcmp(); regcmp(0,0)" ctype && lib=-lPW; } \
|| { havelib PW "extern char *regcmp(); regcmp(0,0)" ctype "char Error[1024];" \
&& lib=-lPW && echo "#define	PWNEEDERROR"; }; then
	echo "#define	USEREGCMP"
fi
if [ -n "$lib" ]; then
	echo "#define	REGEXPLIB	\"$lib\""
	LIBS="$LIBS $lib"
fi


if isexist "rand48()"; then
	echo "#define	USERAND48"
fi
if isexist "setenv(0,0,0)"; then
	echo "#define	USESETENV"
fi
if noexist "select(0,0,0,0,0)"; then
	echo "#define	NOSELECT"
fi
if noexist "vsprintf(0,0,0)" "stdio varargs"; then
	echo "#define	NOVSPRINTF"
fi
if noexist "utimes(0,0)"; then
	echo "#define	USEUTIME"
fi
if noexist "getcwd(0,0)"; then
	echo "#define	USEGETWD"
fi
if isexist "mktime(0)" time; then
	echo "#define	USEMKTIME"
elif isexist "timelocal(0)" time; then
	echo "#define	USETIMELOCAL"
fi
if noexist "gethostname(0,0)"\
&& isexist "uname(0)" sys/utsname; then
	echo "#define	USEUNAME"
fi
if inheader 'int.*[^a-zA-Z_]signal' signal; then
	echo "#define	SIGARGINT"
fi
if inheader '[^a-zA-Z_]signal[^a-zA-Z_]*\(.*\( *int *\) *\)' signal; then
	echo "#define	SIGFNCINT"
fi
if noexist "gettimeofday(0,0)" "sys/time time"\
&& isexist "gettimeofday(0)" "sys/time time"; then
	echo "#define	GETTODARGS	1"
fi
