#!/bin/tcsh -f
#################################################
## 11/2017 Justin Rajendra
## launch 3dMVM validator shiny app

## get the current program name
set prog = `basename $0`

## find afni and set the shiny path
set afni_bin  = `which afni`
set afni_dir = `dirname $afni_bin`
set ShinyFolder = ""
set InTable = ""

## show help
if ( $#argv < 1 ) then
    goto SHOW_HELP
endif

## parse args
set narg = 1
@  amax = $#argv - 0
while ( $narg <= $amax )
    if ( "$argv[$narg]" == "-help" || "$argv[$narg]" == "-h" ) then
        goto SHOW_HELP
    else if ( "$argv[$narg]" == "-ShinyFolder" ) then
        if ( $narg >= $#argv ) goto FAIL_MISSING_ARG
        @ narg ++
        set ShinyFolder = `echo $argv[$narg]`
    else if ( "$argv[$narg]" == "-dataTable" ) then
        if ( $narg >= $#argv ) goto FAIL_MISSING_ARG
        @ narg ++
        set InTable = `echo $argv[$narg]`
    endif
    @ narg ++
end

## check the inputs
if ( "$InTable" == "" ) then
    echo ; echo "Error: -dataTable seems to be wrong." ; echo
    goto SHOW_HELP
endif
if ( ! -f $InTable ) then
    echo ; echo "Error: Cannot find -dataTable ${InTable}." ; echo
    exit 1
endif

## if there is no custom folder, use the standard one
if ( "$ShinyFolder" == "" ) then
    set ShinyFolder = "${afni_dir}/shiny/3dMVM_validator_shiny"
endif
if ( ! -d $ShinyFolder ) then
    echo ; echo "Error: Cannot find -ShinyFolder ${ShinyFolder}." ; echo
    exit 1
endif

## get the current directory
set dir_loc = `pwd`
echo $dir_loc

## get full path for the input table
set table_file = `basename $InTable`
set temp_dir = `dirname $InTable`
cd $temp_dir
set file_dir = `pwd`
cd -
set full_path_file = "${file_dir}/${table_file}"

## run shiny
R --slave --no-restore \
-e "shiny::runApp('$ShinyFolder', launch.browser=TRUE)" \
--args $full_path_file $dir_loc &
exit 0

###########################################################################
FAIL_MISSING_ARG:
   echo ; echo "ERROR: missing parameter for option $argv[$narg]" ; echo
exit 1

###########################################################################
SHOW_HELP:
cat << EOF

   ----------------------------------------------------------------------------
   $prog
      Launch the 3dMVM model validation shiny app in a web browser.
      Input is a file containing a table formatted like the 3dMVM "-dataTable".
      See 3dMVM -help for the correct format.
      This will create a temporary folder in the current directory with a
      random name similar to:
      __8726_3dMVM_validator_temp_delete
      It will be deleted when you close the shiny app. If it is still there
      after you close the app, it is safe to delete.
      If you seem to be missing some R packages, you may need to run:
      @afni_R_package_install -shiny

   -----------------------------------------------------------------------------
   options:
      -dataTable   : A file containing a data table formatted like the
                     3dMVM "-dataTable".
      -ShinyFolder : Use a custom shiny folder (for testing purposes).
      -help        : show this help

   -----------------------------------------------------------------------------
   examples:
      $prog -dataTable ~/my_dataTable.csv

   -----------------------------------------------------------------------------
   Justin Rajendra 11/2017

EOF

exit 0
