#!/bin/sh

##
##  Copyright 2012, 2014 SRI International
##
##  Copyright 2012 Torsten Rohlfing
##
##  This file is part of the Computational Morphometry Toolkit.
##
##  http://www.nitrc.org/projects/cmtk/
##
##  The Computational Morphometry Toolkit is free software: you can
##  redistribute it and/or modify it under the terms of the GNU General Public
##  License as published by the Free Software Foundation, either version 3 of
##  the License, or (at your option) any later version.
##
##  The Computational Morphometry Toolkit is distributed in the hope that it
##  will be useful, but WITHOUT ANY WARRANTY; without even the implied
##  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License along
##  with the Computational Morphometry Toolkit.  If not, see
##  <http://www.gnu.org/licenses/>.
##
##  $Revision: 5378 $
##
##  $LastChangedDate: 2015-01-17 11:31:55 -0800 (Sat, 17 Jan 2015) $
##
##  $LastChangedBy: torstenrohlfing $
##

export CMTK_BINARY_DIR=${CMTK_BINARY_DIR:-/usr/lib/cmtk/bin}
 
# Get the cmtk_functions.sh script from the scripts/ directory in the CMTK source tree
. ${CMTK_BINARY_DIR}/cmtk_functions.sh
 
# Check for command line arguments, print help if none given
if test $# -lt 3; then
    echo "Reformat all floating images previously aligned by one of CMTK's groupwise registration tools."
    echo
    echo "USAGE: $0 [OPTIONS] groupwiseXform templateGrid outputPathPattern [XFORMS ...]"
    exit 2
fi

# put all arguments starting with "-" into reformat options
reformatOptions=""
while expr "$1" : ^- >/dev/null; do
    reformatOptions="${reformatOptions}$1 "
    shift
done

# first non-option argument is groupwise xform
groupwiseXform=$1
if [ ! -f ${groupwiseXform} ]; then
    groupwiseXform=${groupwiseXform}.gz
    if [ ! -f ${groupwiseXform} ]; then
	echo "ERROR: could not find groupwise transformation $1"
	exit 1
    fi
fi

# second argument is template grid path
templateGrid=$2
if ! ${CMTK_BINARY_DIR}/describe ${templateGrid} > /dev/null; then
    echo "ERROR: template grid ${templateGrid} is not a valid image."
    exit 1
fi

# third argument is output pattern
outPattern=$3

# everything else is optional xforms to be added before the groupwise alignment
shift 3
reformatXforms="$*"

make_output_path()
{
    local target=$1
    local pattern=$2

    local base=`basename $target | sed 's/\..*//g'`
    local dir=`dirname $target`

    local idx=1
    while [ "${base}" != "" ]; do
	pattern=`eval "echo $pattern | sed 's/%${idx}/${base}/g'"`

	base=`basename $dir | sed 's/\.,*//g'`
	dir=`dirname $dir`

	$((idx=idx+1))
    done

    echo ${pattern}
}

process_target()
{
    local target=$1

    local tmp=`mktemp`
    echo "! TYPEDSTREAM 1.1" > ${tmp}

    while IFS="" read line; do
	if expr "${line}" : ^\} >/dev/null; then
	    break;
	fi

	echo "${line}" >> ${tmp}
    done
    echo "}" >> ${tmp}

    outputPath=`make_output_path ${target} ${outPattern}`

    if [ "${outputPath}" != "" ]; then
	${CMTK_BINARY_DIR}/reformatx ${reformatOptions} --floating ${target} --outfile ${outputPath} ${templateGrid} ${tmp} ${reformatXforms}
    fi

    rm -rf ${tmp}
}

parse_xform_file()
{
  while read line; do
    if expr "${line}" : "target" >/dev/null; then
	target=`echo ${line} | sed 's/target \"//g; s/\".*//g'`
	process_target ${target}
    fi
  done
}

gzip -cdf ${groupwiseXform} | parse_xform_file
