#! /bin/csh -f

#
# getfirstrundir-sess -- gets first 3 digit, zero-padded directory.
#
# 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
#


if($#argv != 1) then
  echo "USAGE: getfirstrundir-sess directory"
  exit 1;
endif

set targdir = $argv[1];

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

cd $targdir
set targdir = `pwd`;

ls -d ??? >& /dev/null
if($status)then
  echo "ERROR: cannot find any run directories under $targdir"
  exit 1;
endif

set run = 0;
while ( $run < 100)
  set runstring = `printf %03d $run`;
  if(-d $runstring) then
    echo $targdir/$runstring;
    exit 0;
  endif

  @ run = $run + 1;
end

echo "ERROR: cannot find any run directories under $targdir"

exit 1;
#################################
