#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
# (c) 2003-2020 Jaroslav Fojtik

AC_PREREQ(2.57)
AC_INIT(WP2LaTeX, 3.91, fojtik@penguin.cz)
AC_CONFIG_SRCDIR([sources.cc/charactr.cc])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
#AC_PROG_INSTALL
AC_PROG_MAKE_SET

# Disable JPEG.
AC_ARG_WITH(jpeg,
	      [  --without-jpeg          disable JPEG support],
	      [with_jpeg=$withval],
	      [with_jpeg='yes'])
# Disable JPEG.
AC_ARG_WITH(png,
	      [  --without-png           disable PNG support],
	      [with_png=$withval],
	      [with_png='yes'])
# Disable LibIntl.
AC_ARG_WITH(libintl,
	      [  --without-libintl       disable LibIntl support],
	      [with_libintl=$withval],
	      [with_libintl='yes'])

AC_ARG_ENABLE(profile,
    [  --enable-profile    Turn on profile ],
     [case "${enableval}" in
       yes) PROFILE_CFLAGS="-pg -fprofile-arcs -ftest-coverage" ;;
       no)  PROFILE_CFLAGS="" ;;
       *) AC_MSG_ERROR(bad value ${enableval} for --enable-profile) ;;
     esac],[PROFILE_CFLAGS=""])

AC_ARG_ENABLE(extra-optimization,
    [  --enable-extra-optimization    Turn on extra optimizations (speed, size, no)],
     [case "${enableval}" in
       yes) OPTIMIZE_CFLAGS="-O3" ;;
       speed) OPTIMIZE_CFLAGS="-O3" ;;
       no)  OPTIMIZE_CFLAGS="-O2" ;;
       size) OPTIMIZE_CFLAGS="-Os" ;;
       *) AC_MSG_ERROR(bad value ${enableval} for --enable-extra-optimization) ;;
     esac],[OPTIMIZE_CFLAGS="-O2"])
AC_ARG_ENABLE(optimization,
    [  --enable-optimization    Turn on extra optimizations (speed, size, no)],
     [case "${enableval}" in
       yes) OPTIMIZE_CFLAGS="-O3" ;;
       speed) OPTIMIZE_CFLAGS="-O3" ;;
       no)  OPTIMIZE_CFLAGS="-O2" ;;
       size) OPTIMIZE_CFLAGS="-Os" ;;
       *) AC_MSG_ERROR(bad value ${enableval} for --enable-optimization) ;;
     esac],[])

AC_ARG_ENABLE(debug,
    [  --enable-debug    Enable generation of debug build ],
     [case "${enableval}" in
       yes) OPTIMIZE_CFLAGS="-O0"
            DEBUG_FLAGS="-DDEBUG -g -ggdb" ;;
       no)  DEBUG_FLAGS="" ;;
       *) AC_MSG_ERROR(bad value ${enableval} for --enable-debug) ;;
     esac],[DEBUG_FLAGS=""])
AC_SUBST(DEBUG_FLAGS)

AC_SUBST(OPTIMIZE_CFLAGS)


AC_ARG_ENABLE(debug,
    [  --disable-latex --enable-latex    Disable / enable LaTeX use during testing ],
     [case "${enableval}" in
       yes) TEST_LTX='testltx' ;;
       no)  TEST_LTX='testconv' ;;
       *) AC_MSG_ERROR(bad value ${enableval} for --enable-latex) ;;
     esac],[TEST_LTX='check'])


# Checks for libraries.
AC_MSG_CHECKING(if -lm is necessary)
AC_TRY_LINK_FUNC(sin,
   echo "link OK - not needed",
   [AC_CHECK_LIB([m],sin,LIBS="-lm $LIBS",,)])

# FIXME: Replace `main' with a function in `-lstdcpp':
#AC_CHECK_LIB([stdcpp], [main])

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([limits.h stddef.h stdlib.h string.h strings.h unistd.h values.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_MEMCMP
AC_HEADER_MAJOR
AC_FUNC_REALLOC
AC_TYPE_SIGNAL
AC_CHECK_FUNCS([memmove memset mkdir putenv setlocale strchr strdup strstr])


#Check if compiler accepts -fno-rtti
WINE_TRY_CFLAGS([-fno-rtti],support_fnortti='yes',support_fnortti='no')
#echo $support_fnortti
if test "$support_fnortti" != 'no'
then
  CXXFLAGS="$CXXFLAGS -fno-rtti"
fi

#Check if compiler accepts -fno-exceptions
WINE_TRY_CFLAGS([-fno-exceptions],support_fnoexceptions='yes',support_fnoexceptions='no')
#echo $support_fnoexceptions
if test "$support_fnoexceptions" != 'no' 
then
  CXXFLAGS="$CXXFLAGS -fno-exceptions"
fi


AC_MSG_CHECKING(if strict const convention must be used)
AC_LANG_PUSH(C++)
AC_COMPILE_IFELSE( [ AC_LANG_SOURCE(
                   [[
                   #include <string.h> 
                   const char *StrChr(const char *s, int c)
                   {
                     if(s==NULL) return(NULL);
                     return(strchr(s,c));
                   }

                   char *StrChr(char *s, int c)
                   {
                     if(s==NULL) return(NULL);
                     return(strchr(s,c));
                   }
                   ]])],
                    [
                      AC_MSG_RESULT([yes])
                      STRICT_CONST_PROC='#define __Strict_Const_Procs'
                    ],
                    [
                      AC_MSG_RESULT([no])
                      STRICT_CONST_PROC=''
                    ]
                 )
AC_LANG_POP(C++)

AC_SUBST(STRICT_CONST_PROC)


####### Check for JPEG ######
have_jpeg='no'
LIB_JPEG=''
if test "$with_jpeg" == 'xno' ; then
   with_jpeg = 'no'
fi
if test "$with_jpeg" != 'no'
then
    AC_MSG_CHECKING(for JPEG support )
    AC_MSG_RESULT()
    failed=0;
    passed=0;
    AC_CHECK_HEADER(jconfig.h,passed=`expr $passed + 1`,failed=`expr $failed + 1`)
    AC_CHECK_HEADER(jmorecfg.h,passed=`expr $passed + 1`,failed=`expr $failed + 1`)
    AC_CHECK_HEADER(jpeglib.h,passed=`expr $passed + 1`,failed=`expr $failed + 1`)
    AC_CHECK_LIB(jpeg,jpeg_read_header,passed=`expr $passed + 1`,failed=`expr $failed + 1`,)

    AC_MSG_CHECKING(if JPEG package is complete)
    if test $passed -gt 0
    then
      if test $failed -gt 0
      then
 	AC_MSG_RESULT(no -- some components failed test)
        have_jpeg='no (failed tests)'
        with_jpeg='no'
      else
	LIB_JPEG='-ljpeg'
	LIBS="$LIB_JPEG $LIBS"
	AC_DEFINE(HasJPEG,1,Define if you have JPEG library)
	AC_MSG_RESULT(yes)
        have_jpeg='yes'
      fi
    else
        AC_MSG_RESULT(no)
        with_jpeg='no'
    fi
fi
#AM_CONDITIONAL(HasJPEG, test "$have_jpeg" = 'yes')
if test "$with_jpeg" == 'no'
then
 CXXFLAGS="$CXXFLAGS -DSupportJPG=0"
fi
AC_SUBST(LIB_JPEG)


####### Check for PNG ######
have_png='no'
LIB_PNG=''
if test "$with_png" == 'xno' ; then
   with_png = 'no'
fi
if test "$with_png" != 'no'
then
    AC_MSG_CHECKING(for PNG support )
    AC_MSG_RESULT()
    failed=0;
    passed=0;

    AC_CHECK_HEADER(png.h,passed=`expr $passed + 1`,failed=`expr $failed + 1`,)
    AC_CHECK_LIB(z,inflate,passed=`expr $passed + 1`,failed=`expr $failed + 1`,)

    if test $passed -gt 0; then
     LIBS_BACKUP="$LIBS"
     LIBS="$LIBS -lz"
     AC_CHECK_LIB(png,png_sig_cmp,passed=`expr $passed + 1`,failed=`expr $failed + 1`,)
     LIBS="$LIBS_BACKUP"
    fi

    AC_MSG_CHECKING(if PNG package is complete)
    if test $passed -gt 0; then
      if test $failed -gt 0; then
	AC_MSG_RESULT(no -- some components failed test)
        have_png="no (failed tests)"
      else
	LIB_PNG='-lpng -lz'
	LIBS="$LIB_PNG $LIBS"
	AC_DEFINE(HasPNG,1,Define if you have PNG library)
	AC_MSG_RESULT(yes)
        have_png='yes'
      fi
    else
      AC_MSG_RESULT(no)
    fi
fi
#AM_CONDITIONAL(HasPNG, test "$have_png" = 'yes')

if test $have_png == 'yes' ; then
  AC_MSG_CHECKING(Check for presence png_sizeof macro)
  AC_LINK_IFELSE(  [AC_LANG_PROGRAM([[#include <png.h>]],
                                     [[
                                     png_color colo;
                                     return(png_sizeof(colo));
                                     ]])],
                    [
                      AC_MSG_RESULT([yes])
                      have_png_sizeof='yes'
                    ],
                    [
                      AC_MSG_RESULT([no - fix added])
                      have_png_sizeof='no'
                    ]
                 )

  if test $have_png_sizeof == 'no' ; then
    OPTIMIZE_CFLAGS="$OPTIMIZE_CFLAGS -Dpng_sizeof=sizeof"
    AC_SUBST(OPTIMIZE_CFLAGS)
  fi
fi


AC_SUBST(LIB_PNG)

####### Check for Libintl ######
have_libintl='no'
LIB_INTL=''
GETTEXT=''
if test "$with_libintl" == 'xno' ; then
   with_libintl = 'no'
fi
if test "$with_libintl" != 'no'
then
    AC_MSG_CHECKING(for LibIntl support )
    AC_MSG_RESULT()
    failed=0;
    passed=0;

    AC_CHECK_HEADER(locale.h,passed=`expr $passed + 1`,failed=`expr $failed + 1`)
    AC_CHECK_HEADER(libintl.h,passed=`expr $passed + 1`,failed=`expr $failed + 1`)

    nolintl=0;
    AC_MSG_CHECKING(for LibIntl integrated in C_lib)
    AC_TRY_LINK_FUNC(textdomain,nolintl=0,nolintl=1)
    if test $nolintl -gt 0; then
      echo "no"
      AC_CHECK_LIB(intl,textdomain,LIB_INTL="-lintl",nolintl=2,)
      if test $nolintl -gt 1; then
        AC_CHECK_LIB(intl,bindtextdomain,LIB_INTL="-lintl -liconv",nolintl=3,-liconv)
        if test $nolintl -gt 2; then
          WL_PARAM="-Wl,-framework -Wl,CoreFoundation"
          # Cannot be called, because it is cached and another test with different arguments is skipped.
          # AC_CHECK_LIB(intl,bindtextdomain,LIB_INTL='-lintl $WL_PARAM',failed=`expr $failed + 1`,$WL_PARAM)
          AC_MSG_CHECKING([for LibIntl with options -lintl -liconv -Wl,-framework -Wl,CoreFoundation])
          ac_check_lib_save_LIBS=$LIBS
          LIBS="-lintl -liconv $WL_PARAM $LIBS"
          AC_TRY_LINK_FUNC(textdomain,LIB_INTL="-lintl -liconv $WL_PARAM",nolintl=4)
          LIBS=$ac_check_lib_save_LIBS
          if test $nolintl -gt 3; then
            failed=`expr $failed + 1`
            AC_MSG_RESULT(no)
          else
            AC_MSG_RESULT(yes)
          fi          
        fi
      fi
    else      
      AC_LINK_IFELSE([AC_LANG_PROGRAM(
             [[ #include<libintl.h> ]], [textdomain("libintl_test");])],
             nolintl=0,nolintl=1);
      if test $nolintl -gt 0; then
        echo "additional testing needed"
        LIB_INTL="-lintl"
      else
        echo "yes"
      fi 
    fi

    AC_MSG_CHECKING(if LibIntl package is complete)
    if test $passed -gt 0; then
      if test $failed -gt 0
      then
	AC_MSG_RESULT(no -- some components failed test)
        have_libintl='no (failed tests)'
      else
	LIBS="$LIB_INTL $LIBS"
	GETTEXT='-D__gettext__'
	AC_DEFINE(HasLibIntl,1,Define if you have LibIntl library)
	AC_MSG_RESULT(yes)
        have_libintl='yes'
      fi
    else
        AC_MSG_RESULT(no)
    fi
fi
#AM_CONDITIONAL(HasLibIntl, test "$have_libintl" = 'yes')
AC_SUBST(GETTEXT)


# Additional tools for libintl
if test "$with_libintl" != 'no'
then
  AC_MSG_CHECKING(for xgettext program working )
  xgettext -o ref_tmp.po ./doc/dump.c
  if test -f ref_tmp.po
  then
    rm ref_tmp.po
    SET_XGETTEXT='XGETTEXT:=xgettext'
    AC_MSG_RESULT(yes)
  else
    AC_MSG_RESULT(no)
    AC_MSG_CHECKING(whether xgettext needs -k_ )
    xgettext -k_ -o ref_tmp.po ./doc/dump.c
    if test -f ref_tmp.po
    then
      rm ref_tmp.po
      SET_XGETTEXT='XGETTEXT:=xgettext -k_'
      XGETTEXT_OPT='-k_'
      AC_MSG_RESULT(yes)
    else
      XGETTEXT_OPT=''
      AC_MSG_RESULT(no)
    fi

    # Try to check all sources, they contain different charsets.
    AC_MSG_CHECKING(whether xgettext needs --from-code=CP1250)
    xgettext $XGETTEXT_OPT -o ref_tmp.po ./sources.cc/*.cc &>/dev/null
    if test -f ref_tmp.po
    then
      rm ref_tmp.po
      AC_MSG_RESULT(no)
    else
      xgettext $XGETTEXT_OPT --from-code=CP1250 -o ref_tmp.po ./sources.cc/*.cc &>/dev/null
      if test -f ref_tmp.po
      then
        rm ref_tmp.po
        SET_XGETTEXT="$SET_XGETTEXT --from-code=CP1250"
        AC_MSG_RESULT(yes)
      else
        AC_MSG_RESULT(failed)
      fi
    fi
  fi
fi

AC_SUBST(SET_XGETTEXT)


####### Check for Memory Streams ######
have_memstreams='no'
MEMSTREAMS=''
if test "$with_memstreams" != 'no'
then
    failed=0;
    passed=0;

    AC_CHECK_FUNC(fmemopen,passed=`expr $passed + 1`,failed=`expr $failed + 1`)
    AC_CHECK_FUNC(open_memstream,passed=`expr $passed + 1`,failed=`expr $failed + 1`)

    if test $failed -lt 1
    then
     AC_LANG(C++)
     AC_TRY_LINK([#include <stdio.h>],
	[
	char *bp_strip=NULL;
	size_t size_strip;
	FILE *F;
	F=open_memstream(&bp_strip, &size_strip);
	],
	passed=`expr $passed + 1`,failed=`expr $failed + 1`)
    fi

    AC_MSG_CHECKING(for memory streams support )
    if test $passed -gt 0
    then
    if test $failed -gt 0
    then
	AC_MSG_RESULT(no -- some components failed test)
        have_memstreams='no (failed tests)'
    else
	AC_DEFINE(Hasmemstreams,1,Define if you have memory streams)
	AC_MSG_RESULT(yes)
	MEMSTREAMS='#define _has_MemStreams'
        have_memstreams='yes'
    fi
    else
        AC_MSG_RESULT(no)
    fi
fi
#AM_CONDITIONAL(HasLibIntl, test "$have_libintl" = 'yes')
AC_SUBST(MEMSTREAMS)


####### Check for multithreading ######
#AC_MSG_CHECKING(Multithreading support )
AC_CHECK_HEADER(pthread.h, multithreading='yes', multithreading='no')

if test "$multithreading" == 'no' 
then
#  AC_MSG_RESULT(no)
  USE_JOBS=""
else
#  AC_MSG_RESULT(yes)
  CXXFLAGS="$CXXFLAGS -D_REENTRANT"
  CFLAGS="$CFLAGS -D_REENTRANT"
  USE_JOBS="OBJECTS+=jobs/jobs\$(OBJ)"
  LIBS="$LIBS -lpthread"  
fi

if test "$USE_JOBS" != "" 
then
 AC_MSG_CHECKING(if -lrt is necessary)
 AC_TRY_LINK_FUNC(clock_gettime, echo "link OK - not needed",
    [AC_CHECK_LIB([rt],clock_gettime,LIBS="-lrt $LIBS",USE_JOBS="",)])
fi

AC_SUBST(USE_JOBS)


####### Check default arg in friend function ######
default_arg_flaw='no'

AC_MSG_CHECKING(whether friend function could have a default arg )

AC_LANG(C++)
AC_TRY_COMPILE(
    [
	class MyClassNo1 {
	public:   MyClassNo1(unsigned len);

	friend int MyFriendFunc(const MyClassNo1 & s, char c='1');
	};
    ],
    [],
    default_arg_flaw='no',
    default_arg_flaw='yes')

if test "$default_arg_flaw" == 'yes' 
then
  CXXFLAGS="$CXXFLAGS -fpermissive"
  echo "no - fix -fpermissive used"
else
  echo "yes"
fi


####### Check Ellipsis expansion ######
elipsis_expand_short='no'
elipsis_deref_vararg='no'
ELLIPSIS_EXPAND_SHORT=''

  AC_LANG(C)
  AC_TRY_COMPILE(
    [#include <stdarg.h>
    int savestruct(char description, ...)
	{
        va_list ap;
        int writed=0;
        va_start(ap, description);
        if(description>1) writed+=va_arg(ap,char);
	if(description>2) writed+=va_arg(ap,short int);
	if(description>3) writed+=va_arg(ap,int);             
        }   	
    ],
    [],
    elipsis_expand_short='no',
    elipsis_expand_short='yes')
    
    AC_TRY_COMPILE(
    [#include <stdarg.h>
    int savestruct(char description, ...)
	{
        va_list ap;
        char *c;
        va_start(ap, description);
        if(description=='c')
        c = &va_arg(ap,char);
        }
    ],
    [],
    elipsis_deref_vararg='yes',
    elipsis_deref_vararg='no' 
    )

  AC_MSG_CHECKING(whether ellipsis expand short )
  if test "$elipsis_expand_short" == 'no' 
  then
    AC_MSG_RESULT(no)
  else
    AC_MSG_RESULT(yes - workaround included)
    ELLIPSIS_EXPAND_SHORT='#define __Ellipsis_Expand_Short'
  fi

  AC_MSG_CHECKING(whether vararg could be dereferenced )
  if test "$elipsis_deref_vararg" == 'no' 
  then
    AC_MSG_RESULT(no)
    ELLIPSIS_EXPAND_SHORT='#define __Ellipsis_Expand_Short'
  else
    AC_MSG_RESULT(yes)
  fi
 
AC_SUBST(ELLIPSIS_EXPAND_SHORT)


####### Check Lvalue Cast ######
lval_cast_fix='no'
LVAL_CAST=''

  AC_LANG(C++)
  AC_TRY_COMPILE(
    [#include <stdio.h>
    void savestruct(void *pointer)
	{ ((char *)pointer)++; }
    ],
    [],
    lval_cast_fix='no',
    lval_cast_fix='yes')

  AC_MSG_CHECKING(whether lvalue cast is allowed )
  if test "$lval_cast_fix" == 'no' 
  then
    AC_MSG_RESULT(yes)
  else
    AC_MSG_RESULT(no - workaround included)
    LVAL_CAST='#define NO_LVAL_CAST'
  fi
 
AC_SUBST(LVAL_CAST)


########### Check for stricmp/strcasecmp function existency ###########
STRICMP='strcmp'
AC_CHECK_FUNC(stricmp,STRICMP='stricmp')
AC_CHECK_FUNC(strcasecmp,STRICMP='strcasecmp')

AC_MSG_CHECKING(case insensitive string compare )
AC_MSG_RESULT($STRICMP)

if test "$STRICMP" == "strcmp"
then
  STRICMP='#define stricmp strcmp'
fi
if test "$STRICMP" == "stricmp"
then
  STRICMP=''
fi
if test "$STRICMP" == "strcasecmp"
then
  STRICMP='#define stricmp strcasecmp'
fi

AC_SUBST(STRICMP)


########### Check for strip tool existency ###########

AC_CHECK_TOOL(STRIP, strip, none, $PATH)
if test "$STRIP" == "none"
then
  STRIP='#echo no strip'
fi


########### Check for LaTeX presence ###########
if test "$TEST_LTX" == "check"
then
  AC_CHECK_TOOL(LATEX, latex, none, $PATH)
  if test "$LATEX" == "none"
  then
    TEST_LTX='testconv'
    LATEX='#echo no LaTeX'
  else
    TEST_LTX='testltx'
  fi
fi
AC_SUBST(TEST_LTX)


########### Check for groff tool existency ###########

AC_CHECK_TOOL(GROFF, groff, none, $PATH)
if test "$GROFF" == "none"
then
  WP2LATEX_MAN=''
else
  WP2LATEX_MAN='wp2latex.1'
fi
AC_SUBST(WP2LATEX_MAN)



######### Fix OSTYPE variable ###########

SET_OSTYPE="OSTYPE=$OSTYPE"

if test "$OSTYPE" == ""; then
  SET_OSTYPE+=`uname`
fi

AC_SUBST(SET_OSTYPE)

########### Installation preffix ###########

if test "$prefix" == ""; then
  prefix='/usr/local'
fi

echo "Installation prefix: ${prefix}"
AC_SUBST(prefix)

########### Create makefiles ###########

AC_CONFIG_FILES([makefile doc/makefile sources.cc/makefile sources.cc/atoms/makefile sources.cc/cp_lib/makefile sources.cc/atoms/include/common.h instaler/makefile])
AC_OUTPUT

########### Write summary ###########
echo " "
cat <<EOF
  JPEG Library:    ${have_jpeg}
  LibIntl Library: ${have_libintl} 
  PNG Library:     ${have_png}  
EOF
AC_MSG_RESULT([Type make to build WP2LaTeX.])