#!/bin/sh
#------------------------------------------------------------------------------
#	Program:	curf2c
#	Date:    1999/07/12
#	Description: This shell program is used for convert
#	Fortran source to C source by calling f2c.
#------------------------------------------------------------------------------

echo "---   Start   ---"

RM="/bin/rm -f "
#RM="echo "
CP=cp
MV=mv
F2C="f2c -A -P"

start_path=`pwd`
if test $# -ge 1
then
	curpath=$1
	cd $curpath
else
	curpath=`pwd`
fi
echo "Current path is: "$curpath

files=`ls`
for f in $files
do
	if test -f $f 
	then
		suffix=`echo $f | cut -d. -f2`
		other=`echo $f | cut -d. -f3`
		echo "suffix="$suffix
		if test $suffix = "f"
		then
			#if test $other != ""
			if test $other
			then 
				c_name=`echo $f | sed 's?.f.?.c.?'`
				p_name=`echo $f | sed 's?.f.?.P.?'`
				echo "f2c "$f" "$c_name"..."
				$CP $f zhuzy.f
				$F2C zhuzy.f
				$MV zhuzy.c $c_name 
				$MV zhuzy.P $p_name 
				$RM $f
			else
				echo "f2c "$f"..."
				$F2C $f
				$RM $f
			fi 
		else
			echo "This is not a Fortran file: "$f
	  	fi	
	else
		if test -d $f 
		then
			echo "This is a folder: "$f
			echo "-----------cd "$curpath"/"$f"..."
		 	tmpdir=$curpath"/"$f	
			cd $f
			files2=`ls`
			for f2 in $files2
			do
				if test -f $f2
				then
					suffix=`echo $f2 | cut -d. -f2`
					other=`echo $f2 | cut -d. -f3`
					if test $suffix = "f"
					then
						#if test $other != ""
						if test $other
						then 
							c_name=`echo $f2 | sed 's?.f.?.c.?'`
							p_name=`echo $f2 | sed 's?.f.?.P.?'`
							echo "    f2c "$f2" "$c_name"..."
							$CP $f2 zhuzy.f
							$F2C zhuzy.f
							$MV zhuzy.c $c_name 
							$MV zhuzy.P $p_name 
							$RM $f2
						else
							echo "    f2c "$f2"..."
							$F2C $f2
							$RM $f2
						fi 
					else
						echo "    This is not a Fortran file: "$f2
					fi
				else
					echo "    This is a folder: "$f2
				 	tmpdir2=$tmpdir"/"$f2	
					echo "    -----------cd "$tmpdir2
					cd $f2
					files3=`ls`
					for f3 in $files3
					do
						if test -f $f3
						then
							suffix=`echo $f3 | cut -d. -f2`
							other=`echo $f3 | cut -d. -f3`
							if test $suffix = "f"
							then
								#if test $other != ""
								if test $other
								then 
									c_name=`echo $f3 | sed 's?.f.?.c.?'`
									p_name=`echo $f3 | sed 's?.f.?.P.?'`
									echo "        f2c "$f3" "$c_name"..."
									$CP $f3 zhuzy.f
									$F2C zhuzy.f
									$MV zhuzy.c $c_name 
									$MV zhuzy.P $p_name 
									$RM $f3
									$RM $zhuzy.f
								else
									echo "        f2c "$f3"..."
									$F2C $f3
									$RM $f3
								fi 
							elif test $suffix = "g"
							then
								c_name=`echo $f3 | sed 's?.g?.c?'`
								p_name=`echo $f3 | sed 's?.g?.P?'`
								$CP $f3 zhuzy.f
								$F2C zhuzy.f
								$MV zhuzy.c $f3
								$MV zhuzy.P $p_name
								$RM zhuzy.f
							else
								echo "        This is not a Fortran file: "$f3
							fi
						else
							echo "        This is a too deep folder: "$f3
						fi
					done
					chmod g+w,o+w *
					echo "    -----------cd .."
					cd ..
				fi
			done
			chmod g+w,o+w *
			echo "-----------cd .."
			cd ..
		fi
	fi
done
chmod g+w,o+w *
cd $start_path
echo "--- Finish ---"
