#!/usr/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
#
# Copyright © 2021 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 = 'fname2stem mockbuild-local';

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 annot m3z gii)
  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;


