#! /bin/csh -f

### help (TODO: put into csurf)
if ($#argv < 3) then
  echo ""
  echo "use: clonefunctHACT <functdir-with-briks> <cloneddirstem> <suff> ..."
  echo ""
  echo "  run after clonefunct"
  echo "  only runs inside 'image' subdir of funct scan directory"
  echo "  requires 'name' file parallel to 'image' directory"
  echo ""
  echo " #### DOES NOT WORK: need clonefunct in csurf to set bfloatpatt ###"
  echo ""
  echo "  e.g., clonefunctHACK pollet1 phavg-pollet1 0.5 1.0"
  echo ""
  exit
endif

### get args
set functdir = $argv[1]
set clonedirstem = $argv[2]
shift argv
shift argv
set suffs = ( $argv )
set imagedir = $cwd
set tail = $cwd:t
set session = $cwd:h

### checks
if ($tail != "image") then
  echo "clonefunctHACK: ### only runs in inside an 'image' subdir"
  exit
endif
if (! -e ../name) then
  echo "clonefunctHACK: ### namefile 'name' not found in parent"
  exit
endif
if (! -e $functdir) then
  echo "clonefunctHACK: ### $imagedir/$functdir not found"
  exit
endif
set nobrikflag = 0
if (`glob $functdir/*.BRIK` !~ *.BRIK)        set nobrikflag = 1
if ($nobrikflag) then
  echo "clonefunctHACK: ### no *.BRIK to link ...quitting"
  exit
endif

### go
echo "clonefunctHACK: making links to data files in functdir: $functdir"
echo "clonefunctHACK: session: $session"
echo "clonefunctHACK: subject: `cat ../name`"
set i = 0
foreach suff ( $suffs )
  set linkdir = $clonedirstem-$suff
  echo "clonefunctHACK: making $functdir BRIK links in $linkdir"
  if (! -e $imagedir/$linkdir) then
    echo "clonefunctHACK: linkdir not found  ...quitting"
    exit
  endif
  cd $imagedir/$functdir
  foreach file (*.BRIK *.HEAD)
    cd $imagedir/$linkdir
    if (! -e $file) then
      ln -s ../$functdir/$file $file
      @ i++
    endif
  end
end
echo "clonefunctHACK: made $i new links in $#suffs dirs"

