#! /bin/csh -f

#
# getstem
#
# Purpose: strips off the _XXX.bxxxx portion
#   of its argument to obtain the stem.  This
#   seems like a hokey way to do it, but it
#   works.
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:17 $
#    $Revision: 1.2 $
#
# Copyright (C) 2002-2007,
# The General Hospital Corporation (Boston, MA). 
# All rights reserved.
#
# Distribution, usage and copying of this software is covered under the
# terms found in the License Agreement file named 'COPYING' found in the
# FreeSurfer source code root directory, and duplicated here:
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
#
# General inquiries: freesurfer@nmr.mgh.harvard.edu
# Bug reports: analysis-bugs@nmr.mgh.harvard.edu
#


set VER = '$Id: getstem,v 1.2 2007/01/09 22:41:17 nicks Exp $';

if($#argv != 1) then
  echo "USAGE getstem bfilename"
  echo "  version: $VER"
  exit 1;
endif

set orig  = `basename $argv[1]`;
set odir  = `dirname $argv[1]`;
set fname = $orig
set c = `printf %c $fname`;
@ n = 1;
while("$c" != "_" && "$c" != "")
  #echo $c
  set fname = `echo $fname | sed s/$c//`;
  #echo $fname
  set c = `printf %c $fname`;
  @ n = $n + 1;
end

if("$c" == "_") then
  @ n = $n - 1;
  set fmt = `printf %%%d.%ds $n $n`;
  printf "$odir/$fmt" $orig
else
  echo "ERROR: getstem could not parse string"
  exit 1;
endif

exit 0;
#########################################
