#!/bin/tcsh -f

set stat = 0
set sdir = $PWD
set ok_1surf = 0
set ok_Gsurf = 0
set ok_Gvol = 0

set cdir = `\pwd | \grep AFNI_InstaCorrDemo`
if ($status == 0 && ${cdir} != '') then
   echo "You should not run this command from a directory under AFNI_InstaCorrDemo"
   set sugdir = `echo $cdir | sed 's/AFNI_InstaCorrDemo/ /g' | cut -f 1 -d ' '`
   echo "Consider running this script again with: "
   echo "   cd $sugdir"
   echo "   `basename $0` $argv[*]"
   echo ""
   goto END
endif

goto PARSE
RET_PARSE:

if ( -f AFNI_InstaCorrDemo/srf/rest_sub99965.STL_rh_SM.niml.dset.1D.smrec ) then
   echo "Subject single surf processed"
   set ok_1surf = 1
endif
if ( -f AFNI_InstaCorrDemo/srf/G1.LR.grpincorr.niml ) then
   echo "Subject group surf processed"
   set ok_Gsurf = 1
endif
if ( -f AFNI_InstaCorrDemo/vol/G1.grpincorr.niml ) then
   echo "Subject group vol processed"
   set ok_Gvol = 1
endif

if ( $ok_Gvol == 1 || $ok_Gsurf == 1 || $ok_1surf == 1 )  then
echo ""
echo "Notice:"
echo "   Output files already exist."
echo "   If you need to reinstall, remove both AFNI_InstaCorrDemo and"
echo "   AFNI_InstaCorrDemo.tgz with:"
echo "\\rm -rf AFNI_InstaCorrDemo.tgz AFNI_InstaCorrDemo"
echo "   then run `basename $0` $argv[*] again."
echo ""
echo "Otherwise to just run the demos do:"
echo ""
set ret_coms = END
goto SHOW_COM
   endif

if ( ! -f AFNI_InstaCorrDemo.tgz) then
   if ($use_curl == -1) then
      which curl
      if ($status) then
         set use_curl = 0;
      else
         set use_curl = 1;
      endif
   endif

   if ($use_curl == 0) then
      wget http://afni.nimh.nih.gov/pub/dist/data/AFNI_InstaCorrDemo.tgz
   else
      curl -O http://afni.nimh.nih.gov/pub/dist/data/AFNI_InstaCorrDemo.tgz
   endif
   tar xvzf AFNI_InstaCorrDemo.tgz
else
   echo "Reusing existing AFNI_InstaCorrDemo.tgz"
   tar xvzf AFNI_InstaCorrDemo.tgz
endif

cd AFNI_InstaCorrDemo/vol
   tcsh ./@SetupVolGroupInCorr
cd -
cd AFNI_InstaCorrDemo/srf
   tcsh ./@SetupSurfGroupInCorr
   tcsh ./@RunSingleSurfInstaCorr -setup_only
cd -

if ( ! -d AFNI_InstaCorrDemo/srf ) then
   echo "Installation Failed"
   goto BEND
endif
set ret_coms = END
goto SHOW_COM

SHOW_COM:
   echo "*****************"
   echo ""
   echo "For Volume-based 3dGroupInCorr run:"
   echo "cd $PWD/AFNI_InstaCorrDemo/vol"
   echo "tcsh ./@RunVolGroupInCorr "
   echo ""
   echo "For Surface-based 3dGroupInCorr run:"
   echo "cd $PWD/AFNI_InstaCorrDemo/srf"
   echo "tcsh ./@RunSurfGroupInCorr "
   echo ""
   echo "For Surface-based Single-Subject InstaCorr run:"
   echo "cd $PWD/AFNI_InstaCorrDemo/srf"
   echo "tcsh ./@RunSingleSurfInstaCorr "
   echo ""
   echo "*****************"
goto $ret_coms

goto END

PARSE:
   set Narg = $#
   set use_curl = -1
   set cnt = 1
   while ($cnt <= $Narg)
		set donext = 1;
      if ($donext && "$argv[$cnt]" == "-echo") then
         set echo
         set donext = 0; goto NEXT		
      endif
      if ($donext && "$argv[$cnt]" == "-curl") then
         set use_curl = 1
         set donext = 0; goto NEXT		
      endif
      if ($donext && "$argv[$cnt]" == "-wget") then
         set use_curl = 0
         set donext = 0; goto NEXT		
      endif
      if ($donext && ("$argv[$cnt]" == "-h" || "$argv[$cnt]" == "-help")) then
         goto HELP
         set donext = 0;	 goto NEXT	
      endif
      if ($donext == 1) then
         echo "Error: Option or parameter '$argv[$cnt]' not understood"
         goto END
      endif
      
      NEXT:
		@ cnt ++
	end
   
goto RET_PARSE

HELP:
   echo "Installs and sets up AFNI's InstaCorr demo archive"
   echo "After setup, all you need to do is run the demo scripts"
   echo "this way:"
   set ret_coms = HELP_COM
   goto SHOW_COM
   HELP_COM:
   echo "Options:"
   echo "[-wget]: Use wget to download archive. Script chooses by default"
   echo "         with preference for curl"
   echo "[-curl]: Use curl to download archive. Script chooses by default"
   echo "         with preference for curl"
   echo ""
   echo "It takes a while to download, unpack, and run the setup scripts"
   echo ""
   goto END

BEND:
   echo "Failed"
   set stat = 1
   goto END
   
END:
   exit $stat
