dnl------------------------------------------------------------------
dnl      Copyright (C) GFD Dennou Club, 2005. All rights reserved.
dnl------------------------------------------------------------------
dnl
dnl= Template file to create configure script for gt4f90io
dnl
dnl  * Developers: Eizi TOYODA, Yasuhiro Morikawa
dnl  * Version: $Id: configure.in,v 1.5 2006/01/21 06:56:37 morikawa Exp $
dnl  * Tag Name: $Name: gt4f90io-20060121 $
dnl  * Change History:
dnl
dnl== Overview
dnl
dnl This file is template file to create "configure" script by
dnl autoconf 2.13. 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.5 2006/01/21 06:56:37 morikawa Exp $)dnl

############################################################
#== StartUp
#

AC_INIT(src/gt4f90io.f90)
AC_PREFIX_DEFAULT(/usr/local/gt4f90io)
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 fortran Compiler and Module type
#
DC_ARG_WITH(fc, [Fortran compiler (override "\$FC")], ac_cv_FC, [
		AC_MSG_RESULT((no))
])

if test X"$ac_cv_FC" != X"" ; then
	FC=$ac_cv_FC
fi

sh ./chkfort.sh || exit
eval `cat chkfort.cfg`

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

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 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*)
	          LINKF='c++ $(INCLUDE_GT) $(INCLUDES) -f90lib'
	          ;;
	      *)
	          ;;
	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_SET_LIBDIR_LIBNAME($ac_cv_lib_netcdf, netcdf_libdir, netcdf_libname)

	try2=`dirname $netcdf_libdir`/include
	if test -f "$try2/netcdf.h"; then
		netcdf_incdir=$try2
	else
		AC_MSG_ERROR(netcdf include file not found)
	fi

############################################################
#== Check Cache about FFLAGS
#
DC_ARG_WITH(fflags, [flags for Fortran compiler], ac_cv_fflags, [
	ac_cv_fflags=""
])

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

############################################################
#== --with-docdir=docidir
#
ac_cv_dest_doc='${prefix}/doc'
DC_ARG_WITH(docdir,
		[document installation directory [${ac_default_prefix}/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 [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 [(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 [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"*) ;;
		*)	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 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

case "$build" in
powerpc-ibm-aix*)
	if test X"$FC" = X"xlf90" ;then
		case "$SYSFFLAGS" in
		*-qsuffix=f=f90*) ;;
		*)	SYSFFLAGS="$SYSFFLAGS -qsuffix=f=f90";;
		esac
	fi
	;;
*) ;;
esac


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' ;;
#*)		echo 'reconfigure with --with-getarg={common}'
#		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

#=== make
#
DC_ARG_WITH(maxdim, [max number of dimensions of in/out data [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
	#
	DC_ARG_WITH(make-cmd, ["make" command], ac_cv_make, [
		ac_cv_make=make
	])
	if test X"${ac_cv_make}" != X"" ; then
		if which $ac_cv_make > /dev/null 2>&1 ; then
			MAKE=$ac_cv_make
		else
			AC_MSG_ERROR(make command \"$ac_cv_make\" is not found. Specify existing make command with --with-make-cmd=)
		fi
	else
		AC_MSG_ERROR(specify make command with --with-make-cmd=)
	fi
	AC_SUBST(MAKE)


	#=== ar
	#
	DC_ARG_WITH(ar-cmd, ["ar" command], ac_cv_ar, [
		ac_cv_ar=ar
	])
	if test X"${ac_cv_ar}" != X"" ; then
		if which $ac_cv_ar > /dev/null 2>&1 ; then
			AR=$ac_cv_ar
		else
			AC_MSG_ERROR(ar command \"$ac_cv_ar\" is not found. Specify existing ar command with --with-ar-cmd=)
		fi
	else
		AC_MSG_ERROR(specify ar command with --with-ar-cmd=)
	fi

	case "$build" in
	powerpc-ibm-aix*)
		AR="$AR -X64"
		;;
	*)
		;;
	esac

	AC_SUBST(AR)

#
#== output
#

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

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