#!/bin/csh -f
set GRECON2BSHORT_VER = '\$Id: grecon2bshort,v 1.2 1999/05/03 14:46:37 greve Exp $';

if($#argv < 4) then
  echo "USAGE: grecon2bshort -i instem -fgs firstgslice" 
  echo "       -nas nanatslices -nfs nfuncslices"
  echo "       -nr nRows -nc nCols -g group"
  echo "       -o outstem"
  echo ""
  echo "       instem:          (required) eg, P40960"
  echo "       firstgslice:     default   1"
  echo "       nanatslices:     default  16"
  echo "       nfuncslices:     default 128"
  echo "       nRows:           number of rows in the functional image (64)"
  echo "       nCols:           number of columns in the functional image (64)"
  echo "       group:           change permissions to make r/w by group"
  echo ""
  echo $GRECON2BSHORT_VER
  exit 1;
endif

#set Instem      = P40960
set FirstGSlice    =   1;
set nAnatSlices    =  16;
set nFuncSlices    = 128;
set instem       = ();
set outstem      = ();
set nRows       = 64;
set nCols       = 64;
set group = ();

goto parse_args;
parse_args_return:

if($#instem == 0) then
  echo "ERROR: must specify an input stem";
  exit 1;
endif

if($#outstem == 0) then
  set outstem = $instem;
  echo INFO: Setting output stem to input stem
endif

set SetGroup = 0;
if($#group > 0) set SetGroup = 1;

set OutDir = `dirname $outstem`;
if( ! -d $OutDir ) then
  echo "INFO: Creating output directory $OutDir";
  mkdir $OutDir;
  if($SetGroup) then
    chown $user":"$group $OutDir
    chmod wr+g $OutDir
  endif
endif

set nS  = 1;
set nAS = 0;

while($nAS < $nAnatSlices)

  if( $nAS < 10 ) then
    set SliceStr = "_00"$nAS;
  else if( $nAS < 100) then
    set SliceStr = "_0"$nAS;
  else
    set SliceStr = "_"$nAS;
  endif

  set OutFile = $outstem$SliceStr".bshort"
  rm -f $OutFile
  touch $OutFile
  echo $OutFile

  set nFS = 0;
  while($nFS < $nFuncSlices)
     # echo $nS

     if( $nS < 10 ) then
       set SStr = "000"$nS;
     else if( $nS < 100) then 
       set SStr = "00"$nS;
     else if( $nS < 1000) then
       set SStr = "0"$nS;
     else
       set SStr = $nS;
     endif

     set Infile = $instem"."$SStr;
     if( ! -r $Infile ) then
       echo "ERROR: cannot find $Infile";
       exit 1;
     endif

     cat $Infile >> $OutFile ;
     #echo $Infile $OutFile

     @ nFS = $nFS + 1;
     @ nS = $nS + 1;

  end 

  set HeaderFile = $outstem$SliceStr".hdr"
  echo $nRows $nCols $nFuncSlices 0 > $HeaderFile

  @ nAS = $nAS + 1;
end

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

############--------------##################
parse_args:

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

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

    case "-i":
      if ( $#argv == 0) goto arg1err;
      set instem = $argv[1]; shift;
      breaksw

    case "-fgs":
      if ( $#argv == 0) goto arg1err;
      set FirstGSlice = $argv[1]; shift;
      breaksw

    case "-nas":
      if ( $#argv == 0) goto arg1err;
      set nAnatSlices = $argv[1]; shift;
      breaksw

    case "-nfs":
      if ( $#argv == 0) goto arg1err;
      set nFuncSlices = $argv[1]; shift;
      breaksw

    case "-nr":
      if ( $#argv == 0) goto arg1err;
      set nRows = $argv[1]; shift;
      breaksw

    case "-nc":
      if ( $#argv == 0) goto arg1err;
      set nCols = $argv[1]; shift;
      breaksw

    case "-o":
      if ( $#argv == 0) goto arg1err;
      set outstem = $argv[1]; shift;
      breaksw

    case "-g":
      if ( $#argv == 0) goto arg1err;
      set group = $argv[1]; shift;
      breaksw

    default:
      echo ERROR: Flag $flag unrecognized.
      echo $cmdline
      exit 1
      breaksw
  endsw

end
goto parse_args_return;


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