
AC_PREREQ([2.59c])
AC_INIT([nolambocoin-miner], [1.0])
AM_INIT_AUTOMAKE([foreign])

AC_CONFIG_SRCDIR([cpu-miner.c])
AC_CONFIG_HEADERS([cpuminer-config.h])

AM_CONDITIONAL([TARGET_RASPBERRY], [false])
AM_CONDITIONAL([TARGET_NOARM], [false])
AM_CONDITIONAL([TARGET_X86_64], [false])
AM_CONDITIONAL([TARGET_MACOS], [false])
AM_CONDITIONAL([TARGET_WINDOWS], [false])
AM_CONDITIONAL([TARGET_DEFAULT], [false])

# Füge die Makefile-Konfiguration hinzu
AC_CONFIG_FILES([Makefile
                 compat/Makefile
                 compat/jansson/Makefile
                 ])

# Lade pkg-config Makros
PKG_PROG_PKG_CONFIG

AM_MAINTAINER_MODE

EXTERNAL_CFLAGS="$CFLAGS"

# Checks for programs
AC_PROG_CC
AC_PROG_GCC_TRADITIONAL
AM_PROG_CC_C_O
AM_PROG_AS
AC_PROG_RANLIB

if test -n "$EXTERNAL_CFLAGS"; then
    CFLAGS="$EXTERNAL_CFLAGS"
else
    CFLAGS='-Wall -O2 -fomit-frame-pointer'
fi

# Checks for header files
AC_CHECK_HEADERS([sys/endian.h sys/param.h syslog.h sys/sysctl.h])

AC_CHECK_DECLS([be32dec, le32dec, be32enc, le32enc], [],
    [],
    [
        AC_INCLUDES_DEFAULT
        #ifdef HAVE_SYS_ENDIAN_H
        #include <sys/endian.h>
        #endif
    ])

AC_FUNC_ALLOCA
AC_CHECK_FUNCS([getopt_long])

# Pthread Flags
AC_CHECK_LIB([pthread], [pthread_create],
    [PTHREAD_LIBS="-lpthread"],
    [AC_MSG_ERROR([pthread library not found.])])
AC_SUBST([PTHREAD_LIBS])

# Ersetze AC_CHECK_FLAGS durch AC_COMPILE_IFELSE
AC_MSG_CHECKING([whether -pthread flag is supported])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])],
  [PTHREAD_CFLAGS="-pthread"],
  [PTHREAD_CFLAGS=""])
AC_MSG_RESULT([$PTHREAD_CFLAGS])
AC_SUBST([PTHREAD_CFLAGS])

# WS2 Libraries (Windows)
WS2_LIBS=""
have_win32=false

# Plattform-Erkennung
UNAME_S=`uname -s`
UNAME_M=`uname -m`

AC_MSG_CHECKING([for target platform])
AC_MSG_RESULT([$UNAME_S $UNAME_M])

case "$UNAME_S" in
    Linux*)
        case "$UNAME_M" in
            armv7l*|armv8*|aarch64*)
                if grep -q "Raspberry Pi" /proc/device-tree/model 2>/dev/null; then
                    AC_DEFINE([TARGET_RASPBERRY], [1], [Define to 1 if compiling for Raspberry])
                    AM_CONDITIONAL([TARGET_RASPBERRY], [true])
                    AM_CONDITIONAL([TARGET_NOARM], [false])
                    AM_CONDITIONAL([TARGET_X86_64], [false])
                    AM_CONDITIONAL([TARGET_MACOS], [false])
                    AM_CONDITIONAL([TARGET_WINDOWS], [false])
                    AC_MSG_NOTICE([Target platform: Raspberry Pi])
                else
                    AC_DEFINE([TARGET_NOARM], [1], [Define to 1 if compiling for ARM Server])
                    AM_CONDITIONAL([TARGET_NOARM], [true])
                    AM_CONDITIONAL([TARGET_RASPBERRY], [false])
                    AM_CONDITIONAL([TARGET_X86_64], [false])
                    AM_CONDITIONAL([TARGET_MACOS], [false])
                    AM_CONDITIONAL([TARGET_WINDOWS], [false])
                    AC_MSG_NOTICE([Target platform: ARM Server])
                fi
                ;;
            x86_64*)
                AC_DEFINE([TARGET_X86_64], [1], [Define to 1 if compiling for x86_64])
                AM_CONDITIONAL([TARGET_X86_64], [true])
                AM_CONDITIONAL([TARGET_RASPBERRY], [false])
                AM_CONDITIONAL([TARGET_NOARM], [false])
                AM_CONDITIONAL([TARGET_MACOS], [false])
                AM_CONDITIONAL([TARGET_WINDOWS], [false])
                AC_MSG_NOTICE([Target platform: x86_64])
                ;;
            *)
                AC_MSG_ERROR([Unsupported architecture: $UNAME_M])
                ;;
        esac
        ;;
    Darwin*)
        AC_DEFINE([TARGET_MACOS], [1], [Define to 1 if compiling for macOS])
        AM_CONDITIONAL([TARGET_MACOS], [true])
        AM_CONDITIONAL([TARGET_RASPBERRY], [false])
        AM_CONDITIONAL([TARGET_NOARM], [false])
        AM_CONDITIONAL([TARGET_X86_64], [false])
        AM_CONDITIONAL([TARGET_WINDOWS], [false])
        AC_MSG_NOTICE([Target platform: macOS])
        ;;
    CYGWIN*|MINGW32*|MSYS*|MINGW*)
        AC_DEFINE([TARGET_WINDOWS], [1], [Define to 1 if compiling for Windows])
        AM_CONDITIONAL([TARGET_WINDOWS], [true])
        AM_CONDITIONAL([TARGET_RASPBERRY], [false])
        AM_CONDITIONAL([TARGET_NOARM], [false])
        AM_CONDITIONAL([TARGET_X86_64], [false])
        AM_CONDITIONAL([TARGET_MACOS], [false])
        AC_MSG_NOTICE([Target platform: Windows])
        ;;
    *)
        AC_MSG_ERROR([Unsupported operating system: $UNAME_S])
        ;;
esac

AM_CONDITIONAL([WANT_JANSSON], [true])

# Checks für libcurl
PKG_CHECK_MODULES([CURL], [libcurl >= 7.0], [],
    [AC_MSG_ERROR([libcurl is required but not installed.])])

# Checks für jansson
PKG_CHECK_MODULES([JANSSON], [jansson >= 2.0], [],
    [AC_MSG_ERROR([jansson is required but not installed.])])

AC_SUBST([JANSSON_CFLAGS])
AC_SUBST([JANSSON_LIBS])
AC_SUBST([CURL_CFLAGS])
AC_SUBST([CURL_LIBS])
AC_SUBST([PTHREAD_CFLAGS])
AC_SUBST([PTHREAD_LIBS])
AC_SUBST([WS2_LIBS])

AC_OUTPUT
