dnl 
dnl template file to create configure script for DCL
dnl

#
# === STARTUP ===
#

AC_CHECK_CANONICAL_HOST
AC_INIT(src/gtool.f90)
AC_PREFIX_DEFAULT(/usr/local/gtool4)

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)

#
# === LIBRARIES BY DCLF90CONFIG ===
#
# --- 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

#
# --- dclf90config of DCL-F90 ---
#

DC_ARG_WITH(dclf90config, [dclf90config command], ac_cv_lib_dclf90config, [
	ac_cv_lib_dclf90config=`which dclf90config 2> /dev/null`
	if test ! -x "$ac_cv_lib_dclf90config"; then
		ac_cv_lib_dclf90config=NONE
	fi
])

#
# --- without DCLF90CONFIG ---
#

if test X"$ac_cv_lib_dclf90config" = X"NONE"
then
	echo '         then' dclconfig, dclf90lib must be specified.
	#
	# --- DCL ---
	#
	DC_ARG_WITH(dclconfig, [dclconfig command], ac_cv_lib_dclconfig, [
		ac_cv_lib_dclconfig=`which dclconfig 2> /dev/null`
		if test ! -x "$ac_cv_lib_dclconfig"; then
			AC_MSG_ERROR(dclconfig not found)
		fi
	])
	#
	# --- dclf90 ---
	#
	DC_ARG_WITH(dclf90lib, [dcl-f90 library], ac_cv_lib_dclf90,
		AC_MSG_ERROR(specify dcl-f90 library filename (like libdcl-f90.a) with --with-dclf90lib= option.)
	)

	if test ! -f $ac_cv_lib_dclf90 ; then
		AC_MSG_ERROR(specified library file $ac_cv_lib_dclf90 not exist)
	fi
	dclf90_libdir=`dirname $ac_cv_lib_dclf90`
	dclf90_libname=`basename $ac_cv_lib_dclf90 .a | sed s/^lib//`
	try1=""; try2=""
	if test -d ${try1:=$dclf90_libdir/module} ; then
		dclf90_moddir=$try1
	elif test -d ${try2:=`dirname $dclf90_libdir`/include} ; then
		dclf90_moddir=$try2
	else
		AC_MSG_ERROR(dcl-f90 module directory not found)
	fi

	dclconfig=$ac_cv_lib_dclconfig
	SYSLDFLAGS="`$dclconfig --ldflags` -L$dclf90_libdir -L$netcdf_libdir"
	SYSLDLIBS="-l$dclf90_libname `$dclconfig --ldlibs` -l$netcdf_libname $SYSFLIBS"
	SYSFFLAGS="`$dclconfig --fflags` -I$dclf90_moddir"
else
	dclf90config="$ac_cv_lib_dclf90config"
	SYSLDFLAGS="`$dclf90config --ldflags` -L$netcdf_libdir"
	SYSLDLIBS="`$dclf90config --ldlibs` -l$netcdf_libname"
	SYSFFLAGS="`$dclf90config --fflags`"
fi 

#
# --- -with-docdir=docidir ---
#
ac_cv_dest_doc='${prefix}/doc'
DC_ARG_WITH(docdir, [document installation directory], ac_cv_dest_doc, [
        AC_MSG_ERROR(specify docdir with --with-docdir=dir)])
docdir=$ac_cv_dest_doc
AC_SUBST(docdir)

#
# --- 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=dcl
)
SYSDEPARG=$ac_cv_fortran_getarg
case $SYSDEPARG in
common)		echo 'sysdepargget() calls GETARG' ;;
dcl)		echo 'sysdepargget() calls DCL OSGARG' ;;
*)		echo 'reconfigure with --with-getarg={common,dcl}'
		AC_MSG_ERROR(unknown getarg type) ;; 
esac
AC_SUBST(SYSDEPARG)

DC_ARG_WITH(abort, [abort runtime procedure], ac_cv_fortran_abort,
        ac_cv_fortran_abort=dcl
)
SYSDEPABORT=$ac_cv_fortran_abort
case $SYSDEPABORT in
abort)		echo 'sysdepabort() calls ABORT';;
dcl)		echo 'sysdepabort() calls DCL OSABRT';;
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,dcl,errtra-setrcd,exit,setrcd,stop}'
		AC_MSG_ERROR(unknown abort type) ;;
esac
AC_SUBST(SYSDEPABORT)

#
# === output ===
#

AC_OUTPUT(Config.mk)

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