#!/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'
i_unistdh=

setheader() {
	echo "${i_unistdh}" > $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 USETERMINFO TERMCAPLIB REGEXPLIB EXTENDLIB \
NOVOID NOUID_T DECLSIGLIST NOSIGLIST \
DECLERRLIST PWNEEDERROR NOERRNO NOFILEMODE NOUNISTDH NOSTDLIBH NOTZFILEH \
USELEAPCNT USESELECTH USESYSDIRH USETIMEH USESTDARGH USEMKDEVH USEMKNODH \
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 NOTERMVAR \
USEUTIME USEGETWD USETIMELOCAL USEMKTIME USESYSCONF USELLSEEK USEUNAME \
USEWAITPID USESIGPMASK USERESOURCEH USEULIMITH USETIMESH \
GETPGRPVOID USESETPGID USESETVBUF SIGARGINT SIGFNCINT \
GETTODARGS
do
	echo "#ifdef	$i"
	echo "#undef	$i"
	echo "#endif"
done
echo
echo "/*"
echo " *	OS Oriented Definitions"
echo " */"
echo


case "$LANG" in
	ja_JP.EUC*|ja_JP.euc*|ja_JP.ujis)
		echo "#ifndef	CODEEUC"
		echo "#define	CODEEUC"
		echo "#endif"
		if [ -d /usr/man/$LANG ]; then
			echo "#ifndef	USEMANLANG"
			echo "#define	USEMANLANG"
			echo "#endif"
		fi
	;;
	ja_JP.SJIS)
		echo "#ifdef	CODEEUC"
		echo "#undef	CODEEUC"
		echo "#endif"
		if [ -d /usr/man/$LANG ]; then
			echo "#ifndef	USEMANLANG"
			echo "#define	USEMANLANG"
			echo "#endif"
		fi
	;;
esac

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 "tgetent(b,\"T\")" "" "char b[1024];"; then
	lib=-ltermlib
elif havelib ncurses "setupterm(\"T\",0,0)" "curses term"; then
	lib=-lncurses
	echo "#define	USETERMINFO"
elif havelib ncurses "tgetent(b,\"T\")" "" "char b[1024];"; then
	lib=-lncurses
elif havelib termcap "tgetent(b,\"T\")" "" "char b[1024];"; then
	lib=-ltermcap
elif havelib curses "setupterm(\"T\",0,0)" "curses term"; then
	lib=-lcurses
	echo "#define	USETERMINFO"
elif havelib curses "tgetent(b,\"T\")" "" "char b[1024];"; then
	lib=-lcurses
elif havelib "curses -ltermcap" "tgetent(b,\"T\")" "" "char b[1024];"; 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_siglist signal; then
	echo "#define	DECLSIGLIST"
elif noexist 'return(*(sys_siglist[0]))' signal 'extern char *sys_siglist[];'
then
	echo "#define	NOSIGLIST"
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"
else
	i_unistdh="#include <unistd.h>"
fi
if noheader stdlib; then
	echo "#define	NOSTDLIBH"
fi
if noheader tzfile; then
	echo "#define	NOTZFILEH"
elif isexist "t.tzh_leapcnt" tzfile "struct tzhead t;"; then
	echo "#define	USELEAPCNT"
fi

if noinheader 'struct.*[^a-zA-Z0-9_]fd_set[^a-zA-Z0-9_]' sys/types \
&& inheader 'struct.*[^a-zA-Z0-9_]fd_set[^a-zA-Z0-9_]' 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-Z0-9_]tm[^a-zA-Z0-9_]' "sys/types sys/time time" \
&& noinheader 'struct.*[^a-zA-Z0-9_]tm[^a-zA-Z0-9_]' "sys/types sys/time"; then
	echo "#define	USETIMEH"
fi
if inheader va_list stdarg; then
	echo "#define	USESTDARGH"
fi
if isexist "return(major(0))" sys/mkdev; then
	echo "#define	USEMKDEVH"
fi
if isexist "return(major(0))" sys/mknod; then
	echo "#define	USEMKNODH"
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 "d.d_ino" "sys/types dirent" "struct dirent d;" \
	&& noexist "d.d_fileno" "sys/types dirent" "struct dirent d;"; then
		echo "#define	SYSVDIRENT"
	fi
	if noexist "d.d_namlen" "sys/types dirent" "struct dirent d;"; then
		echo "#define	NODNAMLEN"
	fi
fi
if (inheader 'extern.*[^a-zA-Z0-9_]timezone[^a-zA-Z0-9_]' sys/time \
&& isexist "timezone=0" "sys/types sys/time" "extern time_t timezone;") \
|| (inheader 'extern.*[^a-zA-Z0-9_]timezone[^a-zA-Z0-9_]' time \
&& isexist "timezone=0" "sys/types time" "extern time_t timezone;"); then
	echo "#define	HAVETIMEZONE"
fi
if (noexist "t.tm_gmtoff" "sys/types sys/time" "struct tm t;" \
&& noexist "t.tm_gmtoff" "sys/types time" "struct tm t;"); then
	echo "#define	NOTMGMTOFF"
fi


arg=2
if inheader 'struct.*[^a-zA-Z0-9_]fs_data[^a-zA-Z0-9_]' sys/mount; then
	echo "#define	USEFSDATA"
elif inheader 'struct.*[^a-zA-Z0-9_]statfs[^a-zA-Z0-9_]' sys/vfs; then
	echo "#define	USEVFSH"
elif inheader 'struct.*[^a-zA-Z0-9_]statfs[^a-zA-Z0-9_]' 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-Z0-9_]statfs[^a-zA-Z0-9_]' \
"sys/types sys/statfs" \
&& { { isexist "statfs(0,&f,0,0)" "sys/types sys/statfs" "struct statfs f;" \
&& arg=4; } \
|| isexist "statfs(0,&f)" "sys/types sys/statfs" "struct statfs f;"; }; then
	echo "#define	USESTATFSH"
elif inheader 'struct.*[^a-zA-Z0-9_]statvfs[^a-zA-Z0-9_]' 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 'getvfsbytype(0); return(*(c.vfc_name));' \
	"sys/param sys/mount" "struct vfsconf c;"; then
		echo "#define	USEVFCNAME"
	elif isexist 'return(*(f.f_fstypename))' \
	"sys/param sys/mount" "struct statfs f;"; 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-Z0-9_]fs_mntops[^a-zA-Z0-9_]' fstab; then
	echo "#define	USEGETFSENT"
fi


lib=
if isexist "re_comp(0)" ctype 'extern char *re_comp();' \
|| { havelib compat "re_comp(0)" ctype 'extern char *re_comp();' \
&& lib=-lcompat; }; then
	echo "#define	USERE_COMP"
elif isexist "regfree(re)" "sys/types regex" 'regex_t *re;'; then
	echo "#define	USEREGCOMP"
elif isexist "regcmp(0,0)" ctype 'extern char *regcmp();' \
|| { havelib gen "regcmp(0,0)" ctype 'extern char *regcmp();' && lib=-lgen; } \
|| { havelib PW "regcmp(0,0)" ctype 'extern char *regcmp();' && lib=-lPW; } \
|| { havelib PW "regcmp(0,0)" ctype 'extern char *regcmp(); 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 "ospeed=0" "" "extern short ospeed;" \
|| noexist "BC=NULL" "" "extern char *BC;"; then
	echo "#define	NOTERMVAR"
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 isexist "sysconf(0)"; then
	echo "#define	USESYSCONF"
fi
if isexist "_llseek(0,0,0,0,0)" "linux/unistd" 'static _syscall5(int, _llseek,
	unsigned int, fd,
	unsigned long, ofs_h,
	unsigned long, ofs_l,
	long long *, result,
	unsigned int, whence);
'; then
	echo "#define	USELLSEEK"
fi
if noexist "gethostname(0,0)" && isexist "uname(0)" sys/utsname; then
	echo "#define	USEUNAME"
fi
if isexist "waitpid(0,0,0)" sys/wait; then
	echo "#define	USEWAITPID"
fi
if isexist "sigprocmask(0,0,0)" signal; then
	echo "#define	USESIGPMASK"
fi
if isexist "getrusage(0,0)" "sys/types sys/time sys/resource" \
&& isexist "getrlimit(0,0)" "sys/types sys/time sys/resource"; then
	echo "#define	USERESOURCEH"
else
	if isexist "ulimit(0,0)" ulimit; then
	echo "#define	USEULIMITH"
	fi
	if isexist "times(0)" "sys/types sys/times"; then
	echo "#define	USETIMESH"
	fi
fi
if noexist "getpgrp(0)" && isexist "getpgrp()"; then
	echo "#define	GETPGRPVOID"
fi
if isexist "setpgid(0,0)"; then
	echo "#define	USESETPGID"
fi
if isexist "setvbuf(0,0,0,0)" stdio; then
	echo "#define	USESETVBUF"
fi
if inheader 'int.*[^a-zA-Z0-9_]signal[^a-zA-Z0-9_]' signal; then
	echo "#define	SIGARGINT"
fi
if inheader '[^a-zA-Z0-9_]signal([^a-zA-Z0-9_].*\(|\()[ (]*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
