#!/bin/sh

verbose=yes
VERBOSE_PRINT() {
	if test -n "$verbose"; then
		echo "$1"
	fi
}

DEBUG=
MAKE="echo \"Done.  Type 'make' to build\"" 
while test "$#" -gt 0; do
	case "$1" in
		-q)
			verbose=
			shift
		;;
		-m)
			MAKE="echo Compiling now...; make && \
			      echo Compilation successfully completed."
			shift
		;;
		-d)
			DEBUG=yes
			shift
		;;
		*)
			echo "Usage: $0 [-q] [-d]"
			exit 1
		;;
	esac
done

cflags=
ldflags=

if test -n "$DEBUG"; then
	cflags="$cflags -g -DDEBUG"
fi

: "${CC:=cc}"

defined() {
	$CC -E - >/dev/null 2>&1 <<EOF
#ifndef $1
#error $1 not defined
#endif
EOF
}
check_include() {
	echo "#include <$1>" | $CC -E - >/dev/null 2>&1
}
grep_include() {
	echo "#include <$1>" | $CC -E - 2>/dev/null | grep -q "$2"
}

solaris=0
irix=0
linux=0
defined linux && linux=1
if test "$linux" = 1; then
	echo "Aha!  You are running Linux!"
	echo "	Congratulations on the choice of a GNU generation."

	cflags="$cflags -O2"
else
	if defined NeXT; then
		echo "NeXTStep!  I'm assuming 3.0 or greater..."
		cflags="$cflags -O2"
	else
		# Check for HP-UX
		if test -e "/hp-ux"; then
			VERBOSE_PRINT "	I see you are running HP-UX."
			cflags="$cflags  -DHP_UX"
		elif test -e /usr/lib/libsocket.a; then # Solaris 2.1
			ldflags="$ldflags -lsocket"
			cflags="$cflags -g -DSOLARIS"
			solaris=1
			VERBOSE_PRINT "	I see you are running Solaris."
		else
			if defined __GLIBC__; then
				VERBOSE_PRINT "	A bold user of GNU/kFreeBSD or GNU/Hurd."
				cflags="$cflags -O2"
			else
				cflags="$cflags -O"
			fi
		fi

		# Check for IRIX
		if grep_include unistd.h _getpty; then
			cflags="$cflags -DIRIX"
			irix=1
			VERBOSE_PRINT "	I see you are running IRIX."
		fi
	fi # defined NeXT
fi # defined linux

check_ldflags() {
	echo 'int main() { return 0; }' | $CC -x c -o /dev/null - "$@" 2>/dev/null
}

# Check for the termcap library
if check_ldflags -ltermcap; then
	cflags="$cflags -DTERMCAP"
	ldflags="$ldflags -ltermcap"
	VERBOSE_PRINT "	Using the termcap library for terminal support."
fi

# Check for the ut_host field in the utmp file
if grep_include utmp.h ut_host; then
	cflags="$cflags -DHAVE_UTHOST"
	ldflags="$ldflags -lutil"
	VERBOSE_PRINT "	Your utmp file uses the host field."
fi

# Check for termio[s].h
if check_include termios.h; then
	cflags="$cflags -DHAVE_TERMIOS_H"
	VERBOSE_PRINT "	I will use termios tty structures."
elif check_include termio.h; then
	cflags="$cflags -DHAVE_TERMIO_H"
	VERBOSE_PRINT "	I will use termio tty structures."
else
	VERBOSE_PRINT "	I will use sgttyb tty structures."
fi

# Check for unistd.h
if check_include unistd.h; then
	cflags="$cflags -DHAVE_UNISTD_H"
fi

# Check for BSD tty compatibility. (HP-UX)
# Check for BSD tty compatibility. (HP-UX)
if check_include sys/bsdtty.h; then
	cflags="$cflags -DHAVE_BSDTTY_H"
	VERBOSE_PRINT "	I see you have BSD tty support."
fi

if ! defined __GLIBC__; then # False positive for GNU/kFreeBSD
	# Check for ioctl compatibility.  (FreeBSD)
	if check_include sys/ioctl_compat.h; then
		cflags="$cflags -DNEED_COMPAT_H"
		VERBOSE_PRINT "I will use your ioctl compatibility header."
	fi
fi

# Check for BSD socket library header (AT&T)
if check_include sys/inet.h; then
	cflags="$cflags -DNEED_INET_H"
	VERBOSE_PRINT "	I will use your inet compatibility header."
fi

# Check for BSD socket library header (AIX)
if check_include sys/select.h; then
	cflags="$cflags -DNEED_SELECT_H"
	VERBOSE_PRINT "	I will use your sockets definition header."
fi

# Check for wait4() (This always seems to be true.)
if grep_include sys/wait.h wait4; then
	cflags="$cflags -DHAVE_WAIT4"
	VERBOSE_PRINT "	I will use wait4() instead of waitpid()."
fi

# Tell the user what kind of configuration to do
if test -r /var/run/utmp -a -w /var/run/utmp -a "$(id -u)" -gt 0; then
	write_utmp=1
else
	write_utmp=0
fi

VERBOSE_PRINT ""
if test "$solaris" = 1 -o "$irix" = 1; then
	if test "$write_utmp" = 1; then
		VERBOSE_PRINT "This program doesn't need to be installed set-uid root."
		VERBOSE_PRINT ""
		VERBOSE_PRINT "This program will put entries for its windows in /var/run/utmp."
	else
		VERBOSE_PRINT "If installed set-uid root, this program will put entries for its windows"
		VERBOSE_PRINT "in /var/run/utmp."
	fi
elif test "$write_utmp" = 1; then
	VERBOSE_PRINT "This program will put entries for its windows in /var/run/utmp."
	VERBOSE_PRINT ""
	VERBOSE_PRINT "If installed set-uid root, this program will change ownership of the"
	VERBOSE_PRINT "ttys acquired to the user running this program."
else
	VERBOSE_PRINT "If installed set-uid root, this program will put entries for its windows"
	VERBOSE_PRINT "in /var/run/utmp, and will also change ownership of the ttys it acquires to the"
	VERBOSE_PRINT "user running this program."
fi
VERBOSE_PRINT ""


if test -x "/bin/csh"; then
	cflags="$cflags -DSHELL=\\\"/bin/csh\\\""
else
	cflags="$cflags -DSHELL=\\\"/bin/sh\\\""
fi

cat >Makefile <<EOF
# This Makefile has been generated from the Configure script.
# Shareware copyright 1993, by Sam Lantinga

CC = ${CC}
EOF
	sh scanpty >>Makefile
	cat >>Makefile <<EOF
PTYOPTS = -DPTYCHAR=\$(PTYCHAR) -DHEXDIGIT=\$(HEXDIGIT)

CFLAGS += -Wall $cflags \$(PTYOPTS)
LIBS = $ldflags
OBJS = splitvt.o misc.o utmp.o vt100.o videomem.o terminal.o vttest.o vtmouse.o \\
	parserc.o lock.o cut-paste.o

splitvt: \$(OBJS)
	\$(CC) \$(LDFLAGS) -o \$@ \$(OBJS) \$(LIBS)

clean:
	rm -f *.o core

distclean: clean
	rm -f splitvt Makefile

install: install-man
	mv splitvt /usr/local/bin/splitvt
	mv examples/xsplitvt /usr/local/bin/xsplitvt

install-man:
	cp splitvt.man /usr/local/man/man1/splitvt.1
EOF
eval $MAKE
