#! /bin/sh
#
#  Name:
#
#     bf2f   script to create .f files from .F files
#     
#  Usage:
#
#     bf2f  [-h|-help] | 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 and
#     removing any C preprocessing statements that start with #.
#
#  Options:
#
#     h		- help. Print usage.
#
#  Example:
#
#     The following command will create yprime.f and yprimeg.f with
#     the pointer statements replaced.
#
#	bf2f yprime.F yprimeg.F
#
#  Copyright (c) 1994 by The MathWorks, Inc.
#  $Revision: 1.4 $  $Date: 1994/04/01 13:33:21 $
#__________________________________________________________________________
#23456789012345678901234567890123456789012345678901234567890123456789012
#
    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
#==============================================================================
#
# 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=""
		;;
	    *)
        	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 [ "$stat" = "" ]; then
	if [ "$msg" != "" ]; then
#------------------------------------------------------------------------------
    echo " "
    echo "    Error: $msg"
    echo " "
#------------------------------------------------------------------------------
	fi
#------------------------------------------------------------------------------
    echo "----------------------------------------------------------------"
    echo " "
    echo "    Usage: bf2f  [-h|-help] | file.F . . ."
    echo " "
    echo "    -h|-help             - display arguments"
    echo "    file.F . . .         - .F Fortran files to convert"
    echo " "
    echo "    NOTE: .f files are created as new files"
    echo "----------------------------------------------------------------"
#------------------------------------------------------------------------------
	exit 1
    fi
#
#------------------------------------------------------------------------------
    echo " "
#------------------------------------------------------------------------------
    for file in $filelist
    do
        base=`expr $file : '\(.*\)\.[^.]*'`
	case "$Arch" in
	    alpha|cray)
    		cat $base.F | awk '
#----------------------------------------------------------------
    BEGIN {state = 0}
    $1 == "pointer" {
	if (state == 0) {
	    print "c-----------------------------------------------------------------------"
	    print "c (pointer) Replace integer*8 by integer*4 on non-alpha, non-cray"
            print "c" 
            i = index($0,"pointer")
	    print substr($0,1,i-1) "integer*8" substr($0,i+7)
	    state = 1
	}
	else {
            i = index($0,"pointer")
	    print substr($0,1,i-1) "integer*8" substr($0,i+7)
	}
	next}
    $1 == "POINTER" {
	if (state == 0) {
	    print "C-----------------------------------------------------------------------"
	    print "C (POINTER) REPLACE INTEGER*8 BY INTEGER*4 ON NON-ALPHA, NON-CRAY"
            print "C" 
            i = index($0,"POINTER")
	    print substr($0,1,i-1) "INTEGER*8" substr($0,i+7)
	    state = 2
	}
	else {
            i = index($0,"POINTER")
	    print substr($0,1,i-1) "INTEGER*8" substr($0,i+7)
	}
	next}
        {if (state == 1)
	      print "c-----------------------------------------------------------------------"
	  else if (state == 2)
	      print "C-----------------------------------------------------------------------"
	 state = 0
	 if (substr($1,1,1) == "#") next
         print}' > $base.f
		;;
	    *)
    		cat $base.F | awk '
#----------------------------------------------------------------
    $1 == "pointer" {
	if (state == 0) {
	    print "c-----------------------------------------------------------------------"
	    print "c (pointer) Replace integer*4 by integer*8 on alpha and cray"
            print "c" 
            i = index($0,"pointer")
	    print substr($0,1,i-1) "integer*4" substr($0,i+7)
	    state = 1
	}
	else {
            i = index($0,"pointer")
	    print substr($0,1,i-1) "integer*4" substr($0,i+7)
	}
	next}
    $1 == "POINTER" {
	if (state == 0) {
	    print "C-----------------------------------------------------------------------"
	    print "C (POINTER) REPLACE INTEGER*4 BY INTEGER*8 ON ALPHA AND CRAY"
            print "C" 
            i = index($0,"POINTER")
	    print substr($0,1,i-1) "INTEGER*4" substr($0,i+7)
	    state = 2
	}
	else {
            i = index($0,"POINTER")
	    print substr($0,1,i-1) "INTEGER*4" substr($0,i+7)
	}
	next}
        {if (state == 1)
	      print "c-----------------------------------------------------------------------"
	  else if (state == 2)
	      print "C-----------------------------------------------------------------------"
	 state = 0
	 if (substr($1,1,1) == "#") next
         print}' > $base.f
#----------------------------------------------------------------
		;;
	esac
#------------------------------------------------------------------------------
    echo "    Created $base.f"
#------------------------------------------------------------------------------
    done
#------------------------------------------------------------------------------
    echo " "
#------------------------------------------------------------------------------
