#! /bin/sh

## f77 -- Simple shell script wrapper to compile/link FORTRAN 77 code
## using the FORTRAN-to-C converter.
##
## Usage:
##   R CMD f77 [options] files [objs]

## Copyright (C) 2002 The R Core Development Team
##
## This document is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## A copy of the GNU General Public License is available via WWW at
## http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
## writing to the Free Software Foundation, Inc., 59 Temple Place,
## Suite 330, Boston, MA  02111-1307, U.S.A.

revision='$Revision: 1.1 $'
version=`set - ${revision}; echo ${2}`
version="R front-end script to f2c ${version}

Copyright (C) 2002 The R Core Development Team.
This is free software; see the GNU General Public Licence version 2
or later for copying conditions.  There is NO warranty."

usage="Usage: R CMD f77 [options] files [objs]

The specified files should be FORTRAN 77 source files ending in '.f'.

Options:
  -h, --help            print short help message and exit
      --version         print version info and exit
  -c                    compile and assemble, but do not link
  -o FILE               place the output into FILE
  -v, --verbose         display the programs invoked

Report bugs to <r-bugs@r-project.org>."

## Defaults from configure
: ${F77='@F77@'}
: ${F2C='@F2C@'}
: ${CC='@CC@'}
: ${CFLAGS='@CFLAGS@'}
FLIBS='@FLIBS@'

## For sake of completeness: if we have a real FORTRAN 77 compiler then
## use it.
if test -z "${F2C}"; then
  exec ${F77} "${@}"
fi

## 
rc=0
tmp_stderr_file=${TMPDIR-/tmp}/f77_stderr.${$}
trap "rm -f ${tmp_stderr_file}; exit \$rc" 0

opt_c=no
opt_o=no
echo=echo
outfile=a.out
verbose=false
srcs=
objs=

## Argument loop.
while test -n "${1}"; do
  case "${1}" in
    -h|--help)
      ${echo} "${usage}"; exit 0 ;;
    --version)
      ${echo} "${version}"; exit 0 ;;
    -v|--verbose)
      verbose=${echo} ;;
    -c)
      opt_c=yes ;;
    -o)
      opt_o=yes
      outfile="${2}"
      shift
      ;;
    *.f)
      srcs="${srcs} ${1}" ;;
    */*|*.a|*.o|*.s[lo]|*.s[lo].*)
      objs="${objs} ${1}" ;;
  esac
  shift
done

set - "${srcs}"
if test ${#} -gt 1 \
     && test "${opt_c}" = yes \
     && test "${opt_o}" = yes; then
  echo "cannot specify -o with -c and multiple compilations"
  exit
fi

for f in ${*}; do
  b=`basename ${f} .f`
  ${verbose} ${F2C} ${F2CFLAGS} ${f}
  ${F2C} ${F2CFLAGS} ${f}
  rc=${?}
  test ${rc} = 0 || exit
  if test "${opt_c}" = yes && test "${opt_o}" = yes; then
    CFLAGS="${CFLAGS} -o ${outfile}"
  fi
  ${verbose} ${CC} -c ${CFLAGS} ${b}.c 2>${tmp_stderr_file}
  ${CC} -c ${CFLAGS} ${b}.c 2>${tmp_stderr_file}
  rc=${?}
  test ${rc} = 0 || exit
  objs="${objs} ${b}.o"
  rm -f ${b}.c
done

if test "${opt_c}" = no && test -n "${objs}"; then
  ${verbose} "${CC} ${CFLAGS} -o ${outfile} -u MAIN__ ${objs} ${FLIBS}"
  ${CC} ${CFLAGS} -o ${outfile} -u MAIN__ ${objs} ${FLIBS}
fi
rc=${?}
exit ${rc}

### Local Variables: ***
### mode: sh ***
### sh-indentation: 2 ***
### End: ***