dnl Process this file with autoconf to produce a configure script.
AC_INIT(lib/musicbrainz.h)
AM_CONFIG_HEADER(config.h)
VERSION="1.0.0"
AM_INIT_AUTOMAKE(musicbrainz, "$VERSION")
dnl AC_DEFINE_UNQUOTED(VERSION, "$VERSION")

CFLAGS="-Wall -O2"
CPPFLAGS="-Wall -O2"

dnl Canonicalize host.
AC_CANONICAL_HOST
case "${host_cpu}-${host_os}" in
  *-beos*)    os=beos; LIBS='-lbe -lroot' ;;
  *-cygwin*)  os=cygwin; LIBS='-lwinmm' ;;
  *-freebsd*) os=freebsd ;;
  *-irix*)    os=irix; CXXFLAGS="$CFLAGS -fsquangle"; 
                       LIBS='-lcdaudio -lmediad -lds' ;; 
  *-linux*)   os=linux ;;
  *-netbsd*)  os=netbsd ;;
  *-openbsd*) os=openbsd ;;
  *-os2_emx*) os=os2 ;;
  *-solaris*) os=solaris; LIBS='-lsocket -lnsl' ;;
  *-qnx*)     os=qnx; LIBS='-lsocket' ;;
  *)          AC_MSG_RESULT([WARNING: unknown system]) ;;
esac

dnl right now we specialize by linking (subject to change)
AC_LINK_FILES(osdep/mb_${os}.cpp osdep/mb_${os}.h, lib/mb.cpp lib/mb.h)

dnl Checks for programs.
AC_PROG_AWK
AC_PROG_CC
AM_PROG_LIBTOOL
AC_SUBST(LIBTOOL_DEPS)  
AC_PROG_CXX
AC_PROG_INSTALL
AC_PROG_LN_S
AC_C_BIGENDIAN
AC_CHECK_SIZEOF(long)

dnl Checks for libraries.

dnl Checks for header files.

dnl Checks for typedefs, structures, and compiler characteristics.

AC_DEFUN([AC_PROTOTYPE],[
 pushdef([function],translit([$1], [a-z], [A-Z]))
 pushdef([tags],[AC_PROTOTYPE_TAGS(builtin([shift],builtin([shift],builtin([shift],$@))))])
 for i in 1
 do
   AC_PROTOTYPE_LOOP(AC_PROTOTYPE_REVERSE($1, AC_PROTOTYPE_SUBST($2,tags),AC_PROTOTYPE_SUBST($3, tags),builtin([shift],builtin([shift],builtin([shift],$@)))))
   AC_MSG_ERROR($1 unable to find a working combination)
 done
 popdef([tags])
 popdef([function])
])

AC_DEFUN([AC_PROTOTYPE_REVERSE],[ifelse($#,0,,$#,1,[[$1]],[AC_PROTOTYPE_REVERSE(builtin([shift],$@)),[$1]])])

AC_DEFUN([AC_PROTOTYPE_SUBST],[ifelse($2,,[$1],[AC_PROTOTYPE_SUBST(patsubst([$1],[$2],[$2[]_VAL]),builtin([shift],builtin([shift],$@)))])])

AC_DEFUN([AC_PROTOTYPE_TAGS],[ifelse($1,,[],[$1, AC_PROTOTYPE_TAGS(builtin([shift],builtin([shift],$@)))])])
AC_DEFUN([AC_PROTOTYPE_DEFINES],[ifelse($1,,[],[AC_DEFINE(function[]_$1, $1_VAL) AC_PROTOTYPE_DEFINES(builtin([shift],$@))])])

AC_DEFUN([AC_PROTOTYPE_STATUS],[ifelse($1,,[],[$1 => $1_VAL AC_PROTOTYPE_STATUS(builtin([shift],$@))])])

AC_DEFUN([AC_PROTOTYPE_EACH],[
  ifelse($2,, [
  ], [
    pushdef([$1_VAL], $2)
    AC_PROTOTYPE_LOOP(rest)
    popdef([$1_VAL])
    AC_PROTOTYPE_EACH($1, builtin([shift], builtin([shift], $@)))
  ])
])

AC_DEFUN([AC_PROTOTYPE_LOOP],[
  ifelse(builtin([eval], $# > 3), 1,
  [
    pushdef([rest],[builtin([shift],builtin([shift],$@))])
    AC_PROTOTYPE_EACH($2,$1)
    popdef([rest])
  ], [
    AC_MSG_CHECKING($3 AC_PROTOTYPE_STATUS(tags))
    ac_save_CPPFLAGS="$CPPFLAGS"
    ifelse(AC_LANG,CPLUSPLUS,if test "$GXX" = "yes" ; then CPPFLAGS="$CPPFLAGS -Werror" ; fi)
    ifelse(AC_LANG,C,if test "$GCC" = "yes" ; then CPPFLAGS="$CPPFLAGS -Werror" ; fi)
    AC_TRY_COMPILE($2, $1, [
      CPPFLAGS="$ac_save_CPPFLAGS"
      AC_MSG_RESULT(ok)
      AC_PROTOTYPE_DEFINES(tags)
      break;
    ], [
      CPPFLAGS="$ac_save_CPPFLAGS"
      AC_MSG_RESULT(not ok)
    ])
  ]
 )
])

AC_DEFUN([AC_PROTOTYPE_ACCEPT],[
AC_PROTOTYPE(accept,
 [
  #include <sys/types.h>
  #include <sys/socket.h>
 ],
 [
  int a = 0;
  ARG2 * b = 0;
  ARG3 * c = 0;
  accept(a, b, c);
 ],
 ARG2, [struct sockaddr, void],
 ARG3, [socklen_t, size_t, int, unsigned int, long unsigned int])
])

AC_PROTOTYPE_ACCEPT

dnl Checks for library functions.
AC_CHECK_LIB(ogg, ogg_stream_flush,[AC_DEFINE(HAVE_OGG)],[],)
AC_CHECK_LIB(vorbis, vorbis_synthesis_init,[AC_DEFINE(HAVE_VORBIS)],[],)
AC_CHECK_LIB(vorbisfile, ov_open,
   [ AC_DEFINE(HAVE_VORBISFILE) 
     VMM="vmm"
     AC_SUBST(VMM)
   ],[],-lvorbis)

AC_DEFINE_UNQUOTED(PREFIX, "${prefix}")
AC_OUTPUT(Makefile lib/Makefile lib/bitzi/Makefile expat/Makefile expat/xmltok/Makefile expat/xmlparse/Makefile examples/Makefile, echo timestamp > stamp-h)
