
AC_PREREQ(2.13)
AC_INIT()

VERSION=1.1.5
PACKAGE=zoom

UTIL_DISPLAY_INIT()
UTIL_DISPLAY_HEADER(Andrew Hunter, 2000-2011)

UTIL_DISPLAY_SECTION(automake)
AM_INIT_AUTOMAKE($PACKAGE, $VERSION)
AM_CONFIG_HEADER(config.h)

UTIL_DISPLAY_SECTION(compilers)
AC_PROG_CC
AC_PROG_CPP
AC_PROG_RANLIB
AM_PROG_LEX
AC_PROG_YACC
AC_PATH_PROG(PERL, perl, noperl)

OBJC="$CC -c"
AC_SUBST(OBJC)

if test "$PERL" = "noperl"; then
  AC_MSG_ERROR([You must have a copy of perl installed to compile Zoom])
fi

ACLOCAL="$ACLOCAL -I m4"

UTIL_DISPLAY_SECTION(compiler characteristics)

AC_C_INLINE
UTIL_CHECK_CFLAG(Wall)
UTIL_CHECK_CFLAG(finline-functions)
UTIL_CHECK_CFLAG(funroll-loops)
UTIL_CHECK_CFLAG(fomit-frame-pointer)

AC_ARG_ENABLE([new-terp], 
[  --enable-new-terp       Enable alternative interpreter (insignificant 
                          performance hit, faster compile times, performance 
                          boost on some architectures) ],
[
UTIL_CHECK_CFLAG(fno-gcse)
AC_MSG_CHECKING([for computed gotos])
AC_TRY_LINK([], 
	[ 
	  void *foo[] = { &&bar, &&baz };

	bar:
	  goto *foo[0];
	baz:
	  goto *foo[1];
	],
	[ 
	  AC_MSG_RESULT(yes) 
	  AC_DEFINE(HAVE_COMPUTED_GOTOS)
	],
	[ AC_MSG_RESULT(no) ])
])

UTIL_DISPLAY_SECTION(architecture characteristics)

AC_CHECK_SIZEOF(unsigned char, 1)
AC_CHECK_SIZEOF(short int,     2)
AC_CHECK_SIZEOF(int,           4)
AC_CHECK_SIZEOF(long int,      4)

case 1 in
$ac_cv_sizeof_unsigned_char)
    ZBYTE_TYPE="unsigned char";;

$ac_cv_sizeof_short_int)
    ZBYTE_TYPE="unsigned short int";;

*)
    ZBYTE_TYPE="unsigned char"
esac

case 2 in
$ac_cv_sizeof_short_int)
    ZWORD_TYPE="signed short int"
    ZUWORD_TYPE="unsigned short int";;

$ac_cv_sizeof_int)
    ZWORD_TYPE="signed int"
    ZUWORD_TYPE="unsigned int";;

*)
    ZWORD_TYPE="signed short int"
esac

case 4 in
$ac_cv_sizeof_int)
    ZDWORD_TYPE="int";;

$ac_cv_sizeof_long_int)
    ZDWORD_TYPE="long int";;

*)
    ZDWORD_TYPE="long int"
esac

AC_SUBST(ZBYTE_TYPE)
AC_SUBST(ZWORD_TYPE)
AC_SUBST(ZUWORD_TYPE)
AC_SUBST(ZDWORD_TYPE)

AC_MSG_CHECKING(byte ordering of $ZWORD_TYPE)
AC_TRY_RUN([
  int main()
    {
      union { $ZWORD_TYPE word; $ZBYTE_TYPE byte[2]; } foo;

      foo.byte[0] = 1;
      foo.byte[1] = 2;

      if (foo.word == 0x102)
        return 0;
      else
        return 1;
    }
  ],
  AC_MSG_RESULT([zmachine standard])
  ZMACHINE_CONV_WORD=0,
  AC_MSG_RESULT([backwards])
  ZMACHINE_CONV_WORD=1,
  ZMACHINE_CONV_WORD=1
  AC_MSG_RESULT([unknown - assuming is backwards])
)

AC_SUBST(ZMACHINE_CONV_WORD)

UTIL_DISPLAY_SECTION(features)

AC_CYGWIN

if test "$CYGWIN" != "yes"; then
  MINGW32=yes
  mingw_prefix=`echo $CC | sed "s/\(.*\)gcc/\1/"`
  AC_PATH_PROG(MINGWWINDRES, "$mingw_prefix"windres)
  AC_MSG_CHECKING([if we're cross-compiling using mingw32])

  ac_OLD_CFLAGS="$CFLAGS"
  CFLAGS="$CFLAGS -mwindows"
  AC_TRY_LINK([], [ { int x; x = 1; } ],
      , MINGW32=no)
  CFLAGS="$CFLAGS -mno-cygwin"
  AC_TRY_LINK([], [ { int x; x = 1; } ],
      , MINGW32=no)

  AC_TRY_LINK([#include <windows.h>],
              [ { MessageBox(NULL, "Arg", "Whoops", MB_OK | MB_SYSTEMMODAL); } ],
	      , MINGW32=no)
  AC_MSG_RESULT($MINGW32)
  CFLAGS=$ac_OLD_CFLAGS

  if test "$MINGW32" = "yes"; then
    UTIL_CHECK_CFLAG(mwindows)
    UTIL_CHECK_LDFLAG(mwindows)
    UTIL_CHECK_CFLAG(mno-cygwin)
    UTIL_CHECK_LDFLAG(mno-cygwin)  
    AC_PATH_PROG(WINDRES, "$mingw_prefix"windres)

    RESEXTN=o
    AC_SUBST(RESEXTN)

    AM_CONDITIONAL(WINDOWS_VERSION, true)
    AM_CONDITIONAL(CREATE_EXE, true)
    AM_CONDITIONAL(CARBON_VERSION, false)

    WINDOW_SYSTEM=2
    AC_DEFINE(WINDOW_SYSTEM, 2)
  else
    AM_CONDITIONAL(CREATE_EXE, false)
  fi
else
  AM_CONDITIONAL(CREATE_EXE, false)
fi

if test "$CYGWIN" = "yes"; then
  UTIL_CHECK_CFLAG(mwindows)
  UTIL_CHECK_LDFLAG(mwindows)
  UTIL_CHECK_CFLAG(mno-cygwin)
  UTIL_CHECK_LDFLAG(mno-cygwin)
  AC_PATH_PROG(WINDRES, windres)
  RESEXTN=o
  AC_SUBST(RESEXTN)

  AM_CONDITIONAL(WINDOWS_VERSION, true)
  AM_CONDITIONAL(CARBON_VERSION, false)

  WINDOW_SYSTEM=2
  AC_DEFINE(WINDOW_SYSTEM, 2)
else
if test "$MINGW32" != "yes"; then
  CARBON_DETECT
  if test "$carbon_present" = "no"; then
    AC_PATH_XTRA
    if test "$no_x" = "yes"; then
      AC_MSG_ERROR([No supported configurations found])
    else
      WINDOW_SYSTEM=1
      AC_DEFINE(WINDOW_SYSTEM, 1)

      AM_CONDITIONAL(WINDOWS_VERSION, false)
      AM_CONDITIONAL(CARBON_VERSION, false)
    fi
  else
    WINDOW_SYSTEM=3
    AC_DEFINE(WINDOW_SYSTEM, 3)
    AM_CONDITIONAL(WINDOWS_VERSION, false)
    AM_CONDITIONAL(CARBON_VERSION, true)

    QUICKTIME_DETECT

    if test "x$quicktime_present" = "xno"; then
      AC_MSG_ERROR([QuickTime not found (?)])
    fi

    AC_CHECK_LIB(z, inflate, [ LIBS="$LIBS -lz" ])

    AC_CHECK_LIB(png, png_get_IHDR,
      [
	AC_DEFINE(HAVE_LIBPNG)
	LIBS="$LIBS -lpng"
      ],
      [
	AC_MSG_WARN([libpng not detected: blecherous QuickTime alpha blending will be used (Ew)])
      ])
  fi
fi
fi

if test "$WINDOW_SYSTEM" = "2"; then
  AC_MSG_CHECKING(that we can link to various windows libraries)
  ac_OLD_LIBS=$LIBS
  LIBS="$LIBS -lcomctl32 -lcomdlg32"
  AC_TRY_LINK([#include <windows.h>
               #include <commctrl.h>
	       #include <commdlg.h>],
	      [ {
	          InitCommonControls();
		}
	      ],
	      AC_MSG_RESULT(woohoo!)
	      ,
	      AC_MSG_RESULT(doh!)
	      AC_WARN([Unable to link to common controls library - you may have to edit the makefile by hand])
	      LIBS=$ac_OLD_LIBS)
fi

AC_HEADER_STDC
AC_CHECK_HEADERS(unistd.h)
AC_CHECK_HEADERS(sys/time.h)

AC_MSG_CHECKING([for gettimeofday])
AC_TRY_LINK(
  [
  	#include <stdio.h>
  	#include <stdlib.h>
  	#include <sys/time.h>
  ],
  [{struct timeval tv; gettimeofday(&tv, NULL);}],
  AC_MSG_RESULT([yes])
  AC_DEFINE(HAVE_GETTIMEOFDAY),
  AC_MSG_RESULT([no]))

AC_MSG_CHECKING([for argp])

AC_TRY_LINK([
  #include <argp.h>
],
[
  argp_parse(NULL, 0, NULL, 0, 0, NULL);
],
[
  AC_MSG_RESULT(yes)
  AC_DEFINE(OPT_TYPE, 0)
],
  AC_MSG_RESULT(no)
  AC_MSG_CHECKING([for getopt])
  AC_TRY_LINK([
    #include <unistd.h>
  ],
  [
    getopt(0, NULL, NULL);
  ],
  [
    AC_MSG_RESULT([yes])
    AC_DEFINE(OPT_TYPE, 1)
  ],
  [
    AC_MSG_RESULT(no)
    AC_DEFINE(OPT_TYPE, 200)
  ])
)

#AC_CHECK_HEADERS([expat.h],,AC_MSG_ERROR([expat is required to compile Zoom]))
#AC_CHECK_LIB(expat, XML_ParserReset,,AC_MSG_ERROR([expat is required to compile Zoom]))

if test "$cross_compiling" = "yes"; then
  UTIL_DISPLAY_SECTION([native gcc (as we're X-compiling)])
  AC_PATH_PROG(BUILDERCC, gcc)
else
  BUILDERCC=$CC
fi

AC_SUBST(BUILDERCC)

HAVE_T1LIB=0
if test "$WINDOW_SYSTEM" = "1"; then
    LIBS="$LIBS $X_PRE_LIBS $X_LIBS $X_EXTRA_LIBS"
    CFLAGS="$CFLAGS $X_CFLAGS"
    LIBS="$LIBS -lX11"

    AC_CHECK_LIB(m, sin)
    AC_CHECK_LIB(t1, T1_InitLib, 
      [
        LIBS="$LIBS -lt1"
        AC_CHECK_LIB(t1x, T1_SetCharX,
	  [ LIBS="$LIBS -lt1x"; HAVE_T1LIB=1 ])
      ])

    if test "$HAVE_T1LIB" = "1"; then
        AC_MSG_CHECKING([that we can compile a T1Lib program])
	AC_TRY_LINK(
	  [ #include <t1lib.h> ],
	  [ T1_InitLib(0); ],
	  [
	    AC_MSG_RESULT(yes)
	    AC_DEFINE(HAVE_T1LIB)
	  ],
	  [
	    AC_MSG_RESULT([no])
	  ])
    fi

    AC_CHECK_LIB(Xext, XdbeQueryExtension, [ LIBS="$LIBS -lXext" ])
    AC_MSG_CHECKING([for the DBE X extension library])
    AC_TRY_LINK(
      [ #include <X11/Xlib.h>
        #include <X11/Xatom.h>
	#include <X11/Xutil.h>
	#include <X11/Xresource.h>
        #include <X11/extensions/Xdbe.h> ],
      [ XdbeQueryExtension(NULL, NULL, NULL); ],
      [
        AC_MSG_RESULT(yes)
	AC_DEFINE(HAVE_XDBE)
      ],
      [
        AC_MSG_RESULT(no)
      ])
      
    # If Xft is installed, then there will be a xft-config file on the current path
    AC_MSG_CHECKING([for xft-config])
    XFTCONFIG=`which xft-config`
    XFT_CFLAGS=""
    XFT_LIBS=""
    if test -x ${XFTCONFIG}; then
    	AC_MSG_RESULT([$XFTCONFIG])
    	XFT_CFLAGS=`${XFTCONFIG} --cflags`
    	XFT_LIBS=`${XFTCONFIG} --libs`
    else
    	AC_MSG_RESULT(no)
    fi

    # Had to move this, Xft 2 again
    AC_CHECK_LIB(Xrender, XRenderQueryExtension,
      [
        LIBS="$LIBS -lXrender"
	HAVE_XRENDER=1
      ])
    if test "$HAVE_XRENDER" = "1"; then
      AC_MSG_CHECKING([that we can compile an Xrender program])
      AC_TRY_LINK(
        [ #include <X11/extensions/Xrender.h> ],
	[ XRenderCreatePicture(NULL, None, NULL, 0, NULL); ],
	[ 
	  AC_MSG_RESULT(yes)
	  AC_DEFINE(HAVE_XRENDER)
	],
	[
	  AC_MSG_RESULT(no)
	])
    fi

    # Xft 2 requires that we link to fontconfig, too
    # Grr, I thought dependency hell was supposed to be a Windows thing.
    AC_CHECK_LIB(fontconfig, FcPatternAddInteger)

    AC_CHECK_LIB(Xft, XftInit,
      [
        LIBS="$LIBS $XFT_LIBS"
        CFLAGS="$CFLAGS $XFT_CFLAGS"
	HAVE_XFT=1
      ])
    if test "$HAVE_XFT" = "1"; then
      AC_MSG_CHECKING([that we can compile an Xft program])
      AC_TRY_LINK(
        [ #include <X11/Xft/Xft.h> ],
	[ XftDrawCreate(NULL, 0, NULL, 0); ],
	[ 
	  AC_MSG_RESULT(yes)
	  AC_DEFINE(HAVE_XFT)
	],
	[
	  AC_MSG_RESULT(no)
	])
    fi

    AC_CHECK_LIB(z, inflate, [ LIBS="$LIBS -lz" ])
    AC_CHECK_LIB(png, png_get_IHDR,
      [
	AC_DEFINE(HAVE_LIBPNG)
	LIBS="$LIBS -lpng"
      ],
      [
	AC_MSG_WARN([libpng not detected: image support will be disabled])
      ])
fi

UTIL_DISPLAY_INFO(Generating files)

AC_OUTPUT([
  Makefile
  m4/Makefile
  src/Makefile
  src/ztypes.h
  src/zoom.rc
  builder/Makefile
  manual/Makefile
  bonus/Makefile])
