AC_INIT(src/main.c)

AC_CONFIG_HEADER(config.h)

AC_PROG_CC
AC_PROG_INSTALL
AC_C_BIGENDIAN

if test $prefix = NONE
then
    myprefix=/usr/local
else
    myprefix=$prefix
fi

######################################################################
# configure arguments
######################################################################
DEBUGGING=0
LOGGING=0

AC_ARG_WITH(debug,              [  --with-debug              include debug info ], [ DEBUGGING=1; ], [])
AC_ARG_WITH(gprof-debug,        [  --with-gprof-debug        include debug info and gprof debug info ], [ DEBUGGING=2; ], [])
AC_ARG_WITH(log,                [  --with-log                create log ], [ LOGGING=1; ], [])
AC_ARG_WITH(console-log,                [  --with-console-log                create console log ], [ LOGGING=2; ], [])
AC_ARG_WITH(optimize,           [  --with-optimize           with optimize(-O in CFLAGS) ], [ ENABLE_OPTIMIZE=1 ], [ ENABLE_OPTIMIZE=0 ])
AC_ARG_WITH(onig-dir,           [  --with-onig-dir=DIR       set install prefix of oniguruma which located at non standerd directory and installed by user ], [ ONIG_DIR="$withval"; ], [ ONIG_DIR=; ])
AC_ARG_WITH(static,             [ --with-static       link xyzsh used libraries with static to make xyzsh doesn't require any libraries.], [ STATIC_LINK=1 ], [STATIC_LINK= ] )

#####################################################################
# check cpu type
#####################################################################
AC_MSG_CHECKING(CPU type)

if test `uname -m` = x86_64
then
    echo x86_64

    AC_DEFINE(__64BIT_CPU__, 1)
else
    echo 32BIT_CPU

    AC_DEFINE(__32BIT_CPU__, 1)
fi

#####################################################################
# check operating systems
#####################################################################
SO_VERSION=1.0.0
AC_SUBST(SO_VERSION)
AC_MSG_CHECKING(Operating System)

OBJ="src/main.o src/utf_mb_str.o src/heap.o src/buffer.o src/klass.o src/vm.o src/debug.o src/xfunc.o src/interface.o src/type.o src/c_to_clover.o src/obj_clover.o src/obj_int.o src/obj_byte.o src/obj_short.o src/obj_uint.o src/obj_long.o src/obj_char.o src/obj_bytes.o src/obj_float.o src/obj_double.o src/obj_bool.o src/obj_pointer.o src/obj_null.o src/obj_class_object.o src/obj_user_object.o src/obj_string.o src/obj_string_buffer.o src/obj_parser.o src/obj_oniguruma_regex.o src/obj_array.o src/obj_hash.o src/obj_range.o src/obj_block.o src/obj_system.o src/obj_thread.o src/obj_mutex.o src/obj_file.o src/obj_file.o src/obj_type_object.o src/obj_void.o src/obj_anonymous.o src/obj_field.o src/obj_method.o src/obj_enum.o src/obj_wait_status.o src/obj_tm.o"

AC_SUBST(OBJ)

CFLAGS='-DSYSCONFDIR="\"${sysconfdir}/\"" -DDOCDIR="\"${docdir}/\"" -DDATAROOTDIR="\"${datarootdir}/\""'

if test `uname -s` = "Darwin"; then
    echo "Darwin"

    OS=DARWIN

    AC_DEFINE(__DARWIN__, 1)

    LIBSONAME=libclover.dylib
    LIBSO2NAME=libclover.$SO_VERSION.dylib

    if test $myprefix = /usr/local; then
        CFLAGS="-Isrc/ -I. -L . -fPIC $CFLAGS -Qunused-arguments"
    else 
        CFLAGS="-Isrc/ -I. -L . -I$myprefix/include -L$myprefix/lib -fPIC $CFLAGS -Qunused-arguments"
    fi
elif test `uname -s` = "SunOS"; then
    echo "SunOS"

    OS=SUNOS

    AC_DEFINE(__SUNOS__, 1)

    LIBSONAME=libclover.so
    LIBSO2NAME=libclover.so.$SO_VERSION

    if test $myprefix = /usr/local; then
        CFLAGS="-Isrc/ -I. -L . -fPIC $CFLAGS"
    else 
        CFLAGS="-Isrc/ -I. -L . -I$myprefix/include -L$myprefix/lib -fPIC $CFLAGS"
    fi
elif test `uname -s` = "FreeBSD"; then
    echo "FreeBSD"

    OS=FREEBSD

    AC_DEFINE(__FREEBSD__, 1)

    LIBSONAME=libclover.so
    LIBSO2NAME=libclover.so.$SO_VERSION

    CFLAGS="-Isrc/ -I. -L . -I$myprefix/include -L$myprefix/lib -fPIC $CFLAGS"
elif uname -s | grep -q CYGWIN_NT; then
    echo CYGWIN

    OS=CYGWIN

    AC_DEFINE(__CYGWIN__, 1)

    LIBSONAME=libclover.so
    LIBSO2NAME=libclover.so.$SO_VERSION

    if test $myprefix = /usr/local; then
        CFLAGS="-Isrc/ -I. -L . $CFLAGS"
    else 
        CFLAGS="-Isrc/ -I. -L . -I$myprefix/include -L$myprefix/lib $CFLAGS"
    fi
else
    echo "Linux"

    OS=LINUX

    AC_DEFINE(__LINUX__, 1)

    LIBSONAME=libclover.so
    LIBSO2NAME=libclover.so.$SO_VERSION

    if test $myprefix = /usr/local; then
        CFLAGS="-Isrc/ -I. -L . -fPIC $CFLAGS"
    else
        CFLAGS="-Isrc/ -I. -L . -I$myprefix/include -L$myprefix/lib -fPIC $CFLAGS"
    fi

    AC_HAVE_LIBRARY(util, [ LIBS="$LIBS -lutil"; ], [])
fi

##########################################################################
# CFLAGS settings
##########################################################################
if test -d /usr/local/include
then
    CFLAGS="$CFLAGS -I/usr/local/include"
fi

if test -d /usr/local/lib
then
    CFLAGS="$CFLAGS -L /usr/local/lib"
fi

if test -d /opt/local/include
then
    CFLAGS="$CFLAGS -I/opt/local/include"
fi

if test -d /opt/local/lib
then
    CFLAGS="$CFLAGS -L /opt/local/lib"
fi

if test -n "$ONIG_DIR"
then
    if test -e "$ONIG_DIR/include/oniguruma.h" && test -e "$ONIG_DIR/lib/libonig.a"
    then
         CFLAGS="$CFLAGS -I$ONIG_DIR/include -L $ONIG_DIR/lib"
    else
        echo "can't found oniguruma.h in $ONIG_DIR/include or libonig.a in $ONIG_DIR/lib"
    fi
fi

if test $ENABLE_OPTIMIZE = 1
then
    CFLAGS="$CFLAGS -O3"
fi

if test $DEBUGGING = 1
then
    CFLAGS="$CFLAGS -g -DMDEBUG -Werror"
elif test $DEBUGGING = 2
then
    CFLAGS="$CFLAGS -pg"
else
    CFLAGS="$CFLAGS"
fi

if test $LOGGING = 1
then
    CFLAGS="$CFLAGS -DVM_DEBUG"
fi

if test $LOGGING = 2
then
    CFLAGS="$CFLAGS -DVM_DEBUG2"
fi

if test "$STATIC_LINK" = 1
then
    CFLAGS="$CFLAGS -static"

    LIBANAME=libclover.a
    LIBSONAME=
    LIBSO2NAME=
fi

AC_SUBST(OS)
AC_SUBST(LIBANAME)
AC_SUBST(LIBSONAME)
AC_SUBST(LIBSO2NAME)
AC_SUBST(CFLAGS)

##########################################################
# environmnet variables
##########################################################
CC=gcc
INSTALL="/usr/bin/install -c"

#####################################################################
# oniguruma settings
#####################################################################
AC_CHECK_HEADER(oniguruma.h, [AC_DEFINE(HAVE_ONIGURUMA_H, 1)], [ exit ])
AC_HAVE_LIBRARY(onig, [ LIBS="$LIBS -lonig"; ], [ exit ])

#####################################################################
# pthread settings
#####################################################################
AC_CHECK_HEADER(pthread.h, [AC_DEFINE(HAVE_PTHREAD_H, 1)], [ exit ])
AC_HAVE_LIBRARY(pthread, [ LIBS="$LIBS -lpthread"; ], [ exit ])

#####################################################################
# readline settings
#####################################################################
AC_CHECK_HEADER(readline/readline.h, [AC_DEFINE(HAVE_READLINE_H, 1)], [ exit ])
AC_HAVE_LIBRARY(readline, [ LIBS="$LIBS -lreadline"; ], [ exit ])

#####################################################################
# dl settings(optional)
#####################################################################
if test "$OS" != FREEBSD
then
    AC_HAVE_LIBRARY(dl, [ LIBS="$LIBS -ldl"; ], [])
fi

#####################################################################
# math settings(optional)
#####################################################################
AC_CHECK_HEADER(math.h, [AC_DEFINE(HAVE_MATH_H,1)], [])
AC_HAVE_LIBRARY(m, [ LIBS="$LIBS -lm"; ], [])

####################################################################
# iconv settings(optional)
####################################################################
AC_CHECK_HEADER(iconv.h, [AC_DEFINE(HAVE_ICONV_H, 1)], [])

if test "$OS" = CYGWIN|| test "$OS" = DARWIN || test "$OS" = FREEBSD
then
    LIBS="$LIBS -liconv"
fi

#####################################################################
# nanosleep settings
#####################################################################
AC_CHECK_FUNCS(nanosleep, [AC_DEFINE(HAVE_NANOSLEEP, 1)], [])

AC_SUBST(LIBS)

#####################################################################
# ncurses settings(optiolanl)
#####################################################################
AC_CHECK_HEADER(ncurses.h, [NCURSES_H_OK=1; AC_DEFINE(HAVE_NCURSES_H, 1)])
AC_CHECK_HEADER(ncurses/ncurses.h, [NCURSES_H_OK=1; AC_DEFINE(HAVE_NCURSES_NCURSES_H, 1)])
AC_HAVE_LIBRARY(cursesw, [ NCURSES_LIB_OK=1; LIBS="$LIBS -lcursesw" ])
AC_HAVE_LIBRARY(ncursesw, [ NCURSES_LIB_OK=1; LIBS="$LIBS -lncursesw" ])

if test -z "$NCURSES_LIB_OK"
then
    AC_HAVE_LIBRARY(curses, [ NCURSES_LIB_OK=1; LIBS="$LIBS -lcurses" ])
    AC_HAVE_LIBRARY(ncurses, [ NCURSES_LIB_OK=1; LIBS="$LIBS -lncurses" ])
fi

#####################################################################
# editline settings(optional)
#####################################################################
#AC_CHECK_HEADER(histedit.h, [EDITLINE_H_OK=1; AC_DEFINE(HAVE_HISTEDIT_H, 1)], [])
#AC_HAVE_LIBRARY(edit, [ EDITLINE_LIB_OK=1; LIBS="$LIBS -ledit"; ], [])

#####################################################################
# interructive clover settings
#####################################################################
#if test -n "$NCURSES_H_OK" && test -n "$NCURSES_LIB_OK" && test -n "$EDITLINE_H_OK" && test -n "$EDITLINE_LIB_OK"
#then
#    ICLOVER=iclover
#    AC_SUBST(ICLOVER)
#fi

##########################################################################
# type checking
###########################################################################
AC_EGREP_HEADER(OnigUChar, oniguruma.h, AC_DEFINE(HAVE_ONIGUCHAR))

AC_OUTPUT(Makefile)

