#! /bin/csh -f
#
# fname2stem
#
# Converts the name of a file to a stem,
#   Eg, f.mgh f.nii f.nii.gz would return f
#   Eg, here/f.mgh would return here/f
#   This works only on the string passed to it,
#   The file does not need to exist.
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2011/03/02 20:16:39 $
#    $Revision: 1.4 $
#
# Copyright © 2011 The General Hospital Corporation (Boston, MA) "MGH"
#
# Terms and conditions for use, reproduction, distribution and contribution
# are found in the 'FreeSurfer Software License Agreement' contained
# in the file 'LICENSE' found in the FreeSurfer distribution, and here:
#
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferSoftwareLicense
#
# Reporting: freesurfer@nmr.mgh.harvard.edu
#
#


set VERSION = '$Id: fname2stem,v 1.4 2011/03/02 20:16:39 nicks Exp $';

if($#argv != 1) then
  echo "fname2stem filename"
  echo "  Converts the name of a file to a stem"
  echo "  Eg, f.mgh f.nii f.nii.gz would return f"
  echo "  The file does not need to exist. See also stem2fname"
  exit 1;
endif

set fname = $argv[1];
set dname = `dirname $fname`;
set fname = `basename $fname`;

foreach ext (mgh mgz nii nii.gz img bhdr)
  set tmp = `basename $fname .$ext`;
  if($fname != $tmp) then
    if($dname == ".") then
      echo $tmp
    else
      echo $dname/$tmp;
    endif
    exit 0;
  endif
end

echo "ERROR: cannot determine stem"
exit 1;


