#! /bin/sh
#
# Get the available types (kind form)
# This only works if programs can be RUN.  If cross-compiling only,
# you need to use the traditional form.
#
#set -x
MAKE=${MAKE:-"make"}
basic_types="integer real complex logical"
supported_types=""
for intdigits in 2 4 8 15 ; do
    rm -f conftest*
    cat >conftest.f <<EOF
        program main
        print *, selected_int_kind($intdigits)
        end
EOF
    if ${MAKE} conftest >>conftest.log 2>&1 ; then 
        if ./conftest >conftest.out 2>&1 ; then
	    intkind=`cat conftest.out | sed -e 's/ //g'`
	    intkinds="$intkinds $intkind"
        fi
    fi
done
#
for realdigits in 6 9 15 ; do
    rm -f conftest*
    cat >conftest.f <<EOF
        program main
        print *, selected_real_kind($realdigits)
        end
EOF
    if ${MAKE} conftest >>conftest.log 2>&1 ; then 
        if ./conftest >conftest.out 2>&1 ; then
	    realkind=`cat conftest.out | sed -e 's/ //g'`
	    realkinds="$realkinds $realkind"
        fi
    fi
done
for logbytes in 1 2 4 8  ; do
    rm -f conftest*
    cat >conftest.f <<EOF
        program main
        logical*$logbytes ll
        print *, kind(ll)
        end
EOF
    if ${MAKE} conftest >>conftest.log 2>&1 ; then 
        if ./conftest >conftest.out 2>&1 ; then
	    logkind=`cat conftest.out | sed -e 's/ //g'`
	    logkinds="$logkinds $logkind"
        fi
    fi
done
rm -f kinderr.log
if [ -z "$intkinds" ] ; then
    # We had a problem building and/or running the 
    cat conftest.log > kinderr.log
    rm -f conftest*
    exit 1
fi
rm -f conftest*
#
# Now we eliminate duplicates from the lists of types
# 
lastkind="-10"
for kind in $intkinds ; do
    if [ $kind != $lastkind -a $kind -ge 0 ] ; then
	supported_types="$supported_types integer$kind"
    fi
    lastkind=$kind
done
lastkind="-10"
for kind in $realkinds ; do
    if [ $kind != $lastkind -a $kind -ge 0 ] ; then
	supported_types="$supported_types real$kind complex$kind"
    fi
    lastkind=$kind
done
lastkind="-10"
for kind in $logkinds ; do
    if [ $kind != $lastkind -a $kind -ge 0 ] ; then
	supported_types="$supported_types logical$kind"
    fi
    lastkind=$kind
done
echo $supported_types
