#!/bin/tcsh -ef

# --------------------- revision history -------------------------
# Jan, 2017
#   + rename
# ----------------------------------------------------------------

set version = "1.0"
set rev_dat = "Jan, 2017"
set this_prog = "fat_proc_dwi_to_dt"

set idwis    = ""
set ibmat    = ""
set odir     = ""
set opref    = ""
set ori_new  = "RPI"       # default file output orientation
set doflip   = ""          # default: no flip of grads




# ------------------- process options, a la rr ----------------------

if ( $#argv == 0 ) goto SHOW_HELP

set ac = 1
while ( $ac <= $#argv )
    # terminal options
    if ( ("$argv[$ac]" == "-h" ) || ("$argv[$ac]" == "-help" )) then
        goto SHOW_HELP
    endif
    if ( "$argv[$ac]" == "-ver" ) then
        goto SHOW_VERSION
    endif

    #here, specify NIFTI *files*, not directories
    if ( "$argv[$ac]" == "-in_dwis" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set idwis = "$argv[$ac]"

    if ( "$argv[$ac]" == "-in_bmat" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set ibmat = "$argv[$ac]"

    else if ( "$argv[$ac]" == "-outdir" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set odir = "$argv[$ac]"

    else if ( "$argv[$ac]" == "-prefix" ) then
        if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
        @ ac += 1
        set opref = "$argv[$ac]"

   else if ( "$argv[$ac]" == "-gradomat" ) then
      if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
      @ ac += 1
      set doflip = "-$argv[$ac]"

      if ( ("$doflip" == "-flip_x") ||   \
           ("$doflip" == "-flip_y") ||   \
           ("$doflip" == "-flip_y") ) then
           echo "++ OK, will flip grads with: $doflip"
      else
           echo "** ERROR: that flip option not allowed."
           echo "   You can use only one of:"
           echo "\t -flip_x , -flip_y , -flip_z"
      endif

   else if ( "$argv[$ac]" == "-orient" ) then
      if ( $ac >= $#argv ) goto FAIL_MISSING_ARG
      @ ac += 1
      set ori_new = "$argv[$ac]"

    # other stuff.....
    # !!!!! structural, for orientation??

    else
        echo "** unexpected option #$ac = '$argv[$ac]'"
        exit 2

    endif
    @ ac += 1
end

# =======================================================================
# ============================ ** SETUP ** ==============================
# =======================================================================

echo "++ Start script version: $version"

# ============================= dicom dir ===============================

if ( ("$idir_ap" == "") && ("$idir_pa" == "") ) then
    echo "** ERROR: need to input at least one DICOM directory,"
    echo "\t using either after '-indir_ap' or '-indir_pa'."
    exit
endif
