#! /bin/sh
#
#Bourne shell program to run absorb repeatedly, changing faceml parameters
#J Hester, Oct 91
#Replaces version involving mathematica
#
#First set initial values
if [ $# -lt 6 ]
	then echo 'Not enough arguments on the command line.'
	echo 'Usage: newabs [-g] line links field increment number compid'
	exit
fi
execute="/usr1/XTAL/bin/xtal30.x"	#xtal executable
machine="xtal"		#default machine is xtal
while [ $# -gt 6 ]		#while options are present
	do
	case $1 in
	-g)
	machine="graf"
	shift;;			#have used up option
	*) echo $1 'is not a valid option'
	esac
	done
#initialise variables from the command line
number=$5	#number of times	
filename=$6	#filename (without extension)
count=0 	#count number of increments applied
echo '*****Beginning Run*******' >> "$filename.cycles"
cp $filename.psd $filename.000	#copy bdf output from difdat
if [ $machine = "graf" ]
then
rsh graf "cd $PWD;/usr1/syd/3.0/xtal.x" < $filename.dat | grep 'SUM(Y)' >> $filename.cycles
else
/usr1/XTAL/bin/xtal30.x < $filename.dat | grep 'SUM(Y)' >> $filename.cycles
fi
#go through using unchanged file
while [ "$count" -lt "$number" ]
do
count=`expr $count + 1`		#another time through
cp $filename.psd $filename.000	#copy bdf output from difdat
#now edit dat file using awk
awk 'BEGIN {lineno = 0
}
	{
	if ($1 != "faceml")
	{
	print $0 
	}
	else 
	{
	lineno++	#keep count of number of faceml lines
	if ((lineno >= line) && (lineno < line  + links)) 
	{
	split($0,parts,FS)	#allocate fields of line
	parts[field] += incr 	#increment this field
	printf "faceml %-3d %-3d %-3d %-7.4f\n",parts[2],parts[3],parts[4],\
	parts[5]
	printf "faceml %-3d %-3d %-3d %-7.4f\n",parts[2],parts[3],parts[4],\
	parts[5] >> out          #also output change to record file
	}
	else
	print $0 
	}
	}' line=$1 links=$2 field=$3 incr=$4 out=$filename.cycles\
	$filename.dat > $filename.tmp
	#end of file edit;  now give it to xtal....
cp $filename.tmp $filename.dat
if [ $machine = "graf" ]
then
rsh graf "cd $PWD;/usr1/syd/3.0/xtal.x" < $filename.dat | grep 'SUM(Y)' >> $filename.cycles
else
/usr1/XTAL/bin/xtal30.x < $filename.dat | grep 'SUM(Y)' >> $filename.cycles
fi
done	#go back for another round
rm -f *.xx?	# get rid of scratch files
rm -f $filename.tmp	#remove temporary file
exit
