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 and gprof debug info ], [ DEBUGGING=2; ], [])
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(migemo,             [  --with-migemo             enable migemo for japanese romaji completion ], [ ENABLE_MIGEMO=1 ], [ ENABLE_MIGEMO=0 ])
AC_ARG_WITH(migemo-dir,         [  --with-migemo-dir=DIR     set migemo install prefix ], [ MIGEMO_DIR="$withval"  ], [ MIGEMO_DIR= ])
AC_ARG_WITH(readline-dir,         [  --with-readline-dir=DIR     set readline install prefix ], [ READLINE_DIR="$withval"  ], [ READLINE_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/utf-8/migemo-dict; then SYSTEM_MIGEMODIR=/usr/local/share/migemo; else SYSTEM_MIGEMODIR=/usr/share/cmigemo; fi ] )

AC_SUBST(SYSTEM_MIGEMODIR)

#####################################################################
# OSの判定 cygwinとLinuxのみ, <--- FreeBSD追加 Darwin追加
#####################################################################
AC_MSG_CHECKING(Operating System)

if test `uname -s | sed -e 's/\(.*\)-\(.*\)/\1/'` = "CYGWIN_NT"; then
    if test `uname -a | perl -ne 'if(/CYGWIN_NT-\S+\s\S+\s(\d+\.\d+)\.\d+/) { print $1, "\n" }'` = "1.7"; then
        echo "CYGWIN_NT1.7"

        OS=CYGWIN

	libdir=/usr/bin
	bindir=/usr/bin
	
	AC_SUBST(libdir)

        AC_DEFINE(__CYGWIN__, 1)
    else
        echo "saphire supports cygwin 1.7 later"
        exit
    fi
    
    CYGWIN_DLL=saphire.dll
    AC_SUBST(CYGWIN_DLL)
    LIBSAPHIRESO=libsaphire.so
    LIBSAPHIRESO1=libsaphire.so.1.0
elif test `uname -s` = "Darwin"; then
    echo "Darwin"

    OS=DARWIN

    AC_DEFINE(__DARWIN__, 1)

    LIBSAPHIRESO=libsaphire.dylib
    LIBSAPHIRESO1=libsaphire.1.0.dylib
elif test `uname -s` = "SunOS"; then
    echo "SunOS"

    OS=SUNOS

    AC_DEFINE(__SUNOS__, 1)

    LIBSAPHIRESO=libsaphire.so
    LIBSAPHIRESO1=libsaphire.so.1.0
elif test `uname -s` = "FreeBSD"; then
    echo "FreeBSD"

    OS=FREEBSD

    AC_DEFINE(__FREEBSD__, 1)

    LIBSAPHIRESO=libsaphire.so
    LIBSAPHIRESO1=libsaphire.so.1.0
else
    echo "Linux"

    OS=Linux

    AC_DEFINE(__LINUX__, 1)

    LIBSAPHIRESO=libsaphire.so
    LIBSAPHIRESO1=libsaphire.so.1.0
fi

AC_SUBST(LIBSAPHIRESO)
AC_SUBST(LIBSAPHIRESO1)

##########################################################################
# CFLAGSを設定する
##########################################################################
if test "$OS" = CYGWIN
then
    CFLAGS="-I. -I$myprefix/include -L$myprefix/lib -L."
else
    CFLAGS="-I. -I$myprefix/include -L$myprefix/lib -fPIC -L."
fi

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 -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 "$READLINE_DIR"
then
    if test -e "$READLINE_DIR/include/readline/readline.h" && test -e "$READLINE_DIR/lib/libreadline.a"
    then
         CFLAGS="$CFLAGS -I$READLINE_DIR/include -L$READLINE_DIR/lib"
    else
        echo "can't found readline/readline.h in $READLINE_DIR/include or libreadline.a in $READLINE_DIR/lib"
    fi
fi

if test $DEBUGGING = 1
then
    if uname|grep -q CYGWIN
    then
        CFLAGS="$CFLAGS -g -DMDEBUG -Werror"
    else
        CFLAGS="$CFLAGS -g -DMDEBUG -Werror -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast"
    fi
elif test $DEBUGGING = 2
then
    CFLAGS="$CFLAGS -pg -Werror -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast"
else
    if uname|grep -q CYGWIN
    then
        if test $(uname -m) = i686    # 最適化すると他のアーキテクチャで落ちる
            CFLAGS="$CFLAGS -O2"
        then
            CFLAGS="$CFLAGS"
        fi
    else
        if test $(uname -m) = i686    # 最適化すると他のアーキテクチャで落ちる
            CFLAGS="$CFLAGS -O2 -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast"
        then
            CFLAGS="$CFLAGS -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast"
        fi
    fi
fi

AC_SUBST(CFLAGS)

#####################################################################
# ncurses(必須)
#####################################################################
AC_CHECK_HEADER(ncurses.h, [NCURSES_OK=1; AC_DEFINE(HAVE_NCURSES_H, 1)])
AC_CHECK_HEADER(ncurses/ncurses.h, [NCURSES_OK=1; AC_DEFINE(HAVE_NCURSES_NCURSES_H, 1)])
AC_HAVE_LIBRARY(curses, [ LIBS="$LIBS -lcurses"; LIBS2="$LIBS2 -lcurses"; NCURSES_OK2=1 ])
AC_HAVE_LIBRARY(ncurses, [ LIBS="$LIBS -lncurses"; LIBS2="$LIBS2 -lncurses"; NCURSES_OK2=1 ])

if test -z "$NCURSES_OK"
then
    exit
fi

if test -z "$NCURSES_OK2"
then
    exit
fi

#####################################################################
# math(必須)
#####################################################################
AC_CHECK_HEADER(math.h, [AC_DEFINE(HAVE_MATH_H,1)], [ exit ])
AC_HAVE_LIBRARY(m, [ LIBS="$LIBS -lm"; LIBS2="$LIBS2 -lm" ], [ exit ])

######################################################################
# readline(必須)
######################################################################
AC_CHECK_HEADER(readline/readline.h, [AC_DEFINE(HAVE_READLINE_H, 1)], [ exit ])
AC_CHECK_HEADER(readline/history.h, [AC_DEFINE(HAVE_READLINE_HISTORY_H, 1)], [ exit ])
AC_HAVE_LIBRARY(readline, [ LIBS="$LIBS -lreadline" ], [ exit ])

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

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

#####################################################################
# 鬼車(必須)
#####################################################################
AC_CHECK_HEADER(oniguruma.h, [AC_DEFINE(HAVE_ONIGURUMA_H, 1)], [ exit ])
AC_HAVE_LIBRARY(onig, [ LIBS="$LIBS -lonig"; LIBS2="$LIBS2 -lonig" ], [ exit ])

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

AC_SUBST(LIBS)
AC_SUBST(LIBS2)

##########################################################################
# 型のチェック
###########################################################################

AC_EGREP_HEADER(OnigUChar, oniguruma.h, AC_DEFINE(HAVE_ONIGUCHAR))



AC_OUTPUT(Makefile)

