#! /bin/sh
#
# Change to the naming convention
# To make life easier on systems with short variable names, we make the
# file names use just the first two characters of the type, e.g., 
# re (real), lo (logical), in (integer), co (complex)
#
choicetype="traditional"
F90SUFFIX=f90
for arg in "$@" ; do
    case $arg in
    -kind) choicetype="kind"
    ;;
    -traditional) choicetype="traditional"
    ;;
    -types=*) types=`echo A$arg | sed -e 's/A-types=//g'`
    ;;
    -f90suffix=*) F90SUFFIX=`echo A$arg | sed -e 's/A-f90suffix=*//g'` 
    ;;
    -echo)
    set -x
    ;;
    -help|-usage)
    cat <<EOF
CreateChoiceSrc [ -kind ] [ -traditional ] [ -types=typenames ]

Generate the Fortran 90 source code for the MPI modules of routines that
involve "choice" arguments.  Also generate the master mpi.f90 file that 
contains these modules.

If -kind is given, the source code will use the KIND parameter to select
data types (e.g., INTEGER (KIND=2)).

If -traditional is given, the source code will use the traditional, but
nonstandard, <type>*size (e.g., INTEGER*2).  -traditional is the default

If -types=string is given, then the string contains the typenames and kinds 
(if -kind) or sizes (if -traditional).  For example, to select integer*2 and
integer*8, use -types="integer2 integer8".  If -types is not given, values
appropriate for the f90 compiler will be determined automatically.
EOF
    ;;
    *) echo "Unrecognized arg $arg" ;;
    esac
done
#
if [ -z "$srcdir" ] ; then 
    srcdir=`echo $0 | sed 's%/[^/]*$%%g'`
    if [ ! -x $srcdir/FindBasicTypes ] ; then
        srcdir="."
    fi
fi
if [ -z "$types" ] ; then
    # Ensure that the MAKE value is available 
    export MAKE
    if [ $choicetype = "traditional" ] ; then
        types="`$srcdir/FindBasicTypes` character"
    else
        types="`$srcdir/FindKindTypes` character"
    fi
fi
rm -f mpi.add
if [ -z "$types" ] ; then
    echo "Could not determine types for type $choicetype!"
    if [ -s kinderr.log ] ; then
        echo "Log of make attempts to build test programs follows"
        cat kinderr.log
    fi
    exit 1
fi
echo "Types are $types"
for type in $types ; do
    if [ $choicetype = "traditional" ] ; then
      f90typestring=`echo $type | sed -e 's/\([a-z]*\)\([0-9][0-9]*\)/\1\*\2/'`
    else
      f90typestring=`echo $type | \
         sed -e 's/\([a-z]*\)\([0-9][0-9]*\)/\1 (KIND=\2)/'`
    fi
    if [ "$f90typestring" = "character" ] ; then
	# Special case for character - always use len=* form
	f90typestring="character (len=*)"
    fi
    eval f90typesize=\$"${type}size"
    if [ -z "$f90typesize" ] ; then
        case $type in 
	logical1) f90typesize=1 ;;
	logical2) f90typesize=2 ;;
	logical4) f90typesize=4 ;;
	logical8) f90typesize=8 ;;
        integer1) f90typesize=2 ;;
        integer2) f90typesize=2 ;;
	integer4) f90typesize=4 ;;
	integer8) f90typesize=8 ;;
	real4)    f90typesize=4 ;;
	real8)    f90typesize=8 ;;
	real16)   f90typesize=16 ;;
	complex8) f90typesize=8 ;;
	complex16) f90typesize=16 ;;
	*1)       f90typesize=1 ;;
	*2)       f90typesize=2 ;;
	*4)       f90typesize=4 ;;
	*8)       f90typesize=8 ;;
	*16)      f90typesize=16 ;;
	*32)      f90typesize=32 ;;
	# Arbitray but common defaults
	integer)   f90typesize=4 ;;
	logical)   f90typesize=4 ;;
	real)      f90typesize=4 ;;
        *) f90typesize=-1 ;;
        esac
    fi
    #
    # Convert type name to ccnnn
    typename=`echo A$type | sed 's/^A\(..\)[^0-9]*\([0-9]*\)/\1\2/'`
    #
    rm -f mpi1__${typename}_v.${F90SUFFIX} mpi1__${typename}_s.${F90SUFFIX} mpi1__${typename}_sv.${F90SUFFIX} \
        mpi1__${typename}_vs.${F90SUFFIX}
    rm -f mpi2__${typename}_v.${F90SUFFIX} mpi2__${typename}_s.${F90SUFFIX} mpi2__${typename}_sv.${F90SUFFIX} \
        mpi2__${typename}_vs.${F90SUFFIX}
    TYPEUC=`echo $type | tr '[a-z]' '[A-Z]'`
    # Create the files
    sed -e "s/MODULE MPI1__<type>_v/MODULE MPI1__${TYPEUC}_V/g" \
        -e "s/<type>/$f90typestring/g" ${srcdir}/mpi1__type_v.f90 > mpi1__${typename}_v.${F90SUFFIX}
    sed -e "s/MODULE MPI1__<type>_s/MODULE MPI1__${TYPEUC}_S/g" \
        -e "s/<type>/$f90typestring/g" ${srcdir}/mpi1__type_s.f90 > mpi1__${typename}_s.${F90SUFFIX}
    sed -e "s/MODULE MPI1__<type>_vs/MODULE MPI1__${TYPEUC}_VS/g" \
        -e "s/<type>/$f90typestring/g" ${srcdir}/mpi1__type_vs.f90 > mpi1__${typename}_vs.${F90SUFFIX}
    sed -e "s/MODULE MPI1__<type>_sv/MODULE MPI1__${TYPEUC}_SV/g" \
        -e "s/<type>/$f90typestring/g" ${srcdir}/mpi1__type_sv.f90 > mpi1__${typename}_sv.${F90SUFFIX}
    sed -e "s/MODULE MPI1__<type>_v2/MODULE MPI1__${TYPEUC}_V2/g" \
        -e "s/<type>/$f90typestring/g" ${srcdir}/mpi1__type_v2.f90 > mpi1__${typename}_v2.${F90SUFFIX}
    sed -e "s/MODULE MPI2__<type>_v/MODULE MPI2__${TYPEUC}_V/g" \
        -e "s/<typesize>/$f90typesize/g" \
        -e "s/<type>/$f90typestring/g" \
	${srcdir}/mpi2__type_v.f90 > mpi2__${typename}_v.${F90SUFFIX}
    sed -e "s/MODULE MPI2__<type>_s/MODULE MPI2__${TYPEUC}_S/g" \
        -e "s/<typesize>/$f90typesize/g" \
        -e "s/<type>/$f90typestring/g" \
	${srcdir}/mpi2__type_s.f90 > mpi2__${typename}_s.${F90SUFFIX}
# Until we change the exclude file, these two modules are empty.  Some
# compilers will complain about the empty modules
#    sed -e "s/MODULE MPI2__<type>_vs/MODULE MPI2__${TYPEUC}_VS/g" \
#        -e "s/<type>/$f90typestring/g" ${srcdir}/mpi2__type_vs.f90 > mpi2__${typename}_vs.${F90SUFFIX}
#    sed -e "s/MODULE MPI2__<type>_sv/MODULE MPI2__${TYPEUC}_SV/g" \
#        -e "s/<type>/$f90typestring/g" ${srcdir}/mpi2__type_sv.f90 > mpi2__${typename}_sv.${F90SUFFIX}
    sed -e "s/MODULE MPI2__<type>_v2/MODULE MPI2__${TYPEUC}_V2/g" \
        -e "s/<typesize>/$f90typesize/g" \
        -e "s/<type>/$f90typestring/g" \
	${srcdir}/mpi2__type_v2.f90 > mpi2__${typename}_v2.${F90SUFFIX}
    # Add them to the mpi.f90 file
    echo "        USE MPI1__${TYPEUC}_V"  >>mpi.add
    echo "        USE MPI1__${TYPEUC}_VS" >>mpi.add
    echo "        USE MPI1__${TYPEUC}_SV" >>mpi.add
    echo "        USE MPI1__${TYPEUC}_S"  >>mpi.add
    echo "        USE MPI1__${TYPEUC}_V2" >>mpi.add

    echo "        USE MPI2__${TYPEUC}_V"  >>mpi.add
#    echo "        USE MPI2__${TYPEUC}_VS" >>mpi.add
#    echo "        USE MPI2__${TYPEUC}_SV" >>mpi.add
    echo "        USE MPI2__${TYPEUC}_S"  >>mpi.add
    echo "        USE MPI2__${TYPEUC}_V2" >>mpi.add
done
# 
# Finally, create mpi.f90
rm -f mpi.${F90SUFFIX}
cat >>mpi.${F90SUFFIX} <<EOF
        MODULE MPI
!       This module was created by the script CreateChoiceSrc
        USE MPI_CONSTANTS,                                               &
     &      BASE_MPI_WTIME => MPI_WTIME, BASE_MPI_WTICK => MPI_WTICK
!
!       Include the interface modules for the choice arguments
EOF
if [ -s mpi.add ] ; then 
    cat mpi.add >> mpi.${F90SUFFIX}
fi
cat >>mpi.${F90SUFFIX} <<EOF
!
!       Include the interface modules for the nonchoice arguments
        USE MPI1
        USE MPI2

        END MODULE MPI
EOF

# This module can be used for the non-choice (basic module) support
rm -f mpi-nochoice.${F90SUFFIX}
cat >>mpi-nochoice.${F90SUFFIX} <<EOF
        MODULE MPI
!       This module was created by the script CreateChoiceSrc
        USE MPI_CONSTANTS,                                               &
     &      BASE_MPI_WTIME => MPI_WTIME, BASE_MPI_WTICK => MPI_WTICK
!
!       Include the interface modules for the choice arguments
!
!       Include the interface modules for the nonchoice arguments
        USE MPI1
        USE MPI2

        END MODULE MPI
EOF
