#!/bin/csh -f
# Script to import data from old SKB study

set srcdir  = ();
set sessdir = ();
set parfile = ();
set warn    = 0;

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

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

set StartDate = `date`;

## Go to the source directory ##
pushd $srcdir > /dev/null

#######################################################
# Check that everything is in the source directory
#######################################################
# There should be one directory under src #
set tmp = `ls | grep -v TRANS.TBL`;
if($#tmp != 1) then
  echo "ERROR: source format is not correct (1)"
  exit 1;
endif
if(! -d $tmp ) then
  echo "ERROR: source format is not correct (2)"
  exit 1;
endif
cd $tmp

## There should be a directory called "Bshorts" #
set BSDir = Bshorts
if(! -d $BSDir ) then
  set BSDir = bshorts
  if(! -d $BSDir ) then
    echo "ERROR: source format is not correct (3):"
    echo " no Bshorts or bshorts directory"
    pwd
    exit 1;
  endif
endif
cd $BSDir

## There should be a directory called "HiRes" ##
if(! -d HiRes) then
  echo "ERROR: source format is not correct (4):"
  echo " no HiRes directory"
  pwd
  exit 1;
endif

## There should be directories called RunX_FL ##
## Where X is a single digit number, and FL are the
## initials.
set RunList = `ls -d Run?_??`;
if($status) then
  echo "ERROR: cannot find any run directories"
  pwd
  exit 1;
endif
   
set bshortdir = `pwd`;

popd > /dev/null

######################################################
# Get full path of paradigm file 
set pardir  = `dirname $parfile`;
set parbase = `basename $parfile`;
pushd $pardir > /dev/null
set pardir = `pwd`;
popd > /dev/null
set parfile = $pardir/$parbase;

#######################################################
# Copy info over 
#######################################################

# Create and get full path for output sessdir #
echo "Creating session directory"
mkdir -p $sessdir
if($status) then
  echo "ERROR creating $sessdir"
  exit 1;
endif
pushd $sessdir > /dev/null
echo `basename $srcdir` > session.info

###-------------------------###
## T1 EPI (bfloat to bshort) ##
echo "Copying t1epi"
set instem = $bshortdir/HiRes/hres
if(! -e $instem"_000.bfloat" & ! -e $instem"_000.bshort") then
  echo "ERROR: cannot find slice 0 for $instem"
  exit 1;
endif
bfileconvert -i $instem -o t1epi/001/f -oext bshort
if($status) then
  echo "ERROR: bfileconvert failed"
  exit 1;
endif

## Copy the aws header ##
cp $bshortdir/HiRes/hres_.awshdr t1epi/001/f_.awshdr
if($status) then
  echo "WARNING: copy of t1epi awshdr failed"
  set warn = 1;
endif

## analyse.dat file ##
set andat = t1epi/001/analyse.dat
rm -f $andat
echo "`pwd`/t1epi/001" >> $andat
echo "f_%03d.bshort"   >> $andat
echo "20 1"    >> $andat
echo "128 128" >> $andat

## sequence info file ##
set seqfile = t1epi/seq.info
rm -f $seqfile
echo "sequencename unknown" >> $seqfile
echo "nx 128" >> $seqfile
echo "ny 128" >> $seqfile
echo "nz 20" >> $seqfile
echo "dx 3.125" >> $seqfile
echo "dy 3.125" >> $seqfile
echo "dz 7.000" >> $seqfile

###-------------------------###
## Functionals ##
echo "Copying functionals"
@ runno = 0;
foreach run ($RunList)
  @ runno = $runno + 1;

  set runid = `printf %03d $runno`;
  echo "---------- $run $runno $runid `date` -------------"

  set instem = $bshortdir/$run/$run;
  if(! -e $instem"_000.hdr" || ! -e $instem"_000.bshort" ) then
    echo WARNING: could not find $instem"_000.bshort"
    set warn = 1;
    continue;
  endif

  ## Copy functionals ##
  bfileconvert -i $instem -o bold/$runid/f -oext bshort
  if($status) then
    echo "WARNING: bfileconvert failed"
    set warn = 1;
    continue;
  endif

  ## Copy aws header ##
  cp $bshortdir/$run/$run"_.awshdr" bold/$runid/f_.awshdr
  if($status) then
    echo "WARNING: copy of $runno awshdr failed"
    set warn = 1;
  endif

  ## analyse.dat file ##
  set andat = bold/$runid/analyse.dat
  rm -f $andat
  echo "`pwd`/bold/$runid" >> $andat
  echo "f_%03d.bshort"   >> $andat
  echo "20 1"    >> $andat
  echo "128 128" >> $andat

  ## paradigm file ##
  if($#parfile != 0) then
    set newpar = bold/$runid/$parbase
    cp $parfile $newpar
    if($status) then
      echo "ERROR: copying $parfile to $newpar"
      exit 1;
    endif
  endif

end

## sequence info file ##
set seqfile = bold/seq.info
rm -f $seqfile
echo "sequencename unknown" >> $seqfile
echo "nx 64" >> $seqfile
echo "ny 64" >> $seqfile
echo "nz 20" >> $seqfile
echo "dx 3.125" >> $seqfile
echo "dy 3.125" >> $seqfile
echo "dz 7.000" >> $seqfile
echo "TR 2.5"   >> $seqfile

echo "------------------------------------------"

set EndDate = `date`;

echo "StartDate: $StartDate"
echo "EndDate:   $EndDate"

echo " "
if($warn) then
  echo "skb-import completed with warnings for $srcdir" 
else
  echo "skb-import completed successfully for $srcdir" 
endif

echo " "

exit 0

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

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

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

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

    case "-umask":
      if ( $#argv == 0) goto arg1err;
      echo "INFO: setting umask to $1"
      umask $1; shift;
      breaksw

    case "-par":
    case "-parfile":
    case "-p":
      if ( $#argv == 0) goto arg1err;
      set parfile = $1; shift;
      if(! -e $parfile ) then
        echo "ERROR: cannot find $parfile"
        exit 1;
      endif
      breaksw

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

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

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

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

end

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

  if ($#srcdir == 0) then
     echo "ERROR: no source directory specified"
     exit 1
  endif

  if ($#sessdir == 0) then
     echo "ERROR: no session directory specified"
     exit 1
  endif

  if($srcdir == $sessdir) then
    echo "ERROR: source and session directories are the same"
    exit 1;
  endif

  if(! -e $srcdir ) then
    echo "ERROR: source directory does not exist"
    exit 1;
  endif

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

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


############--------------##################
usage_exit:
  echo "USAGE: skb-import"
  echo "Options:";
  echo "   -srcdir dirname  : name of source directory"
  echo "   -sessdir dirname : name of target directory"
exit 1;
