dnl Process this file with autoconf to produce a configure script.
dnl Configuration input file for LK.
dnl vi: set tabstop=4 shiftwidth=4:
dnl $Id: configure.in,v 1.51 1998/10/17 22:17:26 neto Exp neto $

dnl      Copyright (C) 1997 David Neto
dnl   
dnl      This program is free software; you can redistribute it and/or modify
dnl      it under the terms of the GNU General Public License as published by
dnl      the Free Software Foundation; either version 2, or (at your option)
dnl      any later version.
dnl   
dnl      This program is distributed in the hope that it will be useful,
dnl      but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl      GNU General Public License for more details.
dnl   
dnl      You should have received a copy of the GNU General Public License
dnl      along with this program; if not, write to the Free Software
dnl      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
dnl      02111-1307, USA.


dnl Prologue ###########################################

AC_INIT(src/lk.w)
AC_REVISION($Revision: 1.51 $)dnl
AC_PREREQ(2.12)dnl This was written in conjunction with Autoconf 2.12.

dnl Initialize Automake 1.3
AM_INIT_AUTOMAKE(lk,0.4.17)
dnl PACKAGE=lk AC_SUBST(PACKAGE)

dnl Be Win32-friendly, at least for Cygwin32 installations.
AM_CYGWIN32

dnl Must create config.h.in (Maybe use autoheader) 
dnl (Put config.h in AC_OUTPUT macro?)  No.
AM_CONFIG_HEADER(config.h) 

dnl Default place for installation.  Without this, the default is /usr/local
AC_PREFIX_DEFAULT(/home/neto/lk)

dnl We use VERSION_STRING within the progs.
AC_DEFINE_UNQUOTED(VERSION_STRING,"$VERSION")

dnl Allow configuration on AIX by enabling certain BSD library functions.
dnl (This may or may not define _ALL_SOURCE.)
dnl AC_AIX


dnl Determine host type; defines host_cpu, host_vendor, host_os, among
dnl other things.
AC_CANONICAL_HOST

dnl Checks for programs ###############################

AC_PROG_CC
dnl SGI boxes, use the SGI compiler and -xansi
if test $GCC = yes; then
	C_DIALECT="-ansi -pedantic"
	C_WARNINGS="-Wall -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wmissing-prototypes"
else
	C_DIALECT=""
	C_WARNINGS=""
fi
CFLAGS="$C_DIALECT $C_WARNINGS $CFLAGS"
AC_SUBST(CFLAGS)


dnl The scripts that process output from LK require Perl and Bash
dnl I also use a Perl script to post-process literate Pizza programs
AC_PATH_PROG(PERL,perl)
AC_PATH_PROG(BASH,bash)

dnl CWEB programs ctangle and cweave are optional since C, Pizza, and Java
dnl sources are included.  
dnl But to modify the source and recompile, you really should have CWEB.  :)
dnl TeX is required to make dvi documentation.
dnl TEXINPUTS is inherited from the user's environment at TeX time.
dnl mp is MetaPost, for figures.
AC_PATH_PROG(CTANGLE,ctangle)
AC_PATH_PROG(CWEAVE,cweave)
AC_PATH_PROG(TEX,tex)
AC_PATH_PROG(LATEX,latex2e)
if test -z "$LATEX"; then
	AC_PATH_PROG(LATEX,latex)
fi
AC_PATH_PROG(DVIPS,dvips)
if test -z "$DVIPS"; then
	AC_PATH_PROG(DVIPS,dvipsk)
fi
AC_PATH_PROG(MP,mp)

dnl For CWEB programs, -bhp turns off banner, happy, progress output
dnl But put CWEAVEFLAGS and CTANGLEFLAGS after the default flags so
dnl the user can override on the make command line.
CTANGLEFLAGS_DEFAULT=-bhp AC_SUBST(CTANGLEFLAGS_DEFAULT)
CWEAVEFLAGS_DEFAULT=-bhp AC_SUBST(CWEAVEFLAGS_DEFAULT)
if test -z "$CTANGLE"; then
    CTANGLE_CMD='@echo "Sorry, ctangle is not installed, so I cannot make $@ from $<" 1>&2; exit 1;';
dnl	PIZZATANGLE_CMD=$CTANGLE_CMD
dnl	JAVATANGLE_CMD=$CTANGLE_CMD
else
    # -bhp turns off the banner, happy and progress messages.
	# We use $*.w instead of $< because we might use this command line to
	# make something with a .ch file as the dependency.
    CTANGLE_CMD='if test -f $(srcdir)/$*.ch; then \\\
        $(CTANGLE) $(CTANGLEFLAGS_DEFAULT) $(CTANGLEFLAGS) $(srcdir)/$*.w $(srcdir)/$*.ch; \\\
	else \\\
		$(CTANGLE) $(CTANGLEFLAGS_DEFAULT) $(CTANGLEFLAGS) $(srcdir)/$*.w; \\\
	fi';
dnl    PIZZATANGLE_CMD='if test -f $(srcdir)/$*.ch; then \\\
dnl        $(CTANGLE) $(CTANGLEFLAGS_DEFAULT) $(CTANGLEFLAGS) $(srcdir)/$*.pw $(srcdir)/$*.ch; \\\
dnl	else \\\
dnl		$(CTANGLE) $(CTANGLEFLAGS_DEFAULT) $(CTANGLEFLAGS) $<; \\\
dnl	fi';
dnl    JAVATANGLE_CMD='if test -f $(srcdir)/$*.ch; then \\\
dnl        $(CTANGLE) $(CTANGLEFLAGS_DEFAULT) $(CTANGLEFLAGS) $(srcdir)/$*.jw $(srcdir)/$*.ch; \\\
dnl	else \\\
dnl		$(CTANGLE) $(CTANGLEFLAGS_DEFAULT) $(CTANGLEFLAGS) $<; \\\
dnl	fi';
fi
if test -z "$CWEAVE"; then
	CWEAVE_CMD='@echo "Sorry, cweave is not installed, so I cannot make $@ from $<" 1>&2; exit 1;';
dnl	PIZZAWEAVE_CMD=$CWEAVE_CMD
dnl	JAVAWEAVE_CMD=$CWEAVE_CMD
else
	# -bhp turns off the banner, happy and progress messages.
	CWEAVE_CMD='if test -f $(srcdir)/$*.ch; then \\\
		$(CWEAVE) $(CWEAVEFLAGS_DEFAULT) $(CWEAVEFLAGS) $(srcdir)/$*.w $(srcdir)/$*.ch; \\\
	else \\\
		$(CWEAVE) $(CWEAVEFLAGS_DEFAULT) $(CWEAVEFLAGS) $<; \\\
	fi';
dnl	PIZZAWEAVE_CMD='if test -f $(srcdir)/$*.ch; then \\\
dnl		$(CWEAVE) $(CWEAVEFLAGS_DEFAULT) $(CWEAVEFLAGS) $(srcdir)/$*.pw $(srcdir)/$*.ch; \\\
dnl	else \\\
dnl		$(CWEAVE) $(CWEAVEFLAGS_DEFAULT) $(CWEAVEFLAGS) $<; \\\
dnl	fi';
dnl	JAVAWEAVE_CMD='if test -f $(srcdir)/$*.ch; then \\\
dnl		$(CWEAVE) $(CWEAVEFLAGS_DEFAULT) $(CWEAVEFLAGS) $(srcdir)/$*.jw $(srcdir)/$*.ch; \\\
dnl	else \\\
dnl		$(CWEAVE) $(CWEAVEFLAGS_DEFAULT) $(CWEAVEFLAGS) $<; \\\
dnl	fi';
fi
if test -z "$TEX"; then 
	TEX_CMD='@echo "Sorry, TeX is not installed, so I cannot make $@ from $<" 1>&2; exit 1;';
else
	TEX_CMD='$(TEX) $(TEXFLAGS) $<;';
fi
if test -z "$LATEX"; then 
	LATEX_CMD='@echo "Sorry, LaTeX is not installed, so I cannot make $@ from $<" 1>&2; exit 1;';
else
	LATEX_CMD='$(LATEX) $(LATEXFLAGS) $<;';
fi
if test -z "$DVIPS"; then 
	DVIPS_CMD='@echo "Sorry, LaTeX is not installed, so I cannot make $@ from $<" 1>&2; exit 1;';
else
	DVIPS_CMD='$(DVIPS) $(DVIPSFLAGS) $< -o $@;';
fi
if test -z "$MP"; then 
	MP_CMD='@echo "Sorry, MetaPost is not installed, so I cannot make $@ from $<" 1>&2; exit 1;';
else
	MP_CMD='$(MP) $(MPFLAGS) $< \\bye;';
fi
AC_SUBST(CTANGLE_CMD)
dnl AC_SUBST(PIZZATANGLE_CMD)
dnl AC_SUBST(JAVATANGLE_CMD)
AC_SUBST(CWEAVE_CMD)
dnl AC_SUBST(PIZZAWEAVE_CMD)
dnl AC_SUBST(JAVAWEAVE_CMD)
AC_SUBST(TEX_CMD)
AC_SUBST(LATEX_CMD)
AC_SUBST(DVIPS_CMD)
AC_SUBST(MP_CMD)

dnl The tsp instance generators in the tspgen directory are written in
dnl literate Pizza.  They need the CWEB suite to be built from sources.
dnl To be compiled, they need a Pizza compiler, which I call pizzac,
dnl a non-standard name; I think the Pizza web sites suggest 
dnl pc as the pizza compiler name.
dnl AC_PATH_PROG(PIZZAC,pizzac)
dnl if test -z "$PIZZAC"; then
dnl 	echo yeah
dnl 	PIZZAC_CMD='@echo "Sorry, a Pizza compiler is not installed as pizzac, so I cannot make $@ from $<" 1>&2; exit 1;';
dnl else
dnl 	PIZZAC_CMD='$(PIZZAC) $(PIZZACFLAGS) $<;';
dnl fi
dnl AC_SUBST(PIZZAC_CMD)
dnl AC_PATH_PROG(JAVAC,javac)
dnl if test -z "$JAVAC"; then
dnl 	JAVAC_CMD='@echo "Sorry, a Java compiler is not installed as javac, so I cannot make $@ from $<" 1>&2; exit 1;';
dnl else
dnl 	JAVAC_CMD='$(JAVAC) $(JAVACFLAGS) $<;';
dnl fi
dnl AC_SUBST(JAVAC_CMD)



dnl Checks for libraries ##############################
dnl `sqrt' is an arbitrary function in the math library, -lm
AC_CHECK_LIB(m, sqrt)

dnl Solaris 2.4 is strange because it puts getrusage and other BSD functions
dnl in -lucb.
dnl The user might have to have /usr/ucblib in their LD_LIBRARY_PATH.
dnl What about other systems?
AC_CHECK_LIB(ucb, getrusage)


dnl Checks for header files ###########################
AC_HEADER_STDC
AC_CHECK_HEADERS(limits.h sys/time.h unistd.h time.h)

dnl Header files on Solaris and IRIX are missing some prototypes.
dnl We provide prototypes for standard things only.
OS_IS_SUNOS=0; 
OS_HAS_BROKEN_HEADERS=0;
case $host_os in
	solaris* | sunos* ) OS_HAS_BROKEN_HEADERS=1 OS_IS_SUNOS=1 ;;
	irix*) OS_HAS_BROKEN_HEADERS=1 ;;
esac
AC_DEFINE_UNQUOTED(OS_HAS_BROKEN_HEADERS,$OS_HAS_BROKEN_HEADERS)

dnl We have to jump through hoops to properly get `[' and `]' because
dnl Autoconf does some funny substitutions, destryoing my sed expression.
dnl At one point I used echo -e to define lbr and rbr, but that doesn't
dnl work on Solaris 2.4.
lbr=`echo ' ' | tr ' ' '\133'`
rbr=`echo ' ' | tr ' ' '\135'`
OS_VERSION_MAJOR=`uname -r | sed -e 's/\('$lbr'0-9'$rbr'*\)\..*/\1/'`
OS_VERSION_MINOR=`uname -r | sed -e 's/'$lbr'0-9'$rbr'*\.\('$lbr'0-9'$rbr'*\).*/\1/'`
dnl echo host is $host_cpu $host_vendor $host_os 
dnl echo OS_VERSION_MAJOR is $OS_VERSION_MAJOR
dnl echo OS_VERSION_MINOR is $OS_VERSION_MINOR
AC_DEFINE_UNQUOTED(OS_IS_SUNOS,$OS_IS_SUNOS)
AC_DEFINE_UNQUOTED(OS_VERSION_MAJOR,$OS_VERSION_MAJOR)
AC_DEFINE_UNQUOTED(OS_VERSION_MINOR,$OS_VERSION_MINOR)



dnl Checks for typedefs ###############################
AC_TYPE_SIZE_T


dnl Checks for structures #############################
dnl struct rusage


dnl Checks for compiler characteristics ###############
AC_C_CONST
AC_C_INLINE
case "$ac_cv_c_inline" in
	no) AC_DEFINE(COMPILER_SUPPORTS_INLINE,0) ;;
	*) AC_DEFINE(COMPILER_SUPPORTS_INLINE,1) ;;
esac
AC_C_LONG_DOUBLE
dnl Do the most significant bytes come first in a word?
AC_C_BIGENDIAN
dnl Find sizes of variables of various sizes
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(unsigned int)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(long long)

dnl Checks for library functions ######################
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(getrusage getpagesize gethostname strdup time ctime getopt nrand48)

dnl See if the library defines optarg, optind, opterr, and optopt, all required
dnl by getopt
AC_TRY_LINK([
	#if HAVE_UNISTD_H
		#define _POSIX_C_SOURCE 2
		#include <unistd.h>
	#endif
	#include <stdlib.h>
	],[{extern char *optarg; extern int optind, opterr, optopt;}],
	AC_DEFINE_UNQUOTED(LIBRARY_DEFINES_OPTARG_ETC,1),
	AC_DEFINE_UNQUOTED(LIBRARY_DEFINES_OPTARG_ETC,0))
dnl Now see if the headers also *declare* those functions.
AC_TRY_LINK([
	#if HAVE_UNISTD_H
		#define _POSIX_C_SOURCE 2
		#include <unistd.h>
	#endif
	#include <stdlib.h>
	],[{char *foo = optarg;}],
	AC_DEFINE_UNQUOTED(HEADERS_DECLARE_OPTARG_ETC,1),
	AC_DEFINE_UNQUOTED(HEADERS_DECLARE_OPTARG_ETC,0))


dnl Checks system services ############################


dnl Epilogue ##########################################

dnl The "test" for generating stamp-h is needed by Automake when using
dnl macro AC_CONFIGURE_HEADER.
AC_OUTPUT([\
Makefile \
gen.pl \
genhk.pl \
doitall \
src/Makefile \
src/compile.c \
dnl tspgen/Makefile \
dnl tspgen/pizzaw2pizza \
dnl doc/Makefile \
data/Makefile \
expt/Makefile \
script/Makefile \
script/att2upper.pl \
script/cd.pl \
script/cfresults.pl \
script/countpages.pl \
script/distcalc.pl \
script/domst.pl \
script/doshake.pl \
script/doshakelk.pl \
script/expt.pl \
script/GB_flip.pm \
script/hist.pl \
script/improve.pl \
script/least.pl \
script/lkdoit \
script/lkdoitcmp \
script/milestone.pl \
script/mkcwebch.pl \
script/mstelratio.pl \
script/mstrsortlen.pl \
script/mstsortlen.pl \
script/myps2eps.pl \
script/number.pl \
script/PRNG.pm \
script/probe.pl \
script/results.pl \
script/spectrum.pl \
script/TSP.pm \
script/tsp2dm.pl \
script/tspbgen.pl \
script/tspreorder.pl \
script/tsprotate.pl \
script/tsplib2rothberg.pl \
], [test -z "$CONFIG_HEADERS" || echo timestamp >stamp-h])
