# Source of configuration for QDBM



#================================================================
# Generic Settings
#================================================================


# Targets
AC_INIT(qdbm, 1.8.77)

# Export variables
LIBVER=14
LIBREV=13
TARGETS="all"
MYDEFS=""
MYOPTS=""
MGWLIBS=""
LD="ld"
AR="ar"

# Building paths
pathtmp="$PATH"
PATH="$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
PATH="$PATH:/usr/ccs/bin:/usr/ucb:/usr/xpg4/bin:/usr/xpg6/bin:$pathtmp"
LIBRARY_PATH="$HOME/lib:/usr/local/lib:$LIBRARY_PATH"
LD_LIBRARY_PATH="$HOME/lib:/usr/local/lib:$LD_LIBRARY_PATH"
CPATH="$HOME/include:/usr/local/include:$CPATH"
export PATH LIBRARY_PATH LD_LIBRARY_PATH CPATH



#================================================================
# Options
#================================================================


# Internal variables
enables=""
ispthread=""
iszlib=""
isiconv=""
isnowarn=""

# Debug mode
AC_ARG_ENABLE(debug,
  AC_HELP_STRING([--enable-debug], [build for debugging]))
if test "$enable_debug" = "yes"
then
  TARGETS="debug"
  enables="$enables (debug)"
fi

# Developping mode
AC_ARG_ENABLE(devel,
  AC_HELP_STRING([--enable-devel], [build for development]))
if test "$enable_devel" = "yes"
then
  TARGETS="devel"
  enables="$enables (devel)"
fi

# Stable mode
AC_ARG_ENABLE(stable,
  AC_HELP_STRING([--enable-stable], [build for stable release]))
if test "$enable_stable" = "yes"
then
  TARGETS="stable"
  enables="$enables (stable)"
fi

# Enable POSIX thread
AC_ARG_ENABLE(pthread,
  AC_HELP_STRING([--enable-pthread], [use POSIX thread and make APIs thread-safe]))
if test "$enable_pthread" = "yes"
then
  MYDEFS="$MYDEFS -DMYPTHREAD"
  enables="$enables (pthread)"
  ispthread="yes"
fi

# Disable file locking
AC_ARG_ENABLE(lock,
  AC_HELP_STRING([--disable-lock], [build for environments without file locking]))
if test "$enable_lock" = "no"
then
  MYDEFS="$MYDEFS -DMYNOLOCK"
  enables="$enables (no-lock)"
fi

# Disable memory mapping
AC_ARG_ENABLE(mmap,
  AC_HELP_STRING([--disable-mmap], [build for environments without memory mapping]))
if test "$enable_mmap" = "no"
then
  MYDEFS="$MYDEFS -DMYNOMMAP"
  enables="$enables (no-mmap)"
fi

# Enable ZLIB compression
AC_ARG_ENABLE(zlib,
  AC_HELP_STRING([--enable-zlib], [feature ZLIB for B+ tree and inverted index]))
if test "$enable_zlib" = "yes"
then
  MYDEFS="$MYDEFS -DMYZLIB"
  MGWLIBS="-lz $MGWLIBS"
  enables="$enables (zlib)"
  iszlib="yes"
fi

# Enable LZO compression
AC_ARG_ENABLE(lzo,
  AC_HELP_STRING([--enable-lzo], [feature LZO for B+ tree and inverted index]))
if test "$enable_lzo" = "yes"
then
  MYDEFS="$MYDEFS -DMYLZO"
  MGWLIBS="-llzo2 $MGWLIBS"
  enables="$enables (lzo)"
  islzo="yes"
fi

# Enable BZIP2 compression
AC_ARG_ENABLE(bzip,
  AC_HELP_STRING([--enable-bzip], [feature BZIP2 for B+ tree and inverted index]))
if test "$enable_bzip" = "yes"
then
  MYDEFS="$MYDEFS -DMYBZIP"
  MGWLIBS="-lbz2 $MGWLIBS"
  enables="$enables (bzip)"
  isbzip="yes"
fi

# Enable ICONV utilities
AC_ARG_ENABLE(iconv,
  AC_HELP_STRING([--enable-iconv], [feature ICONV utilities]))
if test "$enable_iconv" = "yes"
then
  MYDEFS="$MYDEFS -DMYICONV"
  MGWLIBS="-liconv $MGWLIBS"
  enables="$enables (iconv)"
  isiconv="yes"
fi

# No warning configuration
AC_ARG_ENABLE(warn,
  AC_HELP_STRING([--disable-warn], [hide warnings in the configuration]))
if test "$enable_warn" = "no"
then
  isnowarn="yes"
fi

# Messages
printf '#================================================================\n'
printf '# Configuring QDBM version %s%s.\n' "$PACKAGE_VERSION" "$enables"
printf '#================================================================\n'



#================================================================
# Checking Commands to Build with
#================================================================


# C compiler
AC_PROG_CC
if test "$GCC" != "yes"
then
  if test "$isnowarn" != "yes"
  then
    printf '#================================================================\n' 1>&2
    printf '# WARNING: GCC is required to build this package.\n' 1>&2
    printf '#================================================================\n' 1>&2
  fi
fi
if uname | egrep -i 'Linux' > /dev/null 2>&1 &&
  uname -m | egrep '(x|i)(3|4|5|6|7|8|9)?86' > /dev/null 2>&1
then
  MYOPTS="-minline-all-stringops"
fi

echo "Compiler version:" `$CC -v`

if $CC -v 2>&1 | egrep -iq '(^| )clang '; then
  MYOPTS="-O1 -fno-omit-frame-pointer"
elif uname | egrep -iq 'SunOS' || \
	uname | egrep -iq 'BSD' || \
	gcc --version | egrep -iq '^2\.(8|9)'
then
  MYOPTS="-O1 -fno-omit-frame-pointer -fno-force-addr"
else
  MYOPTS="-O1 -fno-omit-frame-pointer -fforce-addr"
fi

# Linker
printf 'checking for ld... '
if which ld | grep '/ld$' > /dev/null 2>&1
then
  LD=`which ld`
  printf '%s\n' "$LD"
else
  printf 'no\n'
  if test "$isnowarn" != "yes"
  then
    printf '#================================================================\n' 1>&2
    printf '# WARNING: ld is not found in PATH.\n' 1>&2
    printf '#================================================================\n' 1>&2
  fi
fi

# Archiver
printf 'checking for ar... '
if which ar | grep '/ar$' > /dev/null 2>&1
then
  AR=`which ar`
  printf '%s\n' "$AR"
else
  printf 'no\n'
  if test "$isnowarn" != "yes"
  then
    printf '#================================================================\n' 1>&2
    printf '# WARNING: ar is not found in PATH.\n' 1>&2
    printf '#================================================================\n' 1>&2
  fi
fi



#================================================================
# Checking Libraries
#================================================================


# Underlying libraries
AC_CHECK_LIB(c, main)

# for pthread
if test "$ispthread" = "yes"
then
  AC_CHECK_LIB(pthread, main)
fi

# for zlib
if test "$iszlib" = "yes"
then
  AC_CHECK_LIB(z, main)
fi

# for lzo
if test "$islzo" = "yes"
then
  AC_CHECK_LIB(lzo2, main)
fi

# for bzip
if test "$isbzip" = "yes"
then
  AC_CHECK_LIB(bz2, main)
fi

# for iconv
if test "$isiconv" = "yes"
then
  AC_CHECK_LIB(iconv, main)
fi

# For old BSDs
if test "$ispthread" = "yes" && uname -a | grep BSD > /dev/null &&
  test -f /usr/lib/libc_r.a && test ! -f /usr/lib/libpthread.a
then
  LIBS=`printf '%s' "$LIBS" | sed 's/-lc/-lc_r/g'`
fi

# Duplication of QDBM for C
AC_CHECK_LIB(qdbm, main,
  if test "$isnowarn" != "yes"
  then
    printf '#================================================================\n' 1>&2
    printf '# WARNING: The existing library was detected.\n' 1>&2
    printf '#================================================================\n' 1>&2
  fi
)



#================================================================
# Generic Settings
#================================================================


# Export variables
AC_SUBST(LIBVER)
AC_SUBST(LIBREV)
AC_SUBST(TARGETS)
AC_SUBST(MYDEFS)
AC_SUBST(MYOPTS)
AC_SUBST(MGWLIBS)
AC_SUBST(LD)
AC_SUBST(AR)
AC_SUBST(CPPFLAGS)
AC_SUBST(LDFLAGS)

# Targets
AC_OUTPUT(Makefile LTmakefile qdbm.spec qdbm.pc)

# Messages
printf '#================================================================\n'
printf '# Ready to make.\n'
printf '#================================================================\n'



# END OF FILE
