:
#
# Netris -- A free networked version of T*tris
# Copyright (C) 1994-1996,1999  Mark H. Weaver <mhw@netris.org>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: Configure,v 1.18 1999/05/16 06:56:19 mhw Exp $
#

CC="gcc"
COPT="-g -O"
CEXTRA=""
LEXTRA=""
CURSES_HACK=false

while [ $# -ge 1 ]; do
	opt="$1"
	shift
	case "$opt" in
		-g)
			COPT="-g"
			CEXTRA="-Wall -Wstrict-prototypes"
			;;
		-O*)
			COPT="$opt"
			CEXTRA="-DNDEBUG"
			;;
		--cc)
			CC="$1"
			shift
			;;
		--copt)
			COPT="$1"
			shift
			;;
		--cextra)
			CEXTRA="$1"
			shift
			;;
		--lextra)
			LEXTRA="$1"
			shift
			;;
		--curses-hack)
			CURSES_HACK=true
			;;
		*)
			cat << "END"
Usage: ./Configure [options...]
    -g: Full debugging, no optimization, and full warnings
    -O?: Optimization, no debugging or warnings
    --cc <compiler>: Set the C compiler to use (default "gcc")
    --copt <opt>: Set C optimization flags
    --cextra <opt>: Set extra C flags
    --lextra <opt>: Set extra linker flags
    --curses-hack: Disable scroll-optimization for broken curses
END
			exit 1
			;;
	esac
done

CFLAGS="$COPT $CEXTRA"

echo "Checking for libraries"
echo 'main(){}' > test.c
LFLAGS=""
for lib in -lcurses -lncurses; do
	if $CC $CFLAGS $LEXTRA test.c $lib > /dev/null 2>&1; then
		LFLAGS="$lib"
	fi
done
for lib in -lsocket -lnsl -ltermcap; do 
	if $CC $CFLAGS $LEXTRA test.c $lib > /dev/null 2>&1; then
		LFLAGS="$LFLAGS $lib"
	fi
done

echo "Checking for on_exit()"
cat << END > test.c
void handler(void) {}
main() { on_exit(handler, (void *)0); }
END
if $CC $CFLAGS $LEXTRA test.c > /dev/null 2>&1; then
	HAS_ON_EXIT=true
else
	HAS_ON_EXIT=false
fi

echo "Checking for sigprocmask()"
cat << END > test.c
#include <signal.h>
main() { sigset_t set; sigprocmask(SIG_BLOCK, &set, &set); }
END
if $CC $CFLAGS $LEXTRA test.c > /dev/null 2>&1; then
	HAS_SIGPROCMASK=true
else
	HAS_SIGPROCMASK=false
fi

echo "Checking for getopt.h"
cat << END > test.c
#include <getopt.h>
main(){}
END

if $CC $CFLAGS $LEXTRA test.c > /dev/null 2>&1; then
	HAS_GETOPT_H=true
else
	HAS_GETOPT_H=false
fi

echo "Checking for memory.h"
cat << END > test.c
#include <memory.h>
main(){}
END

if $CC $CFLAGS $LEXTRA test.c > /dev/null 2>&1; then
	HAS_MEMORY_H=true
else
	HAS_MEMORY_H=false
fi

rm -f test.c test.o a.out

ORIG_SOURCES="game- curses- shapes- board- util- inet- robot-"
GEN_SOURCES="version-"
SOURCES="$ORIG_SOURCES $GEN_SOURCES"

SRCS="`echo $SOURCES | sed -e s/-/.c/g`"
OBJS="`echo $SOURCES | sed -e s/-/.o/g`"

DISTFILES="README FAQ COPYING VERSION Configure netris.h sr.c robot_desc"
DISTFILES="$DISTFILES `echo $ORIG_SOURCES | sed -e s/-/.c/g`"

echo > .depend

echo "Creating Makefile"
sed -e "s/-LFLAGS-/$LFLAGS/g" -e "s/-SRCS-/$SRCS/g" \
	-e "s/-OBJS-/$OBJS/g" -e "s/-DISTFILES-/$DISTFILES/g" \
	-e "s/-COPT-/$COPT/g" -e "s/-CEXTRA-/$CEXTRA/g" \
	-e "s/-LEXTRA-/$LEXTRA/g" -e "s/-CC-/$CC/g" \
	<< "END" > Makefile
#
# Automatically generated by ./Configure -- DO NOT EDIT!
#

CC = -CC-
COPT = -COPT-
CEXTRA = -CEXTRA-
LEXTRA = -LEXTRA-
LFLAGS = -LEXTRA- -LFLAGS-
CFLAGS = $(CEXTRA) $(COPT)

PROG = netris
HEADERS = netris.h

SRCS = -SRCS-
OBJS = -OBJS-
DISTFILES = -DISTFILES-

all: Makefile config.h proto.h $(PROG) sr

$(PROG): $(OBJS)
	$(CC) -o $(PROG) $(OBJS) $(LFLAGS)

sr: sr.o
	$(CC) -o sr sr.o $(LFLAGS)

.c.o:
	$(CC) $(CFLAGS) -c $<

Makefile config.h: Configure
	@echo "Makefile and/or config.h is out of date"
	@echo "Run ./Configure now"
	@false

version.c: VERSION
	@echo "Creating version.c"
	@sed -e 's/^\(.*\)$$/char *version_string = "\1";/' VERSION > $@

proto.h: $(SRCS)
	@touch $@
	@mv $@ $@.old
	@cat $(SRCS) | grep '^ExtFunc[ 	]' | sed -e 's/)$$/);/' > $@
	@if diff $@.old $@ > /dev/null 2>&1; then :; else \
		echo "proto.h changed"; \
		touch proto.chg; \
	fi
	@rm -f $@.old

depend: proto.h $(SRCS)
	@echo "Checking dependencies"
	@sed -n -e '1,/make depend #####$$/p' Makefile > Makefile.new
	@$(CC) -M $(SRCS) | sed -e 's/proto\.h/proto.chg/g' >> Makefile.new
	@mv -f Makefile.new Makefile

dist: $(DISTFILES)
	@vers=`cat VERSION`; \
	dir="netris-$$vers"; \
	echo "Creating $$dir directory"; \
	rm -rf $$dir; \
	mkdir $$dir; \
	cp $(DISTFILES) $$dir; \
	chmod 755 $$dir; \
	chmod 644 $$dir/*; \
	chmod 755 $$dir/Configure; \
	echo "Creating $$dir.tar.gz"; \
	tar -cvzof $$dir.tar.gz $$dir

clean:
	rm -f proto.h proto.chg $(PROG) $(OBJS) version.c test.c a.out sr sr.o

cleandir: clean
	rm -f .depend Makefile config.h

##### DO NOT EDIT OR DELETE THIS LINE, it's needed by make depend #####
END

echo "Creating config.h"
cat << END > config.h
/*
 * Automatically generated by ./Configure -- DO NOT EDIT!
 */

END

if [ "$HAS_GETOPT_H" = "true" ]; then
	echo "#include <getopt.h>" >> config.h
else
	echo "extern char *optarg;" >> config.h
	echo "extern int optind;" >> config.h
fi
if [ "$HAS_MEMORY_H" = "true" ]; then
	echo "#include <memory.h>" >> config.h
fi
if [ "$HAS_ON_EXIT" = "true" ]; then
	echo "#define HAS_ON_EXIT" >> config.h
fi
if [ "$HAS_SIGPROCMASK" = "true" ]; then
	echo "#define HAS_SIGPROCMASK" >> config.h
fi
if [ "$CURSES_HACK" = "true" ]; then
	echo "#define CURSES_HACK" >> config.h
fi

echo "Running 'make depend'"
if make depend; then :; else cat << END; fi

make depend failed, but that's OK unless you're doing development
END
cat << END

Now do a 'make'

END

# vi: ts=4 ai
