#!/bin/sh
#
# Professional Galactic Empires Client
# Copyright 1996-1999 David A. Holland
# Version 0.1, 6/11/99
# All rights reserved.
#
# See the included file "COPYING" for license information.
#
# pge client configure script
# ./configure --help for usage

# Things we need:
#   . compiler to use
#   . -g or -O2
#   . -lncurses or -lcurses or -lcurses -ltermcap
#   . whether we need snprintf
#   . whether we need -lsocket -lnsl
#   . whether we need -lresolv
#   . destdir
#   . srcdir
#   . default server
#   . default port

DESTDIR='/usr/local/games'
DEFSERVER='pge.bantha.org'
DEFPORT='5024'

while [ x$1 != x ]; do
   case "$1" in
	--help)
		cat << EOF
Usage: ./configure [options]
	Options are:
	--help               Print this help
	--version            Print version of configure script
	--destdir=DIR        Install in DIR [/usr/local/games]
	--srcdir=DIR         Find source in DIR [.]
        --with-default-server=HOST
        --with-default-port=PORT
                             Select a different default server to use
        --with-compiler=CC   Compile with CC [guessed]
	--with-debug         Compile with debug info

EOF
		exit
	;;
	--version)
		echo 'pgeclient configure script'
		echo '$Id: configure,v 1.14 2020/03/21 20:49:53 dholland Exp $'
		exit
	;;
	--destdir=*)
		DESTDIR=`echo $1 | sed 's/^[^=]*=//'`
	;;
	--srcdir=*)
		SRCDIR=`echo $1 | sed 's/^[^=]*=//'`
	;;
	--with-default-server=*)
		DEFSERVER=`echo $1 | sed 's/^[^=]*=//'`
	;;
	--with-default-port=*)
		DEFPORT=`echo $1 | sed 's/^[^=]*=//'`
	;;
	--with-compiler=*)
		CC=`echo $1 | sed 's/^[^=]*=//'`
	;;
	--with-debug)
		USE_DEBUG=1
	;;
	*)
		echo "Unknown option $1"
		echo "Use --help for help"
		exit 1
	;;
   esac
shift
done


if [ x$SRCDIR != x ]; then
	if [ ! -f $SRCDIR/version.txt ]; then
		echo '--srcdir: Wrong directory'
		exit 1
	fi
	echo "Using source in $SRCDIR"
else
	if [ ! -f version.txt ]; then
		if [ -f ../version.txt ]; then
			echo "Using source in .."
			SRCDIR='..'
		else
			echo 'Source not found - use --srcdir option'
			exit 1
		fi
	fi
fi

############################################################

# emit test program for compiler (see if it can compile c++)
cat << EOF > __conftest.cc
	template <class T> class foo { public: T x; foo(T y) { x=y; }};
	int main() { foo<int> *a = new foo<int> (0); return a->x; }
EOF

if [ x"$CC" = x ]; then
	# need to pick a compiler

	echo -n 'Looking for a compiler... '
	for TRYCC in gcc g++ clang clang++ c++ CC cc; do
	    (
	        $TRYCC __conftest.cc -o __conftest || exit 1;
	        ./__conftest || exit 1;
	    ) >/dev/null 2>&1 || continue
	    CC=$TRYCC
	    break;
	done
	if [ x$CC = x ]; then
	    echo 'failed.'
	    echo 'Cannot find a compiler. Run configure with --with-compiler.'
	    rm -f __conftest*
	    exit
	fi
	echo "$CC"
else
	# need to test the compiler

	echo -n 'Checking if compiler works... '
	if (
		$CC __conftest.cc -o __conftest >/dev/null 2>&1 || exit 1
		./__conftest >/dev/null 2>&1 || exit 1
	); then
	    echo 'yes'
	else
	    echo 'no'
	    echo "Compiler $CC does not exist or cannot compile C++; try another."
	    rm -f __conftest*
	    exit
	fi
fi

############################################################

# emit test program for ncurses. 
# Assume that if <ncurses/curses.h> exists, -lncurses should too.

cat << EOF > __conftest.cc
#include <ncurses/curses.h>
int main() { initscr(); endwin(); }
EOF

echo -n 'Checking for <ncurses/curses.h>... '
if (
	$CC __conftest.cc -lncurses -o __conftest >/dev/null 2>&1 || exit 1
	./__conftest >/dev/null 2>&1 || exit 1
); then
	CURSES_CFLAGS=
	CURSES_LIBS="-lncurses"
	echo 'yes'
else
	echo 'no'

########################################

# emit test program for other curses.
# Assume <curses.h>.

    cat << EOF > __conftest.cc
#include <curses.h>
int main() { initscr(); endwin(); }
EOF

    echo -n 'Checking for curses libs... '
    for TRY in "-lncurses" "-lcurses" "-ltermcap" "-lcurses -ltermcap"; do
	$CC __conftest.cc $TRY -o __conftest >/dev/null 2>&1 || continue;
	./__conftest >/dev/null 2>&1 || continue;
	CURSES_CFLAGS=-DNO_NCURSES
	CURSES_LIBS="$TRY"
	break;
    done

    if [ "x$CURSES_LIBS" = x ]; then
	if (
		$CC __conftest.cc -o __conftest >/dev/null 2>&1 || exit 1
		./__conftest >/dev/null 2>&1 || exit 1
	); then
		CURSES_CFLAGS=-DNO_NCURSES
		CURSES_LIBS=
		echo 'no libraries needed'
	else
		echo 'not found.'
		echo 'Cannot find a curses library. Try installing ncurses.'
		rm -f __conftest*
		exit
	fi
    else
	echo "$CURSES_LIBS"
    fi

fi  # matches the compilation test for ncurses

############################################################

# emit test program for -lsocket
# this one doesn't run and shouldn't be expected to

echo -n 'Checking for -lsocket... '

cat << EOF > __conftest.cc
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
	struct sockaddr_in sn; 
	int sock;
	sn.sin_port = 0;
	sn.sin_family = AF_INET;
	sn.sin_addr.s_addr = 0;
	sock = socket(AF_INET, SOCK_STREAM, 0);
	connect(sock, (struct sockaddr *)&sn, sizeof(sn));
	return 0;
}
EOF

if $CC __conftest.cc -o __conftest >/dev/null 2>&1; then
	echo 'no'
else
if $CC __conftest.cc -lsocket -o __conftest >/dev/null 2>&1; then
	LIBS="$LIBS -lsocket"
	echo 'yes'
else
	echo 'not found'
	echo 'Cannot find socket()/connect()... help!'
	rm -f __conftest*
	exit
fi;fi

############################################################

# emit test program for -lnsl/-lresolv
# this one doesn't run and shouldn't be expected to

echo -n 'Checking for gethostbyname... '

cat << EOF > __conftest.cc
#include <netdb.h>
int main() {
	struct hostent *h = gethostbyname("foo");
	if (h) return 1;
	return 0;
}
EOF

if $CC __conftest.cc $LIBS -o __conftest >/dev/null 2>&1; then
	echo 'found'
else
if $CC __conftest.cc $LIBS -lnsl -o __conftest >/dev/null 2>&1; then
	LIBS="$LIBS -lnsl"
	echo '-lnsl'
else
if $CC __conftest.cc $LIBS -lresolv -o __conftest >/dev/null 2>&1; then
	LIBS="$LIBS -lresolv"
	echo '-lresolv'
else
	echo 'not found'
	echo 'Cannot find gethostbyname()... help!'
	rm -f __conftest*
	exit
fi;fi;fi


############################################################

# emit test program for declaring snprintf

echo -n 'Checking if we need to declare vsnprintf... '

cat << EOF > __conftest.cc
#include <stdio.h>
#include <stdarg.h>
int main() { return (int)(void *)vsnprintf; }
EOF

if $CC __conftest.cc -o __conftest >/dev/null 2>&1; then
	echo 'no'
else
	SNPRINTF_CFLAGS="-DDECLARE_VSNPRINTF"
	echo 'yes'
fi

############################################################

echo -n 'Checking if we have vsnprintf... '

cat << EOF > __conftest.cc
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#ifdef DECLARE_SNPRINTF
extern "C" int vsnprintf(char *, size_t, const char *, va_list);
#endif
int dotest(const char *fmt, ...) {
	char buf[16];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(buf, 8, fmt, ap);
	va_end(ap);
	if (strlen(buf)!=7) return 1;
	return 0;
}

int main() { 
	return dotest("%s", "123456789");
}
EOF

for TRY in nil -lsnprintf -ldb; do
	if [ x$TRY = xnil ]; then
		TRYLIB=
		TRYREPORT=found
	else
		TRYLIB=$TRY
		TRYREPORT=$TRY
	fi
	$CC $SNPRINTF_CFLAGS __conftest.cc $TRYLIB -o __conftest \
		>/dev/null 2>&1 || continue
	./__conftest >/dev/null 2>&1 || continue
	SNPRINTF_LIBS="$TRYLIB"
	HAS_SNPRINTF=1
	echo $TRYREPORT
	break;
done

if [ x$HAS_SNPRINTF = x ]; then
	echo 'not found... using builtin version'
	SNPRINTF_CFLAGS="$SNPRINTF_CFLAGS -DCOMPILE_VSNPRINTF"
fi

############################################################

# Check for BSD make.
# (This also sets NOINSTALL)

echo -n 'Checking for BSD make...'

case "`uname || echo UNKNOWN`" in
	Linux) ;;
	NetBSD|FreeBSD|OpenBSD|BSD/OS) PMAKE=1;;
	HP-UX) NOINSTALL=1;;
	OSF1) NOINSTALL=1;;
	SunOS) ;;
	Irix) NOINSTALL=1;;
	Ultrix) ;;
	*) NOINSTALL=1;;
esac

if [ x$PMAKE != x ]; then echo found; else echo no; fi

echo -n 'Checking for BSD-compatible install...'
if [ x$NOINSTALL != x ]; then echo no; else echo yes; fi

############################################################

# Add warnings if we seem to be using gcc.

# -Wmissing-prototypes now causes gcc to howl at you if you use it
# on a C++ file, so don't include it.

case "$CC" in
	*gcc*|*egcs*) 
		WARN='-Wall -Wswitch -Wpointer-arith -Wno-cast-qual 
		      -Wcast-align -Wwrite-strings
		      -Winline' 
		MKOPT='-O2 -fomit-frame-pointer -finline-functions 
		       -felide-constructors -fnonnull-objects'
		CANDEPEND=1
	;;
	*)
		MKOPT='-O2'
	;;
esac

############################################################

if [ x$USE_DEBUG != x ]; then
	MKOPT='-g'
fi

# Assemble all CFLAGS.
CFLAGS="$WARN $MKOPT $CURSES_CFLAGS $SOCKLEN_CFLAGS $SNPRINTF_CFLAGS"

# Assemble all LIBS.
LIBS="$CURSES_LIBS $SNPRINTF_LIBS $LIBS"

# Make it all look nice.
CFLAGS=`echo $CFLAGS | sed 's/  */ /g;s/^ *//'`
LIBS=`echo $LIBS | sed 's/  */ /g;s/^ *//'`

########################################

echo 'Creating defs.h...'

echo '/* automatically generated do not edit */' > defs.h
echo '#define DEFAULTSERVER "'"$DEFSERVER"'"' >> defs.h
echo "#define PGE_PORT $DEFPORT"         >> defs.h


# Write out Makefile.
echo 'Creating Makefile...'

# Header
cat > Makefile << EOF
#
# Professional Galactic Empires Client
# Copyright 1996-1999 David A. Holland
# Version 0.1, 6/11/99
# All rights reserved.
#
# See the included file "COPYING" for license information.
#
# Client makefile
# Automatically generated, do not edit

EOF

# Configured defs 
echo "CC=$CC" >> Makefile
echo "CFLAGS=$CFLAGS" >> Makefile
echo "LDFLAGS=" >> Makefile
echo "LIBS=$LIBS" >> Makefile

# Static rules
cat >> Makefile << EOF
all: pge

tidy:
	rm -f core *~

clean: tidy
	rm -f *.o

distclean: clean
	rm -f pge defs.h Makefile

EOF

# Install rule
if [ x$NOINSTALL != x ]; then
	echo "$DESTDIR" | awk '{ 
		printf "install:\n\t(umask 022; cp pge %s/pge)\n\n", $1
	}' >> Makefile
else
	echo "$DESTDIR" | awk '{ 
		printf "install:\n\tinstall -c -m755 pge %s/pge\n\n", $1
	}' >> Makefile
fi

SRCS='main.cc client.cc display.cc util.cc'
OBJS=`echo $SRCS | sed 's/\.cc/.o/g'`

# Link rule
echo $OBJS | awk '{ 
   printf "pge: %s\n\t$(CC) $(LDFLAGS) %s $(LIBS) -o pge\n\n", $0, $0
}' >> Makefile

# Compile rules
for S in $SRCS; do
	O=`echo $S | sed 's/\.cc/.o/'`
	if [ x$SRCDIR != x ]; then
		S="$SRCDIR/$S"
	fi
	echo $O $S | awk '{ 
		printf "%s: %s\n\t$(CC) $(CFLAGS) %s -c\n\n", $1, $2, $2
	}' >> Makefile
done

# Depend
if [ x$PMAKE != x ]; then
	echo '.include "depend.mk"' >> Makefile
else
	echo "include depend.mk" >> Makefile
fi
if [ x$SRCDIR != x ]; then
	sed < $SRCDIR/depend.mk "s,\([^ ]*\.[ch]*\),$SRCDIR/\1,g" > depend.mk
fi

# Update depend
if [ x$CANDEPEND != x ]; then
	echo $SRCS | awk '{ 
	  printf "depend:\n\t$(CC) $(CFLAGS) -MM %s > depend.mk\n\n", $0
	}' >> Makefile
fi


rm -f __conftest*

echo 'Now type "make"'
