dnl -*- m4 -*-
dnl Process this file with autoconf to produce a configure script.
AC_INIT(pgmain.c)
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(picogui-server, 0.45)
AM_ACLOCAL_INCLUDE(macros)

dnl before checking the compiler, save the CFLAGS from environment
user_CFLAGS=${CFLAGS}

AM_PGUI_CROSSCOMPILE

dnl Get the configuration file for the system

AC_ARG_WITH(profile,
[  --with-profile=<config file>   Use given profile for server customization],,
  test -f profile.user || cp "${srcdir}/profile.defaults" profile.user
  with_profile="profile.user")

AC_ARG_WITH(extra-ipath,
[  --with-extra-ipath=<extra include path>   To specify extra include paths],
  DRIVER_CFLAGS="$DRIVER_CFLAGS -I`echo ${withval} | sed 's/:/ -I/g' `")

AC_ARG_WITH(extra-lpath,
[  --with-extra-lpath=<extra lib path>   To specify extra library paths],
  LDFLAGS="$LDFLAGS -L`echo ${withval} | sed 's/:/ -L/g' `")

dnl
dnl Check if we have processmanager or not
dnl 
AC_ARG_WITH(pm-cflags,
[  --with-pm-cflags=<pm cflags>   To specify Process Manager cflags],[
  AC_DEFINE(PM_ENABLED)
  DRIVER_CFLAGS="$DRIVER_CFLAGS ${withval}"
])

dnl
dnl Check if we have extra CFLAGS to pass
dnl 
AC_ARG_WITH(extra-cflags,
[  --with-extra-cflags=<cflags>   To specify extra cflags],[
  DRIVER_CFLAGS="$DRIVER_CFLAGS ${withval}"
])

dnl normalize the path somehow
CONFIG_FILE=$with_profile
CONFIG_FILE=`echo $CONFIG_FILE`

case "$CONFIG_FILE" in
  /*)	;;
   *)	CONFIG_FILE="`pwd`/${CONFIG_FILE}"
	;;
esac

AC_MSG_CHECKING([if the configuration file is readable])

if test -r "$CONFIG_FILE" ; then
  AC_MSG_RESULT($with_profile)
else
  AC_MSG_RESULT($CONFIG_FILE not found)
  AC_MSG_ERROR([cannot find configuration file])
fi

AC_SUBST(CONFIG_FILE)


dnl Use the configuration file to determine the requested files
eval `. $CONFIG_FILE && . ${srcdir}/select.in`

if test $? != "0" ; then
  AC_MSG_ERROR([problem while parsing configuration])
fi

AC_SUBST(WIDGET)
AC_SUBST(DEBUGGING)
AC_SUBST(VIDBASE)
AC_SUBST(DRIVER)
AC_SUBST(VIDWRAP)
AC_SUBST(INPUT)
AC_SUBST(FORMATS)
AC_SUBST(FONT)
AC_SUBST(APPMGR)
AC_SUBST(OS)
AC_SUBST(VIDSUBDIRS)
AC_SUBST(CURSES) 

dnl Check for perl

AC_PATH_PROGS(PERL, perl5 perl,
	AC_MSG_ERROR([you need perl]))

AC_SUBST(PERL)


dnl Check for ncurses _on the build system_
if test "$cross_compiling" = "yes" ; then

  old_CC="$CC"
  old_CPP="$CPP"

  CC="$CC_FOR_BUILD"
  CPP="$CC -E"

  AC_CHECK_LIB(ncurses, delscreen, [
      LXDIALOG="lxdialog"
      CURSES="ncurses"
      AC_CHECK_HEADERS(ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h)
    ], [  
      AC_CHECK_LIB(pdcurses, PDC_gotoxy, [
          LXDIALOG="lxdialog"
          CURSES="pdcurses"
          AC_CHECK_HEADERS(pdcurses/pdcurses.h pdcurses/curses.h pdcurses.h curses.h)
        ], [  
          LXDIALOG="" 
          CURSES=""
          AC_MSG_WARN([menuconfig won't be available])
        ])
    ])

  CC="$old_CC"
  CPP="$old_CPP"
  
else

  AC_CHECK_LIB(ncurses, delscreen, [
    LXDIALOG="lxdialog"
    CURSES="ncurses"
    AC_CHECK_HEADERS(ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h)
  ], [  
    AC_CHECK_LIB(pdcurses, PDC_gotoxy, [
      LXDIALOG="lxdialog"
      CURSES="pdcurses"
      AC_CHECK_HEADERS(pdcurses/pdcurses.h pdcurses/curses.h pdcurses.h curses.h)
    ], [  
      LXDIALOG="" 
      CURSES=""
      AC_MSG_WARN([menuconfig won't be available])
    ])
  ])

fi

AC_SUBST(LXDIALOG)

dnl explicitely set CFLAGS to the value passed by the user

CFLAGS=${user_CFLAGS}

dnl ==================================================
dnl Extra libraries depending on the selected profile
dnl ==================================================

dnl Win32 stuff
AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_OS_WIN32, [ 
  DRIVER_LIBS="$DRIVER_LIBS -lwsock32" 
])


dnl Eventually check for SDL
AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_SDLFB, [ 

  AM_PATH_SDL(1.0.0, , AC_MSG_WARN(cannot find SDL))

  DRIVER_CFLAGS="$DRIVER_CFLAGS ${SDL_CFLAGS}"
  DRIVER_LIBS="$DRIVER_LIBS ${SDL_LIBS}" 
])
AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_SDLGL, [ 

  AM_PATH_SDL(1.0.0, , AC_MSG_WARN(cannot find SDL))

  DRIVER_CFLAGS="$DRIVER_CFLAGS ${SDL_CFLAGS}"
  DRIVER_LIBS="$DRIVER_LIBS ${SDL_LIBS}" 
])

dnl OpenGL
AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_VBL_GL, [ 
  AC_CHECK_LIB(GL,glBegin)
  DRIVER_LIBS="$DRIVER_LIBS -lGL -lGLU" 
])

dnl DirectFB
AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_DIRECTFB, [
  PKG_CHECK_MODULES(DIRECTFB, directfb >= 0.9.19)
  DRIVER_CFLAGS="$DRIVER_CFLAGS ${DIRECTFB_CFLAGS}"
  DRIVER_LIBS="$DRIVER_LIBS ${DIRECTFB_LIBS}" 
])

dnl Configure libtool
if test "$CONFIG_NET_LIBPGSERVER" = "y"; then
  AC_ENABLE_SHARED
  AC_DISABLE_STATIC
else
  AC_ENABLE_STATIC
  AC_DISABLE_SHARED
fi
AM_PROG_LIBTOOL

dnl Check if we need extra libs
AC_ARG_WITH(extra-libs,
[  --with-extra-libs=<extra libs>   To specify extra libraries],
  LIBS="$LIBS `echo ${withval} | sed 's/:/ /g' `")

dnl jpeg
echo "checking whether user wants jpeg... $CONFIG_FORMAT_JPEG"

if test "$CONFIG_FORMAT_JPEG" = "y"; then


  AC_ARG_WITH(jpeg-dir,
  [  --with-jpeg-dir[=DIR]   PGUI: define libjpeg install directory],
     PGUI_WITH_JPEG_DIR="`echo ${withval} | sed 's/:/ /g' `", PGUI_WITH_JPEG_DIR="no")

  if test "$PGUI_WITH_JPEG_DIR" = "no"; then
	AC_CHECK_HEADERS(jpeglib.h)

	if test "$HAVE_JPEGLIB_H" = "no"; then
            AC_MSG_ERROR(jpeglib.h not found! try --with-jpeg-dir=<DIR>)
	fi

        AC_CHECK_LIB(jpeg,jpeg_read_header)

	if test "$HAVE_LIBJPEG" = "no"; then
            AC_MSG_ERROR(libjpeg not found! try --with-jpeg-dir=<DIR>)
	fi

	LIBS="$LIBS -ljpeg"
  else

	echo "user defined jpeg path: $PGUI_WITH_JPEG_DIR"
	
	if [ ! test -f $PGUI_WITH_JPEG_DIR/include/jpeglib.h ]; then
            AC_MSG_ERROR(jpeglib.h not found! Make sure $PGUI_WITH_JPEG_DIR has jpeglib.h)
	fi

	LIBS="$LIBS -L$PGUI_WITH_JPEG_DIR/lib -ljpeg"
	CFLAGS="-I$PGUI_WITH_JPEG_DIR/include"
  fi

fi

dnl Check for VGA & VGAGL
AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_SVGAGL, [

  AC_CHECK_LIB(vga, vga_init,, AC_MSG_ERROR([cannot find libvga]))
  AC_CHECK_LIB(vgagl, gl_clearscreen,, AC_MSG_WARN([can't find libvgagl]))

  DRIVER_LIBS="$DRIVER_LIBS -lvga -lvgagl"
])

AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_SVGAFB, [

  AC_CHECK_LIB(vga, vga_init,, AC_MSG_WARN(cannot find libvga))

  DRIVER_LIBS="$DRIVER_LIBS -lvga"
])

dnl check for libpng

AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_FORMAT_PNG, [

  AC_CHECK_LIB(png, png_sig_cmp,, AC_MSG_WARN(cannot find libpng))

  DRIVER_LIBS="$DRIVER_LIBS -lm -lz -lpng"
])

dnl pthreads are required for the VNC server

AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_VNCSERVER, [

  AC_CHECK_LIB(pthread, pthread_create,, AC_MSG_WARN(cannot find pthreads))

  DRIVER_LIBS="$DRIVER_LIBS -lpthread"
])

dnl check for the X window system

AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_X11, [

  AC_CHECK_LIB(X11, XOpenDisplay,, AC_MSG_WARN([can't find libX11]))

  DRIVER_LIBS="$DRIVER_LIBS -L/usr/X11/lib -L/usr/X11R6/lib -lX11 -lXext"
])

AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_FONTENGINE_XFT, [

  AC_CHECK_LIB(Xft, XftDrawCreate,, AC_MSG_WARN([can't find Xft]))

  DRIVER_LIBS="$DRIVER_LIBS -lXft"
])

AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_X11_ICON, [

  AC_CHECK_LIB(Xpm, XpmCreatePixmapFromData,, AC_MSG_WARN([can't find libXpm]))

  DRIVER_LIBS="$DRIVER_LIBS -lXpm"
])

dnl check for ncurses & gpm

AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_NCURSES, [

  AC_CHECK_LIB(ncurses, delscreen,, AC_MSG_WARN([can't find libncurses]))

  DRIVER_LIBS="$DRIVER_LIBS -lncurses"
])

AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_PDCURSES, [

  AC_CHECK_LIB(pdcurses, PDC_gotoxy,, AC_MSG_WARN([can't find libncurses]))

  DRIVER_LIBS="$DRIVER_LIBS -lpdcurses"
])

AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_GPM, [

  AC_CHECK_LIB(gpm, Gpm_Open,, AC_MSG_WARN([can't find libgpm]))

  DRIVER_LIBS="$DRIVER_LIBS -lgpm"
])

dnl check for mgl2
AM_PGUI_CONFIG($CONFIG_FILE, DRIVER_MGL2FB, [
    DRIVER_CFLAGS="$DRIVER_CFLAGS -I/usr/pkg/include"
    DRIVER_LIBS="$DRIVER_LIBS -L/usr/pkg/lib/ -Wl,-rpath,/usr/pkg/lib -lmgl2"
])

dnl check for freetype2
dnl Gamma correction needs math lib for pow()
AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_FONTENGINE_FREETYPE, [
    AC_CHECK_LIB(freetype, FT_Init_FreeType,, AC_MSG_WARN([can't find libfreetype]))
    CFLAGS="$CFLAGS `freetype-config --cflags`"
    DRIVER_LIBS="$DRIVER_LIBS `freetype-config --libs` -lm"
])

dnl check for freetype2
dnl (ftgl driver doesn't do gamma correction)
AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_FONTENGINE_FTGL, [
    AC_CHECK_LIB(freetype, FT_Init_FreeType,, AC_MSG_WARN([can't find libfreetype]))
    CFLAGS="$CFLAGS `freetype-config --cflags`"
    DRIVER_LIBS="$DRIVER_LIBS `freetype-config --libs`"
])

# More compiler flags...

AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_LINK_STATIC, [
   DRIVER_LIBS="$DRIVER_LIBS --static"
])

AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_OPTIMIZE_SPEED, [
   CFLAGS="${CFLAGS} -O2"
])

AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_MAX_OPTIMIZE, [
   CFLAGS="${CFLAGS} -O3"
])

AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_OPTIMIZE_SIZE, [
   CFLAGS="${CFLAGS} -Os"
])

AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_EFENCE, [
   LDFLAGS="${LDFLAGS} -lefence"
])

AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_GCOV, [
   CFLAGS="${CFLAGS} -fprofile-arcs -ftest-coverage"
])

AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_GPROF, [
   CFLAGS="${CFLAGS} -pg"
   LDFLAGS="${LDFLAGS} -pg"
])


AM_PGUI_CONFIG($CONFIG_FILE, CONFIG_NOSTRIP, [
  CFLAGS="${CFLAGS} -g" 
], [
  CFLAGS="${CFLAGS} -fomit-frame-pointer" 
])

AC_SUBST(DRIVER_CFLAGS)
AC_SUBST(DRIVER_LIBS)

dnl Architecture-specific CFLAGS
case "$host" in
  m68k-pic-*)
	CFLAGS="${CFLAGS} -fPIC"
	;;
  mipsel-*-netbsd*)
	CFLAGS="${CFLAGS}"
	;;
  mipsel-*)
	CFLAGS="${CFLAGS} -msoft-float"
	;;
esac

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h malloc.h sys/ioctl.h sys/time.h unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_TYPE_PID_T
AC_TYPE_SIZE_T
AC_HEADER_TIME

dnl Checks for library functions.
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MMAP
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(gettimeofday select socket strerror strspn strtol)


AC_OUTPUT(
	Makefile
	widget/Makefile
	appmgr/Makefile
	debug/Makefile
	gcore/Makefile
	net/Makefile
	vidbase/Makefile
	theme/Makefile
	video/Makefile
	os/Makefile
	vidwrap/Makefile
	input/Makefile
	font/Makefile
	include/Makefile
	include/pgserver/Makefile
	include/picogui/Makefile
	configscript/Makefile
	configscript/lxdialog/Makefile
	formats/Makefile
	profiles/Makefile
	video/libvncserver/Makefile
	picogui-server.spec
)

echo "--------------------------------------------------"
echo "$PACKAGE, version $VERSION"
echo "--------------------------------------------------"
echo
echo "Profile read from: $with_profile"
echo
echo "      widgets:  $WIDGET"
echo "        debug:  $DEBUGGING"
echo "      vidbase:  $VIDBASE"
echo "       driver:  $DRIVER"
echo "     wrappers:  $VIDWRAP"
echo "        input:  $INPUT"
echo "      formats:  $FORMATS"
echo "           os:  $OS"
echo "     compiler:  ${CC} ${CFLAGS}"
echo "       linker:  ${LD:-ld} ${LDFLAGS}"
echo "driver-cflags:  $DRIVER_CFLAGS"
echo "  driver-libs:  $DRIVER_LIBS"
echo "--------------------------------------------------"
echo
