#!/bin/csh

if( "$1" == "-h" || $#argv != 1 ) then
	if( "$1" != "-h" && $#argv > 1 ) then
		echo "rfcon: too many arguments" >> /dev/stderr
	endif
	echo    "usage: rfcon [-h] NAME" >> /dev/stderr
	echo    "files required: NAME.f, r.NAME, NAME.dat" >> /dev/stderr
	echo -n "to start from the converted data use" >> /dev/stderr
	echo    " restart_run=dat and -IRS 1" >> /dev/stderr
	exit 1
endif

if( !( (-ef $1.f) && (-ef r.$1) && (-ef $1.dat) ) ) then
	test -f $1f    || echo "rfcon: file $1.f not found"   >> /dev/stderr
	test -f r.$1   || echo "rfcon: file r.$1 not found"   >> /dev/stderr
	test -f $1.dat || echo "rfcon: file $1.dat not found" >> /dev/stderr
	exit 1
endif

which f77 >& /dev/null
if ( $status ) then
	echo -n "rfcon: Fortran compiler f77 not found," >> /dev/stderr
	echo    " create an alias or link with" >> /dev/stderr
	echo -n "rfcon: name f77 to your Fortran compiler" >> /dev/stderr
	echo    " before running rfcon again" >> /dev/stderr
	exit 1
endif

echo "converting $1.dat to solution label 1 of run dat"

test -d data || mkdir data

# check for shared object version of AUTO
# bug: these build rules should really be part of cmds.make
if(-ef AUTO_DIR/lib/libfcon.so) then
	if ( $?LD_LIBRARY_PATH ) then
		setenv LD_LIBRARY_PATH AUTO_DIR/lib:$LD_LIBRARY_PATH
	else
		setenv LD_LIBRARY_PATH AUTO_DIR/lib
	endif
	f77 $1.f -o fcon -LAUTO_DIR/lib -lfcon
else
	cp AUTO_DIR/include/fcon.h ./fcon.h
	f77 -o fcon -I. AUTO_DIR/src/fcon.f $1.f
endif

cp -f r.$1   fort.2
cp -f $1.dat fort.3
./fcon
cp -f fort.2 data/r.dat
mv -f fort.8 data/q.dat
echo "converting $1.dat to solution label 1 of run dat" > data/d.dat
echo "   0    PT  TY LAB   DATA FILE [time stamp]"  > data/p.dat
echo "  -1     1   9   1   $1.dat [`stat -c '%Y' $1.dat`]"    >> data/p.dat
rm -f fcon* fort.2 fort.3
