#! /bin/sh
#set -x
#
#  Name:
#
#     F2f   preprocess Fortran file with C includes
#     
#  Usage:
#
#     F2f  [-h|-help] | [-r matlab_root] file.F . . .
#
#  Description:
#
#     This Bourne Shell script takes .F files with 'pointer' statements
#     and creates new .f files with the 'pointer' statements replaced by
#     the Fortran type statements appropriate for the platforms that
#     can't use .F files directly.
#
#     The platforms affected are:
#
#         ibm_rs (RS/6000), c2mp (convex), cray
#
#     For other platforms a diagnostic message is printed and no
#     .f files are created.
#
#     IMPORTANT: The path to the MATLAB root directory MUST be defined for
#		 this script to work properly.
#
#  Options:
#
#     h		- help. Print usage.
#
#     r		- specify the path of the MATLAB root directory
#
#  Example:
#
#     The following command will create yprime.f and yprimeg.f with
#     the pointer statements replaced.
#
#	F2f -r /usr/local/matlab yprime.F yprimeg.F
#
#  File:
#
#     $MATLAB/extern/include/fintrf.h	Header file with macros for
#					pointer statement.
#     $MATLAB/extern/src/F2f		The location of this script.
#
#  Copyright (c) 1993-94 by The MathWorks, Inc.
#  $Revision: 1.13 $  $Date: 1994/03/15 21:04:07 $
#__________________________________________________________________________
#
# Define path to MATLAB root directory here.
#
# IMPORTANT: No spaces around the '=' in the variable assignment!
#
#**************************************************************************
    MATLAB=
#**************************************************************************
#
    trap "exit" 0 1 2 3 15
#
# Determine the Arch
#
#==============================================================================
#
    Arch="unknown"
#
    if [ -f /bin/uname ]; then
   	case "`/bin/uname`" in
	    SunOS)					# sun4 and sol2
	        if [ -d /dev/pts ]; then
		    Arch="sol2"
	        else
		    Arch="sun4"
	        fi
	        ;;
	    HP-UX)					# hp300 and hp700
    	        if [ -f /bin/hp9000s300 ]; then
		    (/bin/hp9000s300) > /dev/null 2>&1
		    if [ $? -eq 0 ]; then
	    	        Arch="hp300"
	            fi
		fi
	        if [ -f /bin/hp9000s700 ]; then
		    (/bin/hp9000s700) > /dev/null 2>&1
		    if [ $? -eq 0 ]; then
	    	        Arch="hp700"
		    fi
	        fi
	        ;;
	    IRIX)					# sgi
    	        if [ -f /bin/4d ]; then
		    (/bin/4d) > /dev/null 2>&1
		    if [ $? -eq 0 ]; then
	    	        Arch="sgi"
		    fi
	        fi
	        ;;
	    ULTRIX)					# dec_risc
    	        if [ -f /ultrixboot ]; then
		    Arch=`file /ultrixboot`
		    if [ `expr "$Arch" : ".*mipsel.*"` -gt 0 ]; then
	    	        Arch="dec_risc"
		    else
	    	        Arch="vax_unix"
		    fi
	        fi
	        ;;
	    OSF1)					# alpha
	    	Arch="alpha"
	        ;;
	    AIX)					# ibm_rs
	        Arch="ibm_rs"
	        ;;
	    DomainOS)					# System V apollo
                if [ -d /usr/apollo/bin ]; then
	            Arch="apollo"
	        fi
	        ;;
	    *)
		if [ "`/bin/uname -a | grep CRAY`" != "" ]; then
		    Arch="cray"
		fi
	        ;;
        esac
    elif [ -f /bin/arch ]; then				# early sun4
	Arch="`/bin/arch`"
    elif [ -d /usr/apollo/bin ]; then			# BSD apollo
	Arch="apollo"
    elif [ -f /ultrixboot ]; then			# early ultrix
	typefile=`file /ultrixboot`
	if [ `expr "$typefile" : ".*mipsel.*"` -gt 0 ]; then
	    Arch="dec_risc"
	else
	    Arch="vax_unix"
	fi
    elif [ -d $MATLAB/bin/c2mp ]; then
	Arch="c2mp"
    fi
#==============================================================================
#
    if [ "$Arch" != "ibm_rs" -a "$Arch" != "c2mp" -a "$Arch" != "cray" ]; then
	noneed=1
    fi
#
# Verify input
#
    stat="OK"
    msg=""
    filelist=
    if [ $# -eq 0 ]; then
	stat=""
    fi
    while [ "$stat" = "OK" -a $# -gt 0 ]; do
	case "$1" in
	    -h|-help)		# -help: Help option.
		stat=""
		;;
	    -r)
		if [ $# -eq 1 ]; then
		    msg="MATLAB root directory not specified"
		    stat=""
		else
		    shift
		    (cd $1) > /dev/null 2>&1
		    if [ $? -ne 0 ]; then 
		        msg="MATLAB root directory = $1 does not exist"
		        stat=""
		    fi
		    MATLAB=$1
		fi
		;;
	    *)
        	ext=`expr $1 : '.*\.\([^.]*\)'`
		if [ "$ext" != "F" ]; then
		    msg="File to convert must end with .F extension."
		    stat=""
		elif [ ! -f $1 ]; then
		    msg="File $1 does not exist."
		    stat=""
		else
		    filelist="$filelist $1"
		fi
		;;
	esac
	shift
    done
#
    if [ "$MATLAB" = "" ]; then
	stat=""
    fi
    if [ "$stat" = "" ]; then
	if [ "$noneed" = "1" ]; then
#------------------------------------------------------------------------------
    echo "----------------------------------------------------------------"
    echo " "
    echo "    Arch = $Arch:  Use .F files directly; no .f files created."
    echo " "  
    echo "    NOTE: Need to convert only on: ibm_rs (RS/6000)"
    echo "                                   c2mp   (convex)"
    echo "                                   cray"
    echo " "  
    echo "          The usage for those cases is:"
#------------------------------------------------------------------------------
	elif [ "$msg" != "" ]; then
#------------------------------------------------------------------------------
    echo "----------------------------------------------------------------"
    echo " "
    echo "    Error: $msg"
#------------------------------------------------------------------------------
        elif [ "$MATLAB" = "" ]; then
#------------------------------------------------------------------------------
    echo "----------------------------------------------------------------"
    echo " "
    echo "    Error: Please set the MATLAB variable to the MATLAB root"
    echo "           directory path. You may do this in one of two ways:"
    echo " "    
    echo "           1. Edit this script and hardcode the path."
    echo "           2. Use the -r option to this script."
#------------------------------------------------------------------------------
	fi
#------------------------------------------------------------------------------
    echo " "
    echo "----------------------------------------------------------------"
    echo " "
    echo "    Usage: F2f  [-h|-help] | [-r matlab_root] file.F . . ."
    echo " "
    echo "    -h|-help             - display arguments"
    echo "    -r matlab_root       - specify the matlab root directory path"
    echo "    file.F . . .         - .F Fortran files to preprocess"
    echo " "
    echo "----------------------------------------------------------------"
#------------------------------------------------------------------------------
	exit 1
    fi
#
#------------------------------------------------------------------------------
    echo " "
    echo "    MATLAB = $MATLAB"
    echo " "
#------------------------------------------------------------------------------
#
    for file in $filelist
    do
        base=`expr $file : '\(.*\)\.[^.]*'`
	case "$Arch" in
	    ibm_rs)
		cc -P -I$MATLAB/extern/include $base.F
		mv $base.i $base.f
		;;
	    c2mp)
		fc -E -I$MATLAB/extern/include $base.F > $base.f
		;;
	    cray)
		/lib/cpp -C -P -I$MATLAB/extern/include $base.F $base.f
		;;
	esac
#------------------------------------------------------------------------------
    echo "    Created $base.f"
#------------------------------------------------------------------------------
    done
#------------------------------------------------------------------------------
    echo " "
#------------------------------------------------------------------------------
