# Process this file with autoconf to produce a configure script.

############################################################
# Autoconf requirements
############################################################

m4_include([gitrev.m4])

AC_PREREQ(2.59)
AC_INIT([chrysalide], [gitversion], [nocbos@gmail.com])

AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])

AM_INIT_AUTOMAKE
AM_SILENT_RULES

AM_GNU_GETTEXT_VERSION([0.18.3])
AM_GNU_GETTEXT([external])

AC_CONFIG_SRCDIR([src/main.c])


############################################################
# Information on the package
############################################################

#--- Checks for programs

AC_PROG_CC
AM_PROG_CC_C_O
AC_PROG_LEX
AC_PROG_YACC
AC_PROG_INSTALL
AC_PROG_MAKE_SET
AC_PROG_LIBTOOL


#--- Checks for libraries

AC_CHECK_LIB([dl], [dlopen])


#--- Checks for header files

AC_HEADER_DIRENT
AC_HEADER_STDBOOL
AC_HEADER_STDC
AC_CHECK_HEADERS([malloc.h])
AC_CHECK_HEADERS([netdb.h])
AC_CHECK_HEADERS([stdlib.h])
AC_CHECK_HEADERS([string.h])
AC_CHECK_HEADERS([unistd.h])


#--- Checks for types

AC_TYPE_SIZE_T


#--- Checks for structures

AC_STRUCT_TM


#--- Checks for compiler characteristics

AC_C_CONST


#--- Checks for library functions

AC_FUNC_CLOSEDIR_VOID
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_FUNC_STAT
AC_FUNC_STRFTIME
AC_FUNC_VPRINTF

AC_CHECK_FUNCS([pow])
AC_CHECK_FUNCS([regcomp])
AC_CHECK_FUNCS([strdup])
AC_CHECK_FUNCS([strerror])
AC_CHECK_FUNCS([strndup])
AC_CHECK_FUNCS([strrchr])


#--- Checks for system services



#--- Autoheader Macros

AH_TEMPLATE([REVISION], [Define the version number of the program for the about box.])
AH_TEMPLATE([PACKAGE_DATA_DIR], [Define the directory where the associated data will be installed.])
AH_TEMPLATE([PACKAGE_SOURCE_DIR], [Define the directory where the code source is waiting for being compiled.])
AH_TEMPLATE([PLUGINS_DIR], [Define the directory where the plugins are installed.])
AH_TEMPLATE([LOCALE_DIR], [Define the directory where the message catalogs are installed.])


AC_DEFINE_UNQUOTED(REVISION, AC_PACKAGE_VERSION)

if test "x${datadir}" = 'x${prefix}/share'; then
   if test "x${prefix}" = "xNONE"; then
      AC_DEFINE_UNQUOTED(PACKAGE_DATA_DIR, "${ac_default_prefix}/share/${PACKAGE}")
   else
      AC_DEFINE_UNQUOTED(PACKAGE_DATA_DIR, "${prefix}/share/${PACKAGE}")
   fi
elif test "x${datadir}" = 'x${datarootdir}'; then
   if test "x${datarootdir}" = 'x${prefix}/share'; then
      if test "x${prefix}" = "xNONE"; then
         AC_DEFINE_UNQUOTED(PACKAGE_DATA_DIR, "${ac_default_prefix}/share/${PACKAGE}")
      else
         AC_DEFINE_UNQUOTED(PACKAGE_DATA_DIR, "${prefix}/share/${PACKAGE}")
      fi
   else
      AC_DEFINE_UNQUOTED(PACKAGE_DATA_DIR, "${datarootdir}/${PACKAGE}")
   fi 
else
   AC_DEFINE_UNQUOTED(PACKAGE_DATA_DIR, "${datadir}/${PACKAGE}")
fi

packagesrcdir=`cd $srcdir && pwd`
AC_DEFINE_UNQUOTED(PACKAGE_SOURCE_DIR, "${packagesrcdir}")

AC_DEFINE_UNQUOTED(PLUGINS_DIR, PACKAGE_DATA_DIR "/plugins")

if test "x${prefix}" = "xNONE"; then
   AC_DEFINE_UNQUOTED(LOCALE_DIR, "${ac_default_prefix}/share/locale")
else
   AC_DEFINE_UNQUOTED(LOCALE_DIR, "${prefix}/share/locale")
fi


############################################################
# Compilation options
############################################################

AC_ARG_ENABLE(debug,        [  --enable-debug          compile with debugging support [def=no]],            [enable_debug=yes],     [enable_debug=no])


#--- Is debug mode needed ?

if test "x$enable_debug" = "xyes"; then
   DEBUG_CFLAGS="$DEBUG_CFLAGS -O0 -ggdb -gdwarf-2 -DDEBUG"
fi

AC_SUBST(DEBUG_CFLAGS)

#-Wall -Wimplicit -Wreturn-type -Wunused -Wswitch -Wcomment -Wuninitialized -Wparentheses -Wpointer-arith -Wmissing-prototypes

WARNING_FLAGS="-Wall -Wimplicit -Wreturn-type -Wunused -Wswitch -Wcomment -Wuninitialized -Wparentheses -Wpointer-arith -Wmissing-prototypes"

#-Wcast-qual -Wconversion -Wsign-compare -Wdisabled-optimization

AC_SUBST(WARNING_FLAGS)

# _BSD_SOURCE: htobe64, be64toh
# _XOPEN_SOURCE: strdup, snprintf
# _ISOC99_SOURCE: INFINITY; NAN
# GTK_DISABLE_DEPRECATED: on reste conforme au C99
#COMPLIANCE_FLAGS="-D_BSD_SOURCE -D_GNU_SOURCE -DGTK_DISABLE_DEPRECATED"
COMPLIANCE_FLAGS="-D_BSD_SOURCE -D_GNU_SOURCE"

AC_SUBST(COMPLIANCE_FLAGS)

#--- Checks for GTK 2.0

PKG_CHECK_MODULES(LIBGTK,gtk+-3.0 >= 3.8.6,[libgtk_found=yes],[libgtk_found=no])

if test "$libgtk_found" = "yes"; then
   libgtk_version=`pkg-config gtk+-3.0 --modversion`
else
   libgtk_version='-'
fi

AC_SUBST(LIBGTK_CFLAGS)
AC_SUBST(LIBGTK_LIBS)


#--- Checks for libxml2

PKG_CHECK_MODULES(LIBXML,libxml-2.0 >= 2.6.11,[libxml_found=yes],[libxml_found=no])

if test "$libxml_found" = "yes"; then
   libxml_version=`pkg-config libxml-2.0 --modversion`
else
   libxml_version='-'
fi

AC_SUBST(LIBXML_CFLAGS)
AC_SUBST(LIBXML_LIBS)


#--- Checks for libarchive

PKG_CHECK_MODULES(LIBARCHIVE,libarchive >= 3.1.2,[libarchive_found=yes],[libarchive_found=no])

if test "$libarchive_found" = "yes"; then
   libarchive_version=`pkg-config libarchive --modversion`
else
   libarchive_version='-'
fi

AC_SUBST(LIBARCHIVE_CFLAGS)
AC_SUBST(LIBARCHIVE_LIBS)


#--- Checks for libsqlite

PKG_CHECK_MODULES(LIBSQLITE,sqlite3 >= 3.8.2,[libsqlite_found=yes],[libsqlite_found=no])

if test "$libsqlite_found" = "yes"; then
   libsqlite_version=`pkg-config sqlite3 --modversion`
else
   libsqlite_version='-'
fi

AC_SUBST(LIBSQLITE_CFLAGS)
AC_SUBST(LIBSQLITE_LIBS)


#--- Checks for libssl

PKG_CHECK_MODULES(LIBSSL,libssl >= 1.0.1k,[libssl_found=yes],[libssl_found=no])

if test "$libssl_found" = "yes"; then
   libssl_version=`pkg-config libssl --modversion`
else
   libssl_version='-'
fi

AC_SUBST(LIBSSL_CFLAGS)
AC_SUBST(LIBSSL_LIBS)


#--- Checks for Python

if test "x$enable_debug" = "xyes"; then
   AC_CHECK_PROG(python3_cfg_binary, [python3-dbg-config] , yes)
else
   AC_CHECK_PROG(python3_cfg_binary, [python3-config] , yes)
fi

AM_CONDITIONAL([HAVE_PYTHON3_CONFIG], [test "x$python3_cfg_binary" = "xyes"])

if test "x$python3_cfg_binary" = "xyes"; then

   if test "x$enable_debug" = "xyes"; then
      LIBPYTHON_CFLAGS=`python3-dbg-config --cflags`
      LIBPYTHON_LIBS=`python3-dbg-config --libs`
      LIBPYTHON_ABI_FLAGS=`python3-dbg-config --abiflags`
   else
      LIBPYTHON_CFLAGS=`python3-config --cflags`
      LIBPYTHON_LIBS=`python3-config --libs`
      LIBPYTHON_ABI_FLAGS=`python3-config --abiflags`
   fi

   AC_SUBST(LIBPYTHON_CFLAGS)
   AC_SUBST(LIBPYTHON_LIBS)
   AC_DEFINE_UNQUOTED(LIBPYTHON_ABI_FLAGS, "${LIBPYTHON_ABI_FLAGS}", [ABI flags of the linked Python library])

   python3_version=`python3 --version | cut -d ' ' -f 2`

else

   python3_version='-'

fi

#--- Checks for pygobject-3.0

PKG_CHECK_MODULES(LIBPYGOBJECT,pygobject-3.0 >= 3.12.1,[libpygobject_found=yes],[libpygobject_found=no])

if test "$libpygobject_found" = "yes"; then
   libpygobject_version=`pkg-config pygobject-3.0 --modversion`
else
   libpygobject_version='-'
fi

AC_SUBST(LIBPYGOBJECT_CFLAGS)
AC_SUBST(LIBPYGOBJECT_LIBS)


############################################################
# Outputting files
############################################################

AC_CONFIG_FILES([stamp-h po/Makefile.in], [echo timestamp > stamp-h])

AC_CONFIG_COMMANDS([marshal], [echo -e "VOID:UINT64\nVOID:UINT64,UINT64\nVOID:INT,UINT64,INT\nVOID:OBJECT\nVOID:OBJECT,OBJECT\nVOID:ENUM,OBJECT\nVOID:ENUM,ENUM" > src/glibext/chrysamarshal.list])

AC_CONFIG_FILES([Makefile
                 pixmaps/Makefile
                 plugins/Makefile
                 plugins/androhelpers/Makefile
                 plugins/devdbg/Makefile
                 plugins/govm/Makefile
                 plugins/libcsem/Makefile
                 plugins/mobicore/Makefile
                 plugins/pychrysa/Makefile
                 plugins/pychrysa/analysis/Makefile
                 plugins/pychrysa/analysis/blocks/Makefile
                 plugins/pychrysa/analysis/contents/Makefile
                 plugins/pychrysa/analysis/db/Makefile
                 plugins/pychrysa/analysis/db/items/Makefile
                 plugins/pychrysa/arch/Makefile
                 plugins/pychrysa/arch/arm/Makefile
                 plugins/pychrysa/arch/arm/v7/Makefile
                 plugins/pychrysa/common/Makefile
                 plugins/pychrysa/core/Makefile
                 plugins/pychrysa/debug/Makefile
                 plugins/pychrysa/format/Makefile
                 plugins/pychrysa/format/dex/Makefile
                 plugins/pychrysa/format/elf/Makefile
                 plugins/pychrysa/glibext/Makefile
                 plugins/pychrysa/gtkext/Makefile
                 plugins/pychrysa/gui/Makefile
                 plugins/pychrysa/gui/panels/Makefile
                 plugins/python/Makefile
                 plugins/python/androperms/Makefile
                 plugins/python/apkfiles/Makefile
                 plugins/python/exectracer/Makefile
                 plugins/python/samples/Makefile
                 plugins/python/welcome/Makefile
                 plugins/readdex/Makefile
                 plugins/readelf/Makefile
                 plugins/ropgadgets/Makefile
                 plugins/stackvars/Makefile
                 src/Makefile
                 src/analysis/Makefile
                 src/analysis/blocks/Makefile
                 src/analysis/contents/Makefile
                 src/analysis/db/Makefile
                 src/analysis/db/items/Makefile
                 src/analysis/db/misc/Makefile
                 src/analysis/decomp/Makefile
                 src/analysis/disass/Makefile
                 src/analysis/types/Makefile
                 src/arch/Makefile
                 src/arch/arm/Makefile
                 src/arch/arm/v7/Makefile
                 src/arch/arm/v7/opdefs/Makefile
                 src/arch/arm/v7/opcodes/Makefile
                 src/arch/arm/v7/operands/Makefile
                 src/arch/dalvik/Makefile
                 src/arch/dalvik/decomp/Makefile
                 src/arch/dalvik/opdefs/Makefile
                 src/arch/dalvik/opcodes/Makefile
                 src/arch/dalvik/operands/Makefile
                 src/arch/dalvik/pseudo/Makefile
                 src/arch/jvm/Makefile
                 src/arch/mips/Makefile
                 src/arch/x86/Makefile
                 src/arch/x86/opcodes/Makefile
                 src/arch/x86/operands/Makefile
                 src/common/Makefile
                 src/core/Makefile
                 src/debug/Makefile
                 src/debug/jdwp/Makefile
                 src/debug/jdwp/misc/Makefile
                 src/debug/jdwp/sets/Makefile
                 src/debug/remgdb/Makefile
                 src/decomp/Makefile
                 src/decomp/expr/Makefile
                 src/decomp/expr/dalvik/Makefile
                 src/decomp/instr/Makefile
                 src/decomp/lang/Makefile
                 src/format/Makefile
                 src/format/dex/Makefile
                 src/format/dwarf/Makefile
                 src/format/dwarf/v2/Makefile
                 src/format/dwarf/v3/Makefile
                 src/format/dwarf/v4/Makefile
                 src/format/elf/Makefile
                 src/format/java/Makefile
                 src/format/mangling/Makefile
                 src/format/mangling/dex/Makefile
                 src/format/mangling/itanium/Makefile
                 src/format/pe/Makefile
                 src/glibext/Makefile
                 src/gtkext/Makefile
                 src/gtkext/graph/Makefile
                 src/gtkext/graph/nodes/Makefile
                 src/gui/Makefile
                 src/gui/core/Makefile
                 src/gui/dialogs/Makefile
                 src/gui/menus/Makefile
                 src/gui/panels/Makefile
                 src/gui/tb/Makefile
                 src/panels/Makefile
                 src/plugins/Makefile
                 src/plugins/overjump/Makefile
                 tools/Makefile
                 tools/d2c/Makefile
                 tools/d2c/args/Makefile
                 tools/d2c/bits/Makefile
                 tools/d2c/conv/Makefile
                 tools/d2c/format/Makefile
                 tools/d2c/hooks/Makefile
                 tools/d2c/rules/Makefile
                 tools/d2c/syntax/Makefile
                 themes/Makefile])

AC_OUTPUT


echo
echo -n $PACKAGE r
echo AC_PACKAGE_VERSION

echo
echo The GNU Image Manipulation Program Toolkit... : $libgtk_version
echo The XML C parser and toolkit of Gnome........ : $libxml_version
echo The flexible interface for archives I/O...... : $libarchive_version
echo The small, fast and reliable database engine. : $libsqlite_version
echo The cryptography and SSL/TLS toolkit......... : $libssl_version

echo
echo Available Python programming language........ : $python3_version
echo The Python GObject bindings.................. : $libpygobject_version

echo
echo Print debugging messages..................... : $enable_debug

echo
echo configure complete, now type \'make\'
echo