#!/bin/sh
# fortc - make C source file FORTRAN compatible
# usage: fortc [-L LibDir] [-O OpSys] file
#
# $Id: fortc.src,v 1.1 2000/08/07 23:15:03 emmerson Exp $


#set -x


# Temporary files:
#
tmp1=/tmp/fortc$$_1
tmp2=/tmp/fortc$$_2


# Automatic cleanup on interruption:
#
trap "exit 1" 1 2 3 13 15
trap 'status=$?; rm -f $tmp1 $tmp2; exit $status' 1 2 3 13 15


# Find the default, fortc(1) runtime library directory.
#
libpath=${FORTC_LIBPATH-/usr/lib/fortc}
saveifs="$IFS"; IFS="${IFS}:"
for dir in $libpath; do
test -z "$dir" && dir=.
if test -f $dir/pre1.sed; then
  LibDir=$dir
  break
fi
done
IFS="$saveifs"


OS=${OS-linux}


for arg do
    case $1 in
    -L)	shift; LibDir=$1; shift;;
    -L*)
	LibDir=`echo $1 | sed 's/-.//'`; shift;;
    -O)	shift; OS=$1; shift;;
    -O*)
	OS=`echo $1 | sed 's/-.//'`; shift;;
    -*)	echo 1>&2 "Option '$1' is unknown"; exit 1;;
    *)	break;;
    esac
done


case $# in
    1)	file=$1;;
    *)	echo 1>&2 "$0: Usage [-L LibDir] [-O OpSys] file"; exit 1;;
esac


if test ! -f $LibDir/pre1.sed; then
    echo 1>&2 "$0: Can't find runtime support files:
    use \`-L' option or FORTC_LIBPATH environment variable"
    exit 1
fi


case "$OS" in
    next-absoft*)
	OS=next-absoft;;
    next*)
	OS=next-absoft;;
    unicos*)
	echo '#include "/usr//include/fortran.h"';
	OS=unicos;;
    vms*)
	echo "#include descrip";
	OS=vms;;
    *)	OS=`echo $OS | sed 's/_.*//'`;;
esac

case "$OS" in
    domainos|ultrix)	sedcmd=d;;
    *)		sedcmd='s//\1 "'$file'"/';;
esac

if [ ! -f $LibDir/$OS.m4 ]; then
    os=`echo $OS | sed 's/[0-9]*$//'`	# try removing version numbers
    if [ -f $LibDir/$os.m4 ]; then
	OS=$os
    else
	echo 1>&2 \
	    "$0: Couldn't find macro file for operating system: $LibDir/$OS.m4"
	exit 1
    fi
fi

if sed -f $LibDir/pre1.sed $file > $tmp1  &&
	sed -f $LibDir/pre2.sed $tmp1 > $tmp2  &&
	m4 $LibDir/$OS.m4 $LibDir/common.m4 $tmp2 > $tmp1  &&
	sed -f $LibDir/post.sed $tmp1 > $tmp2 &&
	sed '/.*\(#line.*\)M4_FORTC_FILE/'"$sedcmd" $tmp2; then
    status=0
fi


# Cleanup:
#
rm -f $tmp1 $tmp2

exit ${status-1}
