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.6 2005/05/30 18:04:24 morikawa Exp $
dnl  * Tag Name: $Name: gt4f90io-20050530 $
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

#
#== StartUp
#

AC_CHECK_CANONICAL_HOST
AC_INIT(src/gt4f90io.f90)
AC_PREFIX_DEFAULT(/usr/local/gt4f90io)

sh ./chkfort.sh
. chkfort.cfg
AC_SUBST(FC)
AC_SUBST(MOD)
AC_SUBST(F90MODTYPE)
: ${IFCCEM:=:}
AC_SUBST(IFCCEM)

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


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)

#
#== 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=)
])

if test ! -f $ac_cv_lib_netcdf ; then
	AC_MSG_ERROR(specified base directory $ac_cv_lib_netcdf not exist)
fi
netcdf_libdir=`dirname $ac_cv_lib_netcdf`
netcdf_libname=`basename $ac_cv_lib_netcdf .a | sed 's/^lib//'`
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

dnl #
dnl #== Libraries by dclf90config
dnl #
dnl 
dnl #
dnl #=== dclf90config of DCL-F90
dnl #
dnl #
dnl #DC_ARG_WITH(dclf90config, [dclf90config command], ac_cv_lib_dclf90config, [
dnl #	ac_cv_lib_dclf90config=`which dclf90config 2> /dev/null`
dnl #	if test ! -x "$ac_cv_lib_dclf90config"; then
dnl #		ac_cv_lib_dclf90config=NONE
dnl #	fi
dnl #])
dnl #
dnl #
dnl #=== without DCLF90CONFIG
dnl #
dnl #
dnl #if test X"$ac_cv_lib_dclf90config" = X"NONE"
dnl #then
dnl #	echo '         then' dclconfig, dclf90lib must be specified.
dnl #	#
dnl #	# --- DCL ---
dnl #	#
dnl #	DC_ARG_WITH(dclconfig, [dclconfig command], ac_cv_lib_dclconfig, [
dnl #		ac_cv_lib_dclconfig=`which dclconfig 2> /dev/null`
dnl #		if test ! -x "$ac_cv_lib_dclconfig"; then
dnl #			AC_MSG_ERROR(dclconfig not found)
dnl #		fi
dnl #	])
dnl #	#
dnl #	# --- dclf90 ---
dnl #	#
dnl #	DC_ARG_WITH(dclf90lib, [dcl-f90 library], ac_cv_lib_dclf90,
dnl #		AC_MSG_ERROR(specify dcl-f90 library filename (like libdcl-f90.a) with --with-dclf90lib= option.)
dnl #	)
dnl #
dnl #	if test ! -f $ac_cv_lib_dclf90 ; then
dnl #		AC_MSG_ERROR(specified library file $ac_cv_lib_dclf90 not exist)
dnl #	fi
dnl #	dclf90_libdir=`dirname $ac_cv_lib_dclf90`
dnl #	dclf90_libname=`basename $ac_cv_lib_dclf90 .a | sed 's/^lib//'`
dnl #	try1=""; try2=""
dnl #	if test -d ${try1:=$dclf90_libdir/module} ; then
dnl #		dclf90_moddir=$try1
dnl #	elif test -d ${try2:=`dirname $dclf90_libdir`/include} ; then
dnl #		dclf90_moddir=$try2
dnl #	else
dnl #		AC_MSG_ERROR(dcl-f90 module directory not found)
dnl #	fi
dnl #
dnl #	dclconfig=$ac_cv_lib_dclconfig
dnl #	SYSLDFLAGS="`$dclconfig --ldflags` -L$dclf90_libdir -L$netcdf_libdir"
dnl #	SYSLDLIBS="-l$dclf90_libname `$dclconfig --ldlibs` -l$netcdf_libname $SYSFLIBS"
dnl #	SYSFFLAGS="`$dclconfig --fflags` -I$dclf90_moddir"
dnl #else
dnl #	dclf90config="$ac_cv_lib_dclf90config"
dnl #	SYSLDFLAGS="`$dclf90config --ldflags` -L$netcdf_libdir"
dnl #	SYSLDLIBS="`$dclf90config --ldlibs` -l$netcdf_libname"
dnl #	SYSFFLAGS="`$dclf90config --fflags`"
dnl #fi 


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

#
#== --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
ac_cv_gt4suffix=${default_gt4suffix}
DC_ARG_WITH(gt4suffix,	[Set suffix to installdir, library name, and executable files [(empty)]],
	ac_cv_gt4suffix, [AC_MSG_ERROR(specify Suffix --with-gt4suffix=ARG)]
)

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}\""
echo "set gt4libsuffix \"${gt4libsuffix}\""
echo "set gt4binsuffix \"${gt4binsuffix}\""

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


#
#== --with-lang_conform_check
#

ac_cv_lang_conform_check=no
DC_ARG_WITH(lang_conform_check,
	[language standard conformance checking [no]],
	ac_cv_lang_conform_check,
	[AC_MSG_ERROR(specify language standard conformance checking with --with-lang_conform_check={f95,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


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)


#
#== output
#

AC_OUTPUT(Config.mk)

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