dnl= Template file to create configure script for gt4f90io
dnl
dnl  Authors::   Eizi TOYODA, Yasuhiro Morikawa
dnl  Version::   $Id: configure.in,v 1.21 2006/12/28 11:27:28 morikawa Exp $
dnl  Tag Name::  $Name: gt4f90io-20070305 $
dnl  Copyright:: Copyright (C) GFD Dennou Club, 2000-2006. All rights reserved.
dnl  License::   See COPYRIGHT[link:COPYRIGHT]
dnl
dnl== Overview
dnl
dnl This file is template file to create "configure" script by
dnl autoconf 2.59. Some functions are defined in "aclocal.m4"
dnl (for example, DC_ARG_WITH).
dnl

############################################################
#== This puts the cvs ID tag in the output configure script.
AC_REVISION($Id: configure.in,v 1.21 2006/12/28 11:27:28 morikawa Exp $)dnl

############################################################
#== StartUp
#
## Running autoconf on this file will trigger a warning if 
## autoconf is not at least version 2.59.
AC_PREREQ(2.59)

## Initialize autoconf with name of project, version, and support
## email address. 
AC_INIT(gt4f90io, 0.1, dcstaff@gfd-dennou.org)

## Autoconf will check for the existance of this file before proceeding.
AC_CONFIG_SRCDIR([src/gt4f90io.f90])
AC_PREFIX_DEFAULT(/usr/local/gt4f90io)

# Find out about the host we're building on.
AC_CANONICAL_HOST

############################################################
#== Set prefix
#
if test X"$prefix" != X"NONE" ; then
	ac_cv_prefix=`echo $prefix | sed 's/\/\+$//'`
fi

AC_CACHE_CHECK([prefix], ac_cv_prefix, [
		ac_cv_prefix=$ac_default_prefix
])
prefix=`echo $ac_cv_prefix | sed 's/\/\+$//'`

############################################################
#== Check cross-compiling
#
CROSS_COMPILING=$cross_compiling
AC_SUBST(CROSS_COMPILING)

############################################################
#== Check fortran Compiler and Module type
#
sh ./chkfort.sh || exit
eval `cat chkfort.cfg`

AC_SUBST(FC)
ac_cv_FC=$FC
AC_SUBST(MOD)
AC_SUBST(MODPATHFLAG)
AC_SUBST(F90MODTYPE)
: ${IFCCEM:=:}
AC_SUBST(IFCCEM)
AC_SUBST(SYSDEPENV)

if test X"$MOD" = X".f90" ; then
	CLEAN_MODS=
elif test X"$MOD" != X"" ; then
	CLEAN_MODS="*$MOD"
else
	CLEAN_MODS=
fi
AC_SUBST(CLEAN_MODS)


#
#== Set host type HOST_CPU
HOST_CPU=$host_cpu
AC_SUBST(HOST_CPU)

############################################################
#== Set Install Command
#

case "$build" in
powerpc-ibm-aix*)
	INSTALL="\$(CP)"
	INSTALLBIN="\$(CP)"
	INSTALLDIR="\$(MKDIR)"
	echo "\"install\" command is not used"
	;;
*)
	INSTALL="install -m 644"
	INSTALLBIN="install -m 755"
	INSTALLDIR="install -d -m 755"
	;;
esac
AC_SUBST(INSTALL)
AC_SUBST(INSTALLBIN)
AC_SUBST(INSTALLDIR)

dnl AC_PROG_INSTALL
dnl DC_MOD_INSTALL

############################################################
#
#== Set variables LINKF, which means link processes.
#
# This code is imported from the patch of netcdf 3.6.0-p1 for
# SX and ES by T.Kagimoto, and modified.
#
#=== Specified by user --with-linker option
#
DC_ARG_WITH(linker, [Linker (If you use non-fortran compiler)], ac_cv_LINKER, [
		AC_MSG_RESULT((no))
])

if test X"$ac_cv_LINKER" != X"" ; then
    LINKF="$ac_cv_LINKER \$(INCLUDE_GT) \$(INCLUDES)"
else
    LINKF='$(FC) $(INCLUDE_GT) $(FFLAGS) $(SYSFFLAGS) $(INCLUDES)'
    case "$host_cpu" in
        sx5*|sx6*|sx7*|sx8*|es*)
	    if test ! "$CROSS_COMPILING" = "yes" && \
	      test ! "$CROSS_COMPILING" = "maybe" ; then
	        LINKF='c++ $(INCLUDE_GT) $(INCLUDES) -f90lib'
	    else
	        LINKF='sxc++ $(INCLUDE_GT) $(INCLUDES) -f90lib'
	    fi
            ;;
        *)
            ;;
    esac
fi

AC_SUBST(LINKF)

############################################################
#== Set GT4DIR
DC_SET_GT4DIR


dnl #
dnl #== CharSet
dnl #
dnl 
dnl AC_CHECK_PROG(SJISTOEUC, nkf, nkf -xe)
dnl AC_CHECK_PROG(SJISTOEUC, iconv, iconv -f SHIFT_JIS -t EUC-JP)
dnl AC_CHECK_PROG(EUCTOSJIS, nkf, nkf -xs)
dnl AC_CHECK_PROG(EUCTOSJIS, iconv, iconv -f EUC-JP -t SHIFT_JIS)
dnl 
dnl AC_ARG_WITH(charset, [charset: of source and resource (shift_jis or euc-jp)], [
dnl 	ac_cv_sys_charset=$withval
dnl ], [
dnl AC_CACHE_CHECK([charset for source/resource], ac_cv_sys_charset, [
dnl 	case $SYSTYPE in
dnl 	*BSD*|Linux*|SunOS-4.*)
dnl 		ac_cv_sys_charset=euc-jp
dnl 		;;
dnl 	*)
dnl 		ac_cv_sys_charset=shift_jis
dnl 		;;
dnl 	esac
dnl ])
dnl ])
dnl CHARSET=$ac_cv_sys_charset
dnl AC_SUBST(CHARSET)

############################################################
#== Library Check

	#=== Netcdf
	#
	DC_ARG_WITH(netcdf, [netcdf library filename], ac_cv_lib_netcdf, [
		AC_MSG_ERROR(specify netcdf library filename (like libnetcdf.a) with --with-netcdf=)
	])

	DC_ARG_WITH(netcdf-include, [netcdf include filename], ac_cv_netcdf_include,, [
		ac_cv_netcdf_include=""
	])

	DC_SET_LIBDIR_LIBNAME($ac_cv_lib_netcdf, netcdf_libdir, netcdf_libname)

	if test -n "$ac_cv_netcdf_include" && test -f "$ac_cv_netcdf_include"; then
	    netcdf_incdir=`dirname $ac_cv_netcdf_include`
	else
	    try2=`dirname $netcdf_libdir`/include
	    try2="$try2 `echo $netcdf_libdir | sed -e 's|/lib/|/include/|' -e 's|/lib$|/include|'`"
	    for try3 in $try2; do
	        if test -f "$try3/netcdf.h"; then
		    netcdf_incdir=$try3
		    break
	        fi
	    done
	fi

	test -d "$netcdf_incdir" || AC_MSG_ERROR(netcdf include file not found. specify netcdf include filename (like /usr/lib/netcdf.h) with --with-netcdf-include=)


############################################################
#== Setting LDFLAGS, LDLIBS, FFLAGS
#
SYSLDFLAGS="-L$netcdf_libdir $SYSLDFLAGS"
SYSLDLIBS="-l$netcdf_libname $SYSLDLIBS"
SYSFFLAGS="$SYSFFLAGS $ac_cv_fflags"

############################################################
#== --with-docdir=docidir
#
ac_cv_dest_doc='${prefix}/doc'
DC_ARG_WITH(docdir,
		[document installation directory (default /usr/local/gt4f90io/doc)],
		ac_cv_dest_doc, [
        AC_MSG_ERROR(specify docdir with --with-docdir=DIR)
])

case $ac_cv_dest_doc in

"/"*)
	docdir=${ac_cv_dest_doc} ;;

'${prefix}/doc')
	docdir=${ac_cv_dest_doc} ;;

no)
	docdir='${prefix}/doc' ;;

*)
	echo -n 'reconfigure with --with-docdir=DIR, '
	echo 'but DIR must be ABSOLUTE path name.'
	AC_MSG_ERROR(unrecognized docdir type) ;;
esac

AC_SUBST(docdir)



############################################################
#== --with-gt4libname=ARG
#

default_gt4libname=gt4f90io
ac_cv_gt4libname=${default_gt4libname}
DC_ARG_WITH(gt4libname,	[Library Name (default gt4f90io)],
	ac_cv_gt4libname,
	[AC_MSG_ERROR(specify Library Name --with-gt4libname=ARG)]
)

case $ac_cv_gt4libname in
*'/'*)
	gt4libname=`basename ${ac_cv_gt4libname}` ;;

*)
	if test "${ac_cv_gt4libname}" = "" ; then
	   gt4libname=${default_gt4libname}
	else
	   gt4libname=${ac_cv_gt4libname}
	fi ;;
esac

echo "set libname \"${gt4libname}\""

AC_SUBST(gt4libname)


############################################################
#== --with-gt4suffix=ARG
#

default_gt4suffix=no
DC_ARG_WITH(gt4suffix,	[Set suffix to installdir, library name, and executable files (default empty)],
	ac_cv_gt4suffix, [
	ac_cv_gt4suffix=""
])

case $ac_cv_gt4suffix in
*'/'*)
	gt4suffix=`basename ${ac_cv_gt4suffix}` ;;

*)
	if test "${ac_cv_gt4suffix}" = "" ; then
	   gt4suffix=${default_gt4suffix}
	else
	   gt4suffix=${ac_cv_gt4suffix}
	fi ;;
esac

if test "${gt4suffix}" = "no" ; then
   gt4dstsuffix=""
   gt4libsuffix=""
   gt4binsuffix=""
else
   gt4dstsuffix=-$gt4suffix
   gt4libsuffix=-$gt4suffix
   gt4binsuffix=.$gt4suffix
fi

echo "set gt4dstsuffix \"${gt4dstsuffix}\"", \
     "set gt4libsuffix \"${gt4libsuffix}\"", \
     "set gt4binsuffix \"${gt4binsuffix}\""

AC_SUBST(gt4dstsuffix)
AC_SUBST(gt4libsuffix)
AC_SUBST(gt4binsuffix)


############################################################
#== --with-lang_conform_check
#

DC_ARG_WITH(lang_conform_check,
	[language standard conformance checking (default no)],
	ac_cv_lang_conform_check,
	[ac_cv_lang_conform_check=no]
)

case $ac_cv_lang_conform_check in
f95)
	if test X"$F90MODTYPE" = X"fqs.mod"; then
	   SYSFFLAGS="$SYSFFLAGS -v95d,95o,95s"
	   echo "set compile and link flags \"-v95d,95o,95s\""
	   
	elif test X"$F90MODTYPE" = X"std.mod" && test X"$FC" = X"g95" ; then
	   SYSFFLAGS="$SYSFFLAGS -std=f95"
	   echo "set compile and link flags \"-std=f95\""
	   
	elif test X"$F90MODTYPE" = X"std.mod" && test X"$FC" = X"ifort" ; then
	   SYSFFLAGS="$SYSFFLAGS -stand 95 -e95"
	   echo "set compile and link flags \"-stand 95 -e95\""
	   
	else
	   AC_MSG_ERROR(I do not know language standard conformance checking options of your compiler)
	fi ;;
no)	;;
*)		echo -n 'reconfigure with --with-lang_conform_check={f95,no}'
		echo ' or --without-lang_conform_check'
		AC_MSG_ERROR(unknown lang_conform_check type) ;;
esac


############################################################
#== --enable-diagmsg
#

DC_ARG_ENABLE(diagmsg,
	[Output diagnostic messages about the source program [disable]],
	ac_cv_diagmsg,
	[ac_cv_diagmsg=no]
)


case $ac_cv_diagmsg in
yes)
	if test X"$F90MODTYPE" = X"fqs.mod"; then
	    case "$SYSFFLAGS" in
		*"-E cg"*) ;;
		*)	SYSFFLAGS="$SYSFFLAGS -E cg";;
	    esac
	    echo "set compile and link flags \"-E cg\""
 
	elif test X"$F90MODTYPE" = X"std.mod" && test X"$FC" = X"g95" ; then
	    case "$SYSFFLAGS" in
		*"-Wall"*) ;;
		*)	SYSFFLAGS="$SYSFFLAGS -Wall";;
	    esac
	    echo "set compile and link flags \"-Wall\""

	elif test X"$F90MODTYPE" = X"std.mod" && test X"$FC" = X"ifort" ; then
	    case "$SYSFFLAGS" in
		*"-warn all"*) ;;
		*)	SYSFFLAGS="$SYSFFLAGS -warn all";;
	    esac
	    echo "set compile and link flags \"-warn all\""

	else
	   AC_MSG_ERROR(I do not know outputs diagnostic messages options of your compiler)
	fi ;;
no)   ;;
*)		echo 'reconfigure with --enable-diagmsg or --disable-diagmsg'
		AC_MSG_ERROR(unknown diagmsg type) ;;
esac


############################################################
#== --enable-debug
#

DC_ARG_ENABLE(debug,
	[Check Fortran source programs [disable]], ac_cv_debug, 
	[ac_cv_debug=no]
)

case $ac_cv_debug in
yes)
	if test X"$F90MODTYPE" = X"fqs.mod"; then
	    case "$SYSFFLAGS" in
		*"-E cg"*) ;;
		*)	SYSFFLAGS="$SYSFFLAGS -E cg";;
	    esac
	    SYSFFLAGS="$SYSFFLAGS -g -H aesux"
	    echo "set compile and link flags \"-E cg -g -H aesux\""
	    
	elif test X"$F90MODTYPE" = X"std.mod" && test X"$FC" = X"g95" ; then
	    case "$SYSFFLAGS" in
		*"-Wall -ftrace=frame"*) ;;
		*)	SYSFFLAGS="$SYSFFLAGS -Wall -ftrace=frame";;
	    esac
	    echo "set compile and link flags \"-Wall -ftrace=frame\""
	   
	elif test X"$F90MODTYPE" = X"std.mod" && test X"$FC" = X"ifort" ; then
	    case "$SYSFFLAGS" in
		*"-warn all -check all -traceback"*) ;;
		*)	SYSFFLAGS="$SYSFFLAGS -warn all -check all -traceback";;
	    esac
	    echo "set compile and link flags \"-warn all -check all -traceback\""
	    
	else
	   AC_MSG_ERROR(I do not know debug options of your compiler)
	fi ;;
no)   ;;
*)		echo 'reconfigure with --enable-debug or --disable-debug'
		AC_MSG_ERROR(unknown debug type) ;;
esac


############################################################
#== Care of some special Environments
# 

if test X"$F90MODTYPE" = X"intel.d"; then
	case "$SYSLDLIBS$SYSLDFLAGS$SYSFFLAGS" in
	*-Vaxlib*) ;;
	*)	SYSLDLIBS="$SYSLDLIBS -Vaxlib";;
	esac
fi

if test X"$F90MODTYPE" = X"fqs.mod"; then
	case "$SYSFFLAGS" in
	*-Am*) ;;
	*)	SYSFFLAGS="$SYSFFLAGS -Am";;
	esac
fi

AC_SUBST(SYSLDLIBS)
AC_SUBST(SYSLDFLAGS)
AC_SUBST(SYSFFLAGS)


############################################################
#== system dependent runtime selection (not checked)
#

DC_ARG_WITH(getarg, [getarg runtime procedure], ac_cv_fortran_getarg,
	ac_cv_fortran_getarg=common
)
SYSDEPARG=$ac_cv_fortran_getarg
case $SYSDEPARG in
common)		echo 'sysdepargget() calls GETARG' ;;
hitachi)	echo 'sysdepargget() calls GETARG' ;;
*)		echo 'reconfigure with --with-getarg={common,hitachi}'
		AC_MSG_ERROR(unknown getarg type) ;; 
esac
AC_SUBST(SYSDEPARG)


DC_ARG_WITH(abort, [abort runtime procedure [abort]], ac_cv_fortran_abort,
        ac_cv_fortran_abort=abort
)
SYSDEPABORT=$ac_cv_fortran_abort
case $SYSDEPABORT in
abort)		echo 'sysdepabort() calls ABORT';;
errtra-setrcd)	echo 'sysdepabort() calls ERRTRA/SETRCD/EXIT (intended for Lahey/Fujitsu Fortran)';;
exit)		echo 'sysdepabort() calls EXIT (intended for Intel Fortran)';;
setrcd)		echo 'sysdepabort() calls SETRCD/EXIT (intended for FQS Fortran)';;
stop)		echo 'sysdepabort() executes STOP';;
*)		echo 'reconfigure with --with-abort={abort,errtra-setrcd,exit,setrcd,stop}'
		AC_MSG_ERROR(unknown abort type) ;;
esac
AC_SUBST(SYSDEPABORT)

############################################################
#== Maximum number of dimensions of input and output data

DC_ARG_WITH(maxdim, [max number of dimensions of in/out data (default 7)], ac_cv_maxdim, [
	ac_cv_maxdim=7
])
if test X"${ac_cv_maxdim}" != X"" ; then
	if let "${ac_cv_maxdim} < 1" ; then 
		AC_MSG_ERROR(Specify positive value with --with-maxdim=)
	elif let "${ac_cv_maxdim} > 100" ; then
		AC_MSG_ERROR(Max number of dimensions with --with-maxdim= is too large. )
	elif let "${ac_cv_maxdim} > 50" ; then
		echo 'WARNING: Max number of dimensions with --with-maxdim= is very large. So the compilation may be impossible.'
		MAXDIM=${ac_cv_maxdim}
	else
		MAXDIM=${ac_cv_maxdim}
	fi
else
	AC_MSG_ERROR(Specify positive value with --with-maxdim=)
fi

#case $ac_cv_maxdim in
#	'*[0-9]*')
#		;;
#	[0-9][0-9])
#		echo 'WARNING: Because max number of dimensions is very large, the compilation may be impossible.'
#		;;
#	[0-9][0-9][0-9]*)
#		AC_MSG_ERROR(The value with --with-maxdim= is too large.)
#		;;
#	*)
#		echo ${ac_cv_maxdim}
#		AC_MSG_ERROR(Specify positive value with --with-maxdim=)
#		;;
#esac

AC_SUBST(MAXDIM)


############################################################
#== Some system commands

#=== make
#
export MAKE
sh ./chkgmake.sh || exit
eval `cat chkgmake.cfg`

AC_SUBST(MAKE)

#=== ar
#
AC_CHECKING(for ar utility)
case "${AR-unset}" in
    unset) AC_CHECK_PROGS(AR, ar, ar) ;;
    *) AC_CHECK_PROGS(AR, $AR ar, ar) ;;
esac
AC_MSG_CHECKING(ar flags)
case "${ARFLAGS-unset}" in
    unset) ARFLAGS=cru ;;
esac
AC_MSG_RESULT($ARFLAGS)
AC_SUBST(ARFLAGS)

case "$build" in
    powerpc-ibm-aix*)
	case "${ARFLAGS-unset}" in
	    unset) ARFLAGS="-X64" ;;
	    *-X64*) ;;
	    *) ARFLAGS="-X64 $ARFLAGS" ;;
	esac
	;;
    *)
	;;
esac

AC_SUBST(AR)

#=== ranlib
#
AC_PROG_RANLIB

#=== ncdump
#
case "${NCDUMP-unset}" in
    unset) AC_CHECK_PROGS(NCDUMP, ncdump) ;;
    *) AC_CHECK_PROGS(NCDUMP, $NCDUMP ncdump) ;;
esac
if test -z "$NCDUMP"; then
    AC_MSG_WARN("Could not find ncdump command")
fi

AC_SUBST(NCDUMP)

#
#== output
#

AC_OUTPUT(Config.mk:Config.mk.in)

# ${MAKE:-make} ${CHARSET}

echo ""
echo "  Execute GNU make in the current directory, as follows."
echo ""
echo "    $MAKE"
echo ""
