#! /bin/sh
#
#Bourne shell program to run CRYLSQ repeatedlY
# based on NEWABS by J Hester, Oct 91
#
#First set initial values

if [ $# -lt 7 ]
	then echo 'Not enough arguments on the command line.'
echo 'Usage: extscal  scf-init scf-inc scf-num  ext-init ext-inc ext-num compid'
	exit
fi

progrm="xtal"
	
#initialise variables from the command line
scf0=$1         # scale factor origin
scfinc=$2       # scale factor increment
scfnum=$3	# number of times to increment scale factor
ext0=$4         # scale factor origin
extinc=$5       # scale factor increment
extnum=$6	# number of times to increment scale factor
filename=$7	#filename (without extension)

scfcount=0 	#count number of increments applied
extcount=0      #count number of increments applied
initflg=1       # flag to initialise scale
echo '*****Beginning Run*******' >> "$filename.cycles"



# begin first loop over scale factor
while [ "$scfcount" -lt "$scfnum" ]
do
scfcount=`expr $scfcount + 1`		#another time through
echo $scfcount 
resetflg=1                              # have to reset the extinc origin

# begin second loop over extinction factor
while [ "$extcount" -lt "$extnum" ]
do
extcount=`expr $extcount + 1`		#another time through
echo $extcount


#now edit dat file using awk
awk 'BEGIN {lineno = 0  }
{
	if ( $1 != "scale" && $1 != "extinc" ) { print $0 }
	else {
		if ( testf == 1) {  # have to reset the initial extinc value
		   if ($1 == "extinc" ) {
			split($0,parts,FS)	#allocate fields of line
			parts[5] = extor 	# reset initial extinc origin
			printf "extinc zach is gaus %-8.4f \n",parts[5] #
			printf "extinc zach is gaus %-8.4f \n",parts[5] >> out 
			}
		   else if (init == 1 ){        # must be scale
			split($0,parts,FS)	#allocate fields of line
			parts[2] = scfor 	#reset initial scale origin
			printf "scale %-8.4f \n",parts[2]  # modify this line
			printf "scale %-8.4f \n",parts[2] >> out # record
			}
		   else if (init != 1 ){        # must be scale
			split($0,parts,FS)	#allocate fields of line
			parts[2] += scfinc 	#reset initial scale origin
			printf "scale %-8.4f \n",parts[2]  # modify this line
			printf "scale %-8.4f \n",parts[2] >> out # record
			}
		}
		else if ($1 == "extinc" ) {
		split($0,parts,FS)	#allocate fields of line
		parts[5] += extinc 	#increment this field
		printf "extinc zach is gaus %-8.4f \n",parts[5] # modify line
		printf "extinc zach is gaus %-8.4f \n",parts[5] >> out #record
		}
		else if ($1 == "scale" ) { print $0 }
		}
}'  scfor=$scf0 extor=$ext0 scfinc=$scfinc extinc=$extinc init=$initflg \
    testf=$resetflg out="$filename.cycles"  $filename.dat > $filename.tmp

#
resetflg=0
initflg=0
	#end of file edit;  now give it to xtal....
cp $filename.tmp $filename.dat
#

##go through using unchanged file
#/usr/local/XTAL/bin/${progrm}_${ARCH}.x < $filename.dat | grep 'R-factor' >>  $filename.cycles
#

done	#end of second (inner) loop over extinction
extcount=0      #count number of increments applied

done	#end of first (outer) loop over scale


#rm -f *.xx?	# get rid of scratch files
#rm -f $filename.tmp	#remove temporary file
exit
