#!/bin/sh

for arg in "$@"
do
	case $arg in
	--prefix=*)
		PREFIX="`echo "$arg" | sed 's/--prefix=//'`"
		;;
	--fc=*)
		FC="`echo "$arg" | sed 's/--fc=//'`"
		;;
	esac
done

#
# --- Fortran compiler ---
#

obj=o
exe=
oflag='-o '

fc=${FC}
fctype=UNKNOWN
if [ X"$fc" != X"" ] && [ -x "$fc" -o X"`which "$fc"`" != X"" ]; then
	echo you have set FC=$fc
elif [ -x "`which f90`" ]; then
	fc=f90
elif [ -x "`which xf90`" ]; then
	fc=xf90
elif [ X"`which f90.exe`" != X"" ]; then
	fc=f90.exe
        obj=OBJ
	exe=.EXE
	oflag=/exe:
elif [ -x "`which frt`" ]; then
	fc=frt
elif [ -x "`which vf90`" ]; then
	fc=vf90
fi

echo end > test.f90
if $fc -c test.f90 > /dev/null 2>&1
then
	echo your Fortran compiler is $fc.
else
	echo you have no Fortran 90 compiler.
	exit 1
fi

# HITACHI check
sed s/HF90TEST// <<EOF > test.f90
HF90TESTbadlabel: print *, 1
HF90TEST       end
EOF
if $fc -c test.f90 > test.out 2>&1
then
	if grep KCHF test.out > /dev/null
	then
		fctype=HITACHI
		echo $fc seems to be HITACHI compiler.
		echo I will use wapper in order to stop in error.
		sed s/HF90WRAP// <<EOF > hf90wrap.sh
HF90WRAP#!/bin/sh
HF90WRAP
HF90WRAPtmp=/tmp/f90wrap\$\$
HF90WRAPcompile=0
HF90WRAP
HF90WRAPfor arg in "\$@"
HF90WRAPdo
HF90WRAP	case "\$arg" in
HF90WRAP	-c)
HF90WRAP		compile=1
HF90WRAP		;;
HF90WRAP	*)
HF90WRAP		;;
HF90WRAP	esac
HF90WRAPdone
HF90WRAP
HF90WRAPif [ X\$compile = X1 ]; then
HF90WRAP	\${FC:-$fc} \$* > \$tmp 2>&1
HF90WRAP	if awk '/KCHF/ { if (\$2 > e) e = \$2; }; END { exit e }' \$tmp
HF90WRAP	then
HF90WRAP		rm \$tmp
HF90WRAP		exit 0
HF90WRAP	else
HF90WRAP		e=\$?
HF90WRAP		echo +--- error \$e "(current directory \`pwd\`)" ---
HF90WRAP		sed 's/^/|/' \$tmp
HF90WRAP		rm \$tmp
HF90WRAP		exit \$e
HF90WRAP	fi
HF90WRAPelse
HF90WRAP	exec \${FC:-$fc} \$*
HF90WRAPfi
EOF
		chmod ugo+x hf90wrap.sh
		fc=`pwd`/hf90wrap.sh
	else
		echo $fc does not return error code if error,
		echo but it wasn\'t HITACHI. I don\'t know what to do.
		exit 1
	fi
else
	echo $fc seems to return error with compilation error, that\'s ok.
fi
rm test*

#
# --- module suffix test ---
#

sed s/:// <<EOF > testmod1.f90
:module testmod
:  integer, parameter:: foo = 1
:end module
EOF

$fc -c testmod1.f90 > testmod1.out 2>&1
if [ $? != 0 ]; then
	echo $fc does not support modules. sure\?
	exit 1
fi

sed s/:// <<EOF > testmod2.f90
:program testmod2
:  use testmod
:  print *, foo
:end program
EOF

if $fc -c testmod2.f90 > testmod2.out 2>&1
then
	echo $fc uses usual module passing mechanism.
	modfree=mod
	modfix=mod
	rm_mod='-rm -f *.mod'
else
	echo $fc uses unusual module passing mechanism.
	#
	# test HITACHI
	#
	cp testmod1.f90 testmod.f90
	if $fc -c testmod2.f90 > testmod2.out 2>&1; then
		echo HITACHI style module: module source file included
		fctype=HITACHI
		modfree=f90
		modfix=f
		rm_mod=echo
	elif rm testmod.f90 && $fc -c -Am testmod1.f90 > testmod1.out 2>&1 \
	&& $fc -c -Am testmod2.f90 > testmod2.out 2>&1
	then
		echo Fujitsu style module: .mod generated only if -Am is given
		fc="$fc -Am"
		modfree=mod
		modfix=mod
		rm_mod='-rm -f *.mod'
	else
		echo please tell us about $fc .
		exit 1
	fi
fi
rm -f testmod*

rm='-rm'

#
# --- iargc check ---
#

sed s/:// <<-EOF > testiargc.f
	:      program tiargc
	:      integer iargc
	:      external iargc
	:      print *, '      INTEGER, PARAMETER:: IARGC_NOARG=', iargc()
	:      end
EOF
if $fc -c testiargc.f && $fc ${oflag}testiargc testiargc.$obj
then
	./testiargc
	./testiargc > iargc.fh
else
	echo "$fc doesn't support IARGC()."
	exit 1
fi
rm testiargc*

#
# === output ===
#

# flags given to fc
fflags=${FFLAGS}
flflags=${FLFLAGS:-${LFLAGS}}

rm -f config.mak
sed s/:// > config.mak <<EOF
:# config.mak generated by configure.
:FC=$fc
:FFLAGS=$fflags
:FLFLAGS=$flflags
:CLEANMOD=$cleanmod
:MOD_FREE=$modfree
:MOD_FIXED=$modfix
:OBJ=$obj
:EXE=$exe
OFLAG=${oflag}:
:RM=$rm
:RM_MOD=$rm_mod
EOF

sed s/:// >> config.mak <<'EOF'
:
:.SUFFIXES: .f .f90 .$(MOD_FREE) .$(MOD_FIXED)
:
:.f.$(OBJ):
:	$(FC) -c $(FFLAGS) $<
:
:.f90.$(OBJ):
:	$(FC) -c $(FFLAGS) $<
:
EOF
