AC_INIT(src/main.c)
AM_INIT_AUTOMAKE(ssbd,0.10)
AM_CONFIG_HEADER(config.h)

dnl Determine default prefix
test x$prefix = "xNONE" && prefix="$ac_default_prefix"


dnl Locate required external software
AC_PROG_CC
AC_PROG_INSTALL

dnl Check for system header files
AC_HEADER_STDC
dnl AC_CHECK_HEADERS(errno.h ctype.h dirent.h fcntl.h libgen.h locale.h math.h nl_types.h process.h pty.h signal.h stdarg.h stdio.h time.h unistd.h sys/ioctl.h sys/param.h sys/select.h sys/signal.h sys/stat.h sys/time.h sys/types.h sys/wait.h)
AC_CHECK_HEADERS(fcntl.h time.h sys/time.h sys/wait.h)
AC_HEADER_TIME

AC_CHECK_LIB(m, log, LIBM="-lm")
LDFLAGS="$LDFLAGS $LIBM"

dnl AM_PATH_GLIB_GC(1.2.0,,ssbd_HAVE_GLIB_GC=no)                                                                 
dnl if test x"$ssbd_HAVE_GLIB_GC" = x"no"; then
dnl AM_PATH_GLIB(1.2.0,,AC_MSG_ERROR([*** GLIB >= 1.2.0 not installed - please install first ***]))                                                                 
dnl fi
dnl 


# check for glib
AC_ARG_ENABLE(glib1, [  --disable-glib1          Do not compile with glib1.x], ,enable_glib1=yes )
AC_ARG_ENABLE(glib2, [  --disable-glib2          Do not compile with glib2.x], ,enable_glib2=yes )

if test x"$enable_glib2" = x"yes"; then
    PKG_CHECK_MODULES(GLIB, glib-2.0 > 2.0.0, ssbd_HAVE_GLIB2=yes, ssbd_HAVE_GLIB2=no)
else
    echo "glib 2.x disabled"    
    ssbd_HAVE_GLIB2=disabled
fi    
    
if test x"$ssbd_HAVE_GLIB2" = x"yes"; then
    AC_DEFINE(HAVE_GLIB2)
    ssbd_HAVE_GLIB1="untested"
else    
    if test x"$enable_glib1" = x"yes"; then
        AM_PATH_GLIB(1.2.0, ssbd_HAVE_GLIB1=yes, ssbd_HAVE_GLIB1=no)
        if test x"$ssbd_HAVE_GLIB1" = x"yes"; then
            AC_DEFINE(HAVE_GLIB1)
        else
            AC_MSG_ERROR([*** GLIB >= 1.2.0 not installed - please install first ***])
        fi
    else
        echo "glib 1.x disabled"    
        AC_MSG_ERROR([*** GLIB >= 1.2.0 not installed - please install first ***])
    fi    
fi    
CFLAGS="$CFLAGS $GLIB_CFLAGS"
LIBS="$LDFLAGS $GLIB_LIBS"
echo "CFLAGS='$GLIB_CFLAGS' LDFLAGS='$GLIB_LIBS'"

libsndfile_LIBS=$LIBS
libsndfile_CFLAGS=$CFLAGS
PKG_CHECK_MODULES(SNDFILE, sndfile >= 1.0.2, ac_cv_sndfile=1, ac_cv_sndfile=0)        
                                                                                   
AC_DEFINE_UNQUOTED([HAVE_SNDFILE],${ac_cv_sndfile},                                   
             [Set to 1 if you have libsndfile.])                                   
                                                                                   
AC_SUBST(SNDFILE_CFLAGS)                                                              
AC_SUBST(SNDFILE_LIBS)                                                                
  
dnl set
dnl echo $ac_cv_sndfile
if test ! x$ac_cv_sndfile = x1 ; then
    LIBS=$libsndfile_LIBS
    echo "***************************"
    echo libsndfile not found
    echo program need it to compile
    echo "***************************"
    exit 1    
fi    

CFLAGS=$libsndfile_CFLAGS
LIBS=$libsndfile_LIBS

CFLAGS="$CFLAGS $SNDFILE_CFLAGS"
LIBS="$LIBS $SNDFILE_LIBS"
echo "CFLAGS='$CFLAGS'"
echo "LIBS='$LIBS'"

AC_CHECK_PROG(HAVE_RPM, rpm, "yes", "no")

HAVE_RPM_PACKAGING=no
if test x$HAVE_RPM = xyes ; then
  AC_MSG_CHECKING(for rpm build directories)
  RPM_SOURCESDIR=`rpm --showrc | grep "^sourcedir" | \
    sed -e 's/.*: //' 2> /dev/null`
  if test x$RPM_SPECDIR = x ; then
    # Red Hat  : /usr/src/redhat
    # Mandrake : /usr/src/RPM
    # SuSE     : /usr/src/packages
    # Debian   : /usr/src/rpm
    for TEST_DIR in /usr/src/redhat /usr/src/RPM /usr/src/packages /usr/src/rpm ; do
      if test -d $TEST_DIR/SPECS -a -d $TEST_DIR/SOURCES -a \
              -d $TEST_DIR/RPMS -a -d $TEST_DIR/SRPMS -a \
              -d $TEST_DIR/BUILD ; then
        RPM_SOURCESDIR=$TEST_DIR/SOURCES
      fi
    done
  fi
  if test x$RPM_SOURCESDIR = x ; then
    AC_MSG_RESULT(not found)
    RPM_SOURCESDIR=""
  else
    AC_MSG_RESULT($RPM_SOURCESDIR)
    HAVE_RPM_PACKAGING=yes
  fi

  if test -f /etc/mandrake-release ; then 
      RPM_RELEASE="mdk"
  else    
      RPM_RELEASE=""
  fi      
  
fi
AC_SUBST(HAVE_RPM_PACKAGING)
AC_SUBST(RPM_SOURCESDIR)
AC_SUBST(RPM_RELEASE)


dnl ebuild for Gentoo
rm -f "pkg/$PACKAGE-*.ebuild"
cp "pkg/$PACKAGE.ebuild" "pkg/$PACKAGE-$VERSION.ebuild"
    
LDFLAGS="$LDFLAGS $LIBS"
AC_OUTPUT(Makefile doc/Makefile src/Makefile pkg/ssbd.spec)

