AC_INIT(main.c)

AC_CONFIG_HEADER(config.h)

AC_PROG_CC
AC_PROG_INSTALL

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

##########################################################################
# 引数
##########################################################################
DEBUGGING=0

AC_ARG_WITH(debug,              [  --with-debug              include debug info ], [ DEBUGGING=1; ], [])
AC_ARG_WITH(gprof-debug,        [  --with-gprof-debug        include debug info ], [ DEBUGGING=2; ], [])
AC_ARG_WITH(socket,             [  --with-socket             enable remote control ], [ ENABLE_SOCKET=1; ], [])
AC_ARG_WITH(migemo,             [  --with-migemo             enable migemo ], [ ENABLE_MIGEMO=1 ], [ ENABLE_MIGEMO=0 ])
AC_ARG_WITH(migemo-dir,         [  --with-migemo-dir=DIR     set migemo directory ], [ MIGEMO_DIR="$with_val"  ], [ MIGEMO_DIR= ])
AC_ARG_WITH(onig-dir,           [  --with-onig-dir=DIR       set oniguruma directory ], [ ONIG_DIR="$with_val"  ], [ ONIG_DIR= ])
AC_ARG_WITH(saphire-dir,        [  --with-saphire-dir=DIR    set saphire directory ], [ SAPHIRE_DIR="$with_val"  ], [ SAPHIRE_DIR= ])
AC_ARG_WITH(system-migemodir,[ --with-system-migemodir       specify the directory of system migemo dictionary. /usr/share/migemo, for example], [ if test -n "$withval" && test -n $LIBMIGEMO ; then echo "system-migemodir = $withval" ; SYSTEM_MIGEMODIR="$withval" ; { AC_DEFINE_UNQUOTED(SYSTEM_MIGEMODIR, "$withval") } ; fi ], [ if test -e /usr/local/share/migemo; then SYSTEM_MIGEMODIR=/usr/local/share/migemo; else SYSTEM_MIGEMODIR=/usr/share/cmigemo; fi ] )

if test $DEBUGGING = 1
then
    CFLAGS="-I$myprefix/include -L$myprefix/lib -I . -I/usr/local/include -L/usr/local/lib -g -DMDEBUG -Werror"
elif test $DEBUGGING = 2
then
    CFLAGS="-I$myprefix/include -L$myprefix/lib -I . -I/usr/local/include -L/usr/local/lib -g -pg -DMDEBUG -Werror"
else
    CFLAGS="-I$myprefix/include -L$myprefix/lib -I . -I/usr/local/include -L/usr/local/lib -O2"
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 -n "$MIGEMO_DIR"
then
    if test -e "$MIGEMO_DIR/include/migemo.h" && test -e "$MIGEMO_DIR/lib/libmigemo.so"
    then
        CFLAGS="$CFLAGS -I$MIGEMO_DIR/include -L$MIGEMO_DIR/lib"
    else
        echo "can't found migemo.h in $MIGEMO_DIR/include or libmigemo.so in $MIGEMO_DIR/lib"
    fi
fi

if test -n "$SAPHIRE_DIR"
then
    if test -e "$SAPHIRE_DIR/include/saphire.h" && test -e "$SAPHIRE_DIR/lib/libsaphire.so"
    then
         CFLAGS="$CFLAGS -I$SAPHIRE_DIR/include -L$SAPHIRE_DIR/lib"
    else
        echo "can't found readline/readline.h in $SAPHIRE_DIR/include or libreadline.a in $SAPHIRE_DIR/lib"
    fi
fi

if test -n "$ENABLE_SOCKET"
then
    CFLAGS="$CFLAGS -DENABLE_SOCKET"
fi

AC_SUBST(CFLAGS)
AC_SUBST(SYSTEM_MIGEMODIR)

#####################################################################
# OSの判定 cygwinとLinuxのみ
#####################################################################
AC_MSG_CHECKING(Operating System)

if test `uname -s | sed -e 's/\(.*\)-\(.*\)/\1/'` = "CYGWIN_NT"; then
    echo "CYGWIN_NT"
    AC_DEFINE(__CYGWIN__, 1)
    
    CYGWIN_DLL=saphire.dll
    AC_SUBST(CYGWIN_DLL)
elif test `uname -s` = "Darwin"; then
    echo "Darwin"

    AC_DEFINE(__DARWIN__, 1)
else
    echo "Linux"

    AC_DEFINE(__LINUX__, 1)
fi

###########################################################################
# cursesのインストールの有無を調べる
###########################################################################
AC_CHECK_HEADER(ncurses.h, [NCURSES_OK=1; LIBS="$LIBS -lncurses"; AC_DEFINE(HAVE_NCURSES_H, 1)])
AC_CHECK_HEADER(ncurses/ncurses.h, [NCURSES_OK=1;LIBS="$LIBS -lncurses";  AC_DEFINE(HAVE_NCURSES_NCURSES_H, 1)])

if test -z NCURSES_OK
then
    exit
fi

###########################################################################
# 数学ライブラリのインストールの有無を調べる
###########################################################################
AC_CHECK_HEADER(math.h, [LIBS="$LIBS -lm"; AC_DEFINE(HAVE_MATH_H, 1)])

##########################################################################
# 鬼車のインストールの有無を調べる
###########################################################################
AC_CHECK_HEADER(oniguruma.h, [LIBS="$LIBS -lonig"; AC_DEFINE(HAVE_ONIGURUMA_H, 1)])

##########################################################################
# saphireのインストールの有無を調べる
###########################################################################
AC_CHECK_HEADER(saphire.h, [LIBS="$LIBS -lsaphire"; AC_DEFINE(HAVE_SAPHIRE_H, 1)])

####################################################################
# iconv(必須)
####################################################################
AC_CHECK_HEADER(iconv.h, [AC_DEFINE(HAVE_ICONV_H, 1)], [ exit ])

##########################################################################
# migemoのインストールの有無を調べる
###########################################################################
if test $ENABLE_MIGEMO = 1
then
    AC_CHECK_HEADER(migemo.h, [LIBS="$LIBS -lmigemo"; AC_DEFINE(HAVE_MIGEMO_H, 1)])
fi

AC_SUBST(LIBS)

AC_OUTPUT(Makefile)

