#! /bin/sh
dnl Process this file with autoconf to produce a configure script.
AC_INIT(src/mosaic.h)
AC_CONFIG_HEADER(config.h)

AC_DEFINE(PRERELEASE)
AC_DEFINE(XMOSAIC)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_RANLIB
AC_CHECK_PROG(AR, ar, ar)


AC_MSG_CHECKING(for machine dependent stuff)
echo ""
dnl Get system information and then set some flags based on that info
AC_CANONICAL_HOST

machine=''
case "${host}" in
    *-linux* )
	machine=linux
	AC_DEFINE(LINUX)
	LIBS="-L/usr/X11R6/lib ${LIBS}"
    ;;

    alpha-*-* )
	machine=alpha
    ;;

    *-bsdi* )
    ;;

    *-dec-ultrix* )
	machine=dec
	AC_DEFINE(BROKEN_MOTIF)
    ;;

    *-dg-dgux* )
	machine=dgux
	AC_DEFINE(DGUX)
	AC_DEFINE(SVR4)
    ;;
	
    *-hp-hpux* )
	machine=hp
	AC_DEFINE(_HPUX_SOURCE)
	syslibs=-Aa 
    ;;

    *-ibm-aix* )
	machine=ibm
	AC_DEFINE(_BSD)
    ;;

    *-sgi-irix5* )
	machine=sgi
	AC_DEFINE(MO_IRIX5)
    ;;

    sparc-sun-solaris23 | m68*-sun-solaris23 )
	machine=solaris
	AC_DEFINE(SVR4)
	AC_DEFINE(SOLARIS23)
    ;;

    sparc-sun-solaris24 | m68-sun-solaris24 )
	machine=solaris
	AC_DEFINE(SVR4)
        AC_DEFINE(SOLARIS24)
    ;;
   
    i[345]86-sun-solaris-2* )
	machine=solaris
	AC_DEFINE(SVR4)
	AC_DEFINE(SOLARIS24X86)
    ;;

    *-sun-sunos* )
	machine=sun
	AC_DEFINE(SUN)
	AC_DEFINE(SVR4)
	AC_DEFINE(BROKEN_MOTIF)
    ;;

    *-next-* )
	machine=next
	AC_DEFINE(NEXT)
    ;;
    	
esac

dnl Checks for header files.
AC_PATH_X
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(fcntl.h limits.h sys/file.h sys/ioctl.h sys/time.h termio.h unistd.h)

dnl Check for regular libs
AC_CHECK_LIB(m, pow)
AC_CHECK_LIB(nsl, gethostname)
AC_CHECK_LIB(gen, regex)
AC_CHECK_LIB(socket, getsockopt) 
AC_CHECK_LIB(PW, Freeend)
AC_CHECK_LIB(ipc, ipc_chains)

dnl Checks for X11 libraries.
AC_CHECK_LIB(X11, XSendEvent)
AC_CHECK_LIB(Xpm, _XpmFreeXpmImage)
AC_CHECK_LIB(ICE, IceProcessMessages)
AC_CHECK_LIB(SM, SmcCloseConnection)
AC_CHECK_LIB(Xext, XShapeQueryExtension)
AC_CHECK_LIB(Xt, XtAppMainLoop)
AC_CHECK_LIB(Xmu, _XEditResCheckMessages)
AC_CHECK_LIB(Xm, XmCreatePushButton, [
AC_DEFINE(MOTIF) 
LIBS="-lXm ${LIBS}"
])

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
AC_TYPE_UID_T

dnl Checks for library functions.
AC_TYPE_GETGROUPS
AC_PROG_GCC_TRADITIONAL
AC_TYPE_SIGNAL
AC_FUNC_VFORK
AC_FUNC_VPRINTF
AC_FUNC_WAIT3
AC_CHECK_FUNCS(gethostname gethostbyname mkdir socket strftime strstr uname)

dnl Now we try to set up all the external software mosaic uses
dnl as smartly as possible. 
AC_MSG_CHECKING(for external software)
echo ""

AC_MSG_CHECKING(for jpeg software)
AC_ARG_WITH(jpegdir,
[  --with-jpegdir=DIR   the directory where the built jpeg library resides ],
[
dnl We got a dir make sure we got the header and library

if test -d ${withval} ; then
    if test -r ${withval}/libjpeg.a ; then
       LIBS="${withval}/libjpeg.a ${LIBS}"
    else
       if test -r ${withval}/lib/libjpeg.a ; then 
         LIBS="${withval}/lib/libjpeg.a ${LIBS}"
       else
	 AC_MSG_ERROR(Could not find libjpeg.a in ${withval})
       fi
    fi

    if test -r ${withval}/jpeglib.h ; then
	CPPFLAGS="${CPPFLAGS} -I${withval}"
    else
	if test -r ${withval}/include/jpeglib.h ; then
	    CPPFLAGS="${CPPFLAGS} -I${withval}/include"
	else
	    AC_MSG_ERROR(Could not find jpeglib.h in ${withval})
	fi
    fi
else
    AC_MSG_RESULT(not)
    AC_MSG_ERROR(Couldn't find the specified jpeg dir: ${withval})
fi
dnl Now we think we got it so lets check 
    AC_TRY_LINK([#include<stdio.h>
		 #include "jpeglib.h"], 
		 [jpeg_read_raw_data(NULL, NULL, 0)], 
		 [AC_DEFINE(HAVE_JPEG)
		  AC_MSG_RESULT(found)],
		 [AC_MSG_RESULT(not found)
		  AC_MSG_ERROR(Couldn't find jpeg stuff in ${withval})])
],
[
    echo ""
    echo "Uh oh, you didn't include jpeg.  Doing this will greatly"
    echo "increase your surfing pleasure.  You can get it from these places:"
    echo "ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6a.tar.gz"
    echo "ftp://ftp.cs.wisc.edu/pub/ghost/jpegsrc.v6a.tar.gz"
    echo ""
    echo "If you go get it and build it, putting it into mosaic"
    echo "is as simple as doing this: "
    echo "./configure --with-jpegdir=/dir/where/you/installed/jpeg"
    echo ""
    echo "or take a look at the config script and edit the jpegdir field."
    echo "Then you just run:"
    echo "./config"
    echo ""
    echo "Would you like to configure to stop now so you can go get jpeg?"
    echo "(yes or no)" 
    read ans 

    case "${ans}" in 
        "y" | "ye" | "yes") 
        exit 
        ;;
    esac
])

dnl Before we check for png lets make sure we can find libz
AC_MSG_CHECKING(for libz)

dnl We need libz after libpng in the link line but we have to check it
dnl before we check libpng, this variable does the magic
lib_save=${LIBS}

AC_ARG_WITH(zdir,
[  --with-zdir=DIR   the directory where the built z library resides ],
[
dnl We got a dir make sure we got the library
if test -d ${withval} ; then
    if test -r ${withval}/libz.a ; then
       LIBS="${withval}/libz.a ${LIBS}"
       libz="${withval}/libz.a"
    else
       if test -r ${withval}/lib/libz.a ; then 
         LIBS="${withval}/lib/libz.a ${LIBS}"
	 libz="${withval}/lib/libz.a"
       else
	 AC_MSG_ERROR(Could not find libz.a in ${withval})
       fi
    fi
else
    AC_MSG_ERROR(Couldn't find the specified zlib dir: ${withval})
fi
dnl Now we think we got it so lets check 
   AC_TRY_LINK([#include<stdio.h>],
		 [inflate();], 
		 [AC_DEFINE(HAVE_PNG)
		  AC_MSG_RESULT(got it)
		  have_z="yes"],
		 [AC_MSG_RESULT(not found)
		  AC_MSG_ERROR(Couldn't find png stuff in ${withval})])

],
[
AC_MSG_RESULT(not)
])

LIBS=${lib_save}

if test "${have_z}" = "yes" ; then
AC_MSG_CHECKING(for png)
AC_ARG_WITH(pngdir,
    [  --with-pngdir=DIR   the directory where the built png library resides ],
    [
dnl We got a dir make sure we got the header and library
    if test -d ${withval} ; then
	if test -r ${withval}/libpng.a ; then
	    LIBS="${withval}/libpng.a ${libz} ${LIBS}"
	else
	    if test -r ${withval}/lib/libpng.a ; then 
		LIBS="${withval}/lib/libpng.a ${libz} ${LIBS}"
	    else
		AC_MSG_ERROR(Could not find libpng.a in ${withval})
	    fi
	fi

	if test -r ${withval}/png.h ; then
	    CPPFLAGS="$CPPFLAGS -I${withval}"
	else
	    if test -r ${withval}/include/png.h ; then
		CPPFLAGS="$CPPFLAGS -I${withval}/include"
	    else
		AC_MSG_ERROR(Could not find png.h in ${withval})
	    fi
	fi
    else
	AC_MSG_RESULT(not found)
	AC_MSG_ERROR(Couldn't find the specified png dir: ${withval})
    fi
dnl Now we think we got it so lets check 
   AC_TRY_LINK([#include<stdio.h>
		 #include "png.h"], 
		 [png_read_data(NULL, NULL, 0)], 
		 [AC_DEFINE(HAVE_PNG)
		  AC_MSG_RESULT(got it)
		  have_png="yes"],
		 [AC_MSG_RESULT(not found)
		  AC_MSG_ERROR(Couldn't find png stuff in ${withval})])
],
[
AC_MSG_RESULT(not found)
have_png='no'
])
else
have_png='no'
fi

if test "${have_png}" = "no" ; then 
    echo ""
    echo "Uh oh, you didn't include png.  This will allow Mosaic to "
    echo "view png images.  You can get it here:"
    echo "ftp://ftp.uu.net/graphics/png/src"
    echo "Before you run out and get it you will also need libz which can be"
    echo "found in the same place."
    echo ""
    echo "If you go get and build these things, putting png support into mosaic"
    echo "is as simple as doing this: "
    echo "./configure --with-pngdir=/where/you/installed/png --with-zdir=/where/you/installed/zlib"
    echo ""
    echo "Or you can just fill in the appropriate fields in the config script"
    echo "and to this:"
    echo "./config"
    echo ""
    echo "Would you like to configure to stop now so you can go get png/zlib?"
    echo "(yes or no)" 

    read ans 

    case "${ans}" in
	"y" | "ye" | "yes")
	exit
	;;
    esac    
fi


dnl AC_ARG_WITH(waisdir,
dnl [  --with-waisdir=DIR   the directory where the built wais library resides dnl ],
dnl [  ],
dnl [ ])

dnl AC_ARG_WITH(hdfdir,
dnl [  --with-hdfdir=DIR    the directory where the built hdf library resides ],
dnl [  ],
dnl [ ])


dnl dtm support switches
AC_ARG_ENABLE(dtm,
[  --enable-dtm    Used to enable dtm support ],
[ 
if test "${withval}" = "yes" ; then
    dtmdirs='libdtm libnet'
else
    dtmdirs=''
fi
],
[ dtmdirs='' ])


dnl Figure out what version of Motif we have (XMosaic sucks)
dnl AC_TRY_LINK(
dnl [
dnl ],
dnl [
dnl ],
dnl [ 
dnl It worked
dnl     AC_DEFINE(MOTIF2_0)
dnl ],
dnl [
    AC_TRY_LINK(
	[
	    #include<XmStrDefs.h>
	],
	[
	    int dummy=XmNpositionIndex;
	],
	[
	    AC_DEFINE(MOTIF1_2)
	],
	[
	    AC_DEFINE(MOTIF1_1)
	])
dnl ])

AC_SUBST(dtmdirs)

for i in libwww2 libhtmlw src libnut libXmx libdtm libnet
do
    cd ${i}
    if test -f Makefile ; then
	cp Makefile Makefile.bak
	echo "cp Makefile Makefile.bak"
    fi
    cd ..
done

AC_OUTPUT(Makefile src/Makefile libhtmlw/Makefile libnut/Makefile libwww2/Makefile libXmx/Makefile)