#! /bin/sh
#
# Get the available types
#
#set -x
basic_types="integer real complex logical"
supported_types=""
dbltype=""
for type in $basic_types ; do
    # Check for what size is available
    for bytes in 1 2 4 8 16 ; do 
        rm -f conftest*
        cat <<EOF > conftest.f
      program main
      $type*$bytes a
      end
EOF
        # We'd like to detect the case where a type is not really 
        # supported.  For example, the Solaris f90 maps ALL integer*k
        # types to integer*4.
        if make conftest >>conftest.out 2>&1 ; then
	    #cat conftest.out
#  	    supported_types="$supported_types $type$bytes"
	    eval curlen=\$"${type}_lens"
	    eval ${type}_lens='"'$curlen $bytes'"'
        fi
    done
done
rm -f conftest*
#
# At this point, we have a candidate list.  Unfortunately, some Fortran90
# compiles, for example f90 on Solaris, accept a wide range of <type>*len 
# arguments, but map them to a much smaller range of types.  This might be 
# ok, but then then cause errors when there are generic modules with, for 
# example, specific instances for integer*1 and integer*2.  
for type in $basic_types ; do
    # Check for what size is available
    eval type_len="\$${type}_lens"
    supported_types="$supported_types $type"
    for bytes in $type_len ; do
        rm -f conftest*
        cat <<EOF > conftest.f
      module test
      interface foo
          subroutine foo_1(arg)
	  $type arg
          end subroutine
          subroutine foo_2(arg)
	  $type*$bytes arg
	  end subroutine
      end interface
      end module test
EOF
        if make conftest.o >>conftest.out 2>&1 ; then
	    if [ $type = "real" ] ; then
	        # is this type the same as double precision?
	        rm -f conftest*
	        cat <<EOF > conftest.f
      module test
      interface foo
          subroutine foo_1(arg)
	  double precision arg
          end subroutine
          subroutine foo_2(arg)
	  $type*$bytes arg
	  end subroutine
      end interface
      end module test
EOF
                if make conftest.o >>conftest.out 2>&1 ; then
		    # No
   	            supported_types="$supported_types $type$bytes"
		else 
		    # Yes
		    if [ -z "$dbltype" ] ; then dbltype="real$bytes" ; fi
		fi
	    else
  	        supported_types="$supported_types $type$bytes"
	    fi
	#else 
	#    cat conftest.out
        fi
    done
    if [ $type = "real" ] ; then
	supported_types="$supported_types $dbltype"
    fi
done
echo $supported_types
