#!/bin/tcsh -f
#################################################
## 08/02/2017 Justin Rajendra
## launch shiny app of Cluster Explorer output

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

## 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
        set InFolder = ($*)
    endif
    @ narg ++
end

## check the inputs
if ( -d $InFolder ) then
    ## get the absolute path
    cd $InFolder
    set ShinyPath = `pwd`

    ## check for global.R
    if ( ! -f "global.R" ) then
        echo ; echo "Error: ${InFolder} does not contain a shiny app." ; echo
        exit 1
    endif

    ## go back
    cd -
else
    echo ; echo "Error: ${InFolder} does not exist." ; echo
    exit 1
endif

## all good. run shiny
R --slave --no-restore -e "shiny::runApp('$ShinyPath', launch.browser=TRUE)"  &
exit 0

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

   ----------------------------------------------------------------------------
   $prog -
      Launch a shiny app that was created by ClustExp_StatParse.py
      Takes one argument that is the folder created by ClustExp_StatParse.py.

   -----------------------------------------------------------------------------
   options:
      -help : show this help
   -----------------------------------------------------------------------------
   examples:
      $prog ~/discoMVM_ClustExp_shiny
   -----------------------------------------------------------------------------
   Justin Rajendra 08/2017

EOF

exit 0
