#! /bin/tcsh -f

#
# fsr-getxopts
#
# USAGE: fsr-getxopts cmd xoptsfile
#
# Parses expert options for freesurfer recon.
# Looks in xoptsfile for a line with cmd as the first item,
# then returns the rest of the items on that line. Ignores
# all lines with # in them, regardless of where on the line
# they occur.
#
# Eg, if the file expert.opts contains:
#   mri_em_register -p .5
# Then
#   fsr-getxopts mri_em_register expert.opts
# will return
#   -p .5
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2011/03/02 20:16:39 $
#    $Revision: 1.5 $
#
# 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: fsr-getxopts,v 1.5 2011/03/02 20:16:39 nicks Exp $';

if($#argv < 2) exit 0;

set cmd       = $argv[1];
set xoptsfile = $argv[2];

if(! -e $xoptsfile) then
  echo "ERROR: cannot find $xoptsfile"
  exit 1;
endif

set tmp = `grep $cmd $xoptsfile | wc -l`;
if($tmp == 0) exit(0);
if($tmp > 1) then
  echo "ERROR: cmd $cmd represented more than once in $xoptsfile"
  exit 1;
endif

set tmp = `grep -v \# $xoptsfile | grep $cmd `;
echo "$tmp[2-$#tmp]"

exit 0
