#! /bin/csh -f

#
# getnconds - returns a list of the number of conditions
#  for each session specified.  The number of conditions
#  does not include any condition with an id of 0.
#
# 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 parname = ();
set sesssubdir = "bold";

if($#argv == 0) then
  goto usage_exit;
  exit 1;
endif

set SessList = `getsesspath $argv`;
if($status || $#SessList == 0) then
  getsesspath $argv
  exit 1;
endif

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

set nconds = ();

foreach sess ($SessList)

  if(! -d $sess) then
    echo "ERROR: $sess does not exist" 
    exit 1;
  endif

  cd $sess/$sesssubdir
  set RunList = `getrunlist .`;
  if($status || $#RunList == 0) then
    echo "ERROR: $sess/$sesssubdir has no runs"
    exit 1;
  endif

  set pars = ();
  foreach run ($RunList)  
    set par = $run/$parname
    if(! -e $par ) then
      echo "ERROR: cannot find $par"
      exit 1;
    endif
    set pars    = ($pars $par);
  end

  # exclude condition 0 #
  set n = `cat $pars | awk '{print $2}' | sort | uniq | grep -e 0 -v -x | wc -l`;
  set nconds = ($nconds $n);

end

echo $nconds

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

############--------------##################
parse_args:
set cmdline = "$argv";
while( $#argv != 0 )

  set flag = $argv[1]; shift;
  
  switch($flag)

    case "-ssd":
      set sesssubdir = $argv[1]; shift;
      breaksw

    case "-parname":
      set parname = $argv[1]; shift;
      breaksw

    case "-verbose":
      set verbose = 1;
      breaksw

    case "-echo":
      set echo = 1;
      breaksw

    case "-debug":
      set verbose = 1;
      set echo = 1;
      breaksw

    case "-g":
    case "-s":
    case "-df":
    case "-d":
      shift;
      # ignore getsesspath arguments 
      breaksw

    default:
      # ignore other arguments
      breaksw
  endsw

end

goto parse_args_return;
############--------------##################

############--------------##################
check_params:

  if ($#SessList == 0) then
     echo "ERROR: no sessions specified"
     exit 1
  endif

  if ($#parname == 0) then
     echo "ERROR: no paradigm name specified"
     exit 1
  endif

goto check_params_return;
############--------------##################

############--------------##################
arg1err:
  echo "ERROR: flag $flag requires one argument"
  exit 1
############--------------##################

############--------------##################
usage_exit:
  echo "USAGE: getnconds"
  echo "Options:";
  echo "   -parname parname : paradigm name"
  echo "   -g grpdef1  <-g grpdef2>  : group def file"
  echo "   -s sessid1  <-s sessid2>  : session id"
  echo "   -d srchdir1 <-d srchdir2> : search directory"
  echo "   -df srchdirfile1 <-df srchdirfile2> : search dir file"
  echo "   -ssd dir : session subdirectory ($sesssubdir)"
exit 1;
