#!/bin/bash

if [[ $# -lt 3 ]] ; then 
  echo "Incorrect Number of Paramaters Specified"
  echo "Usage: <original bvecs> <rotated bvecs> <ecclog> [-k]"
  echo ""
  echo "<ecclog>		the output log file from ecc"
  echo "-k				keep the mat files"
  echo ""
  exit 1;
fi

ECCLOG=$3;
LIST="mat.list"
  
if [ ! -e $1 ] ; then
	echo "Source bvecs $1 does not exist!"
	exit 1
fi

  
if [ -e $2 ] ; then
	echo "Target bvecs: $2 already exists!"
	exit 1
fi

if [ ! -e $ECCLOG ]; then
	echo "Ecc log file $3 does not exist!"
	exit 1
fi

if [ "$4" == "-k" ] ; then
	KEEP="YES"
else
	KEEP="NO"
fi

####################################################################
# Create the mat files from input
#
# Here we read input ecclog file line by line, produce the FSL mat
# file and fill it with respective trasformation. File listing all
# the mat files generates is also created

if [ -e ${LIST} ]
then
	rm ${LIST}
fi

cat ${ECCLOG} | while read line; do
    #create a file name from processed volumes
    matfile=$(remove_ext $(echo ${line} | grep processing | gawk '{print $2}'));
    if [ "${matfile}" != "" ] ; then
	   matfile=${matfile}.mat;
       echo "Generating ... ${matfile}";
       echo ${matfile} >> ${LIST};
       # following two reads will deal with unimportant lines
       read line;
       read line;
       # read matrix and store it in the current matfile
       read line;
       echo ${line} > ${matfile};
       read line;
       echo ${line} >> ${matfile};
       read line;
       echo ${line} >> ${matfile};
       read line;
       echo ${line} >> ${matfile};
    fi
 done
#
####################################################################

####################################################################
# rotate bvecs

newXs="";
newYs="";
newZs=""


BVECS=$1;
Xs=$(cat $BVECS | head -1 | tail -1)
Ys=$(cat $BVECS | head -2 | tail -1)
Zs=$(cat $BVECS | head -3 | tail -1)

MATs=$(cat mat.list);

VOLUMES=$(cat $BVECS | head -1 | tail -1 | wc -w)

if [ $VOLUMES != $(echo ${MATs} | wc -w) ]
then
	echo "Number of *.mat files in $3 is not equal to number"
	echo "of gradients in $BVECS!"
	exit 1
fi

i=1
while [ $i -le $VOLUMES ] ; do
	MAT=$(echo ${MATs} | cut -d " " -f ${i});
	#echo $MAT

	output=$(avscale --allparams ${MAT} | head -2 | tail -1)
	m11=$(echo $output | cut -d " " -f 1)
	m12=$(echo $output | cut -d " " -f 2)
	m13=$(echo $output | cut -d " " -f 3)
	m11=$(printf "%1.7f" $m11)
	m12=$(printf "%1.7f" $m12)
	m13=$(printf "%1.7f" $m13)

	output=$(avscale --allparams ${MAT} | head -3 | tail -1)
	m21=$(echo $output | cut -d " " -f 1)
	m22=$(echo $output | cut -d " " -f 2)
	m23=$(echo $output | cut -d " " -f 3)
	m21=$(printf "%1.7f" $m21)
	m22=$(printf "%1.7f" $m22)
	m23=$(printf "%1.7f" $m23)

	output=$(avscale --allparams ${MAT} | head -4 | tail -1)
	m31=$(echo $output | cut -d " " -f 1)
	m32=$(echo $output | cut -d " " -f 2)
	m33=$(echo $output | cut -d " " -f 3)
	m31=$(printf "%1.7f" $m31)
	m32=$(printf "%1.7f" $m32)
	m33=$(printf "%1.7f" $m33)

	X=$(echo $Xs | cut -d " " -f "$i")
	Y=$(echo $Ys | cut -d " " -f "$i")
	Z=$(echo $Zs | cut -d " " -f "$i")
	X=$(printf "%1.7f" $X)
	Y=$(printf "%1.7f" $Y)
	Z=$(printf "%1.7f" $Z)

	rX=$(echo "scale=7;  ($m11 * $X) + ($m12 * $Y) + ($m13 * $Z)" | bc -l);
	rY=$(echo "scale=7;  ($m21 * $X) + ($m22 * $Y) + ($m23 * $Z)" | bc -l);
	rZ=$(echo "scale=7;  ($m31 * $X) + ($m32 * $Y) + ($m33 * $Z)" | bc -l);

	rX=$(printf "%1.7f" $rX)
	rY=$(printf "%1.7f" $rY)
	rZ=$(printf "%1.7f" $rZ)

#	echo $rX" "$rY" "$rZ;

	rXs=${rXs}${rX}" ";
	rYs=${rYs}${rY}" ";
	rZs=${rZs}${rZ}" ";

	i=$(echo "$i + 1" | bc) ;
done

echo "$rXs" >> $2;
echo "$rYs" >> $2;
echo "$rZs" >> $2;
#
####################################################################

if [ "$KEEP" == "NO" ]; then
	rm $MATs $LIST
fi
