#! /bin/sh
### ##########################################################################
### PUNC = < Portable Understructure for Numerical Computing >
### Copyright (C) 1994-- Michael Holst
###
### This program 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 of the License, 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.
###
### You should have received a copy of the GNU General Public License along
### with this program; if not, write to the Free Software Foundation, Inc.,
### 675 Mass Ave, Cambridge, MA 02139, USA.
###
### rcsid="$Id: buildCsrc,v 1.6 2010/08/12 05:52:15 fetk Exp $"
### ##########################################################################

## ###########################################################################
## File:    buildCsrc
##
## Purpose: Generate C sources from F77 sources.
##
## Notes:   This is two-step:
##          (1) Use "f2c -P source.f" to generate source.c and prototype.P
##          (2) Replace "#include <f2c.h>" with "#include <vf2c.h>"
##
## Author:  Michael Holst
## ###########################################################################

LIBNAME="arpack"

## ###########################################################################
## Should not have to edit below this line...
## ###########################################################################

SH="sh"
SED="sed"
AWK="awk"
LS="/bin/ls -1"
MV="mv -f"
RM="rm -f"
CAT="cat"
CP="cp"
F2C="f2c"

${RM} v${LIBNAME}.h

SRC_DIRS="src_f77 util_f77"
for src_dir in ${SRC_DIRS}; do

    cd ${src_dir}
    SOURCES=`${LS} *.f | ${SED} -e "s/\.f//g"`;
    cd ../

    for file in ${SOURCES}; do

    	echo "${F2C} -P ./${src_dir}/${file}.f"
    	${F2C} -P ./${src_dir}/${file}.f

        echo "${CAT} ${file}.c | ${SED} -e 's/\#include \"f2c.h\"/\#include <punc\/vf2c.h>/g' > ${file}.new;"
        ${CAT} ${file}.c | ${SED} -e 's/\#include \"f2c.h\"/\#include <punc\/vf2c.h>/g' > ${file}.new;

        echo "${MV} ${file}.new ${file}.c"
        ${MV} ${file}.new ${file}.c

        echo "${CAT} ${file}.P >> v${LIBNAME}.h"
        ${CAT} ${file}.P >> v${LIBNAME}.h

        echo "${RM} ${file}.P"
        ${RM} ${file}.P

    done
done

