#!/bin/tcsh -fe

set stat = 0
set sdir = "$PWD"

# [PT: May 1, 2020]   updated version
# [PT: July 30, 2020] updated version, run @Install_NMT here, too
# [PT: July 31, 2020] option for getting smaller 'lite' version of demo
# [PT: Aug 4, 2020]   just use simple dir names, without ver num
# [PT: Oct 29, 2021]  @Install_NMT gets v2.1
set demo = MACAQUE_DEMO_REST

goto PARSE
RET_PARSE:

if ( -d ./$demo) then
echo ""
echo "ERROR:"
echo "   Directory ./$demo exists already"
echo "   If you want to recreate it, remove it with:"
echo "\rm -rf ./$demo"
echo "   then run `basename $0` $argv[*] again."
echo ""
echo "Otherwise to run demos, see content of ./$demo/README.txt"
echo ""
goto END
   endif

echo "++ Going to fetch demo:  ${demo}"

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 https://afni.nimh.nih.gov/pub/dist/tgz/$demo.tgz
else
   curl -O https://afni.nimh.nih.gov/pub/dist/tgz/$demo.tgz
endif
tar xvzf $demo.tgz
cd $demo

echo "++ Getting standard reference datasets:"
echo "     symmetric NMT v2.1, CHARM, D99 atlas, and more."

if ($use_curl == 0) then
   @Install_NMT -wget -sym sym -nmt_ver 2.1
else
   @Install_NMT -curl -sym sym -nmt_ver 2.1
endif

echo "********************************************************"
echo "Follow examples in ./${demo}/README.txt "
echo "********************************************************"

tcsh check_for_template.tcsh

cd -

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]" == "-lite_version") then
         set demo = MACAQUE_DEMO_REST_LITE
         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:

cat <<EOF

Installs the demo archive for AFNI's resting state FMRI
macaque-analysis demo.  The Demo contains 6 subjects, each with 2 EPIs
and one anatomical reference.  Processing includes using
@animal_warper, afni_proc.py and 3dNetCorr.

After the archive is downloaded and unpacked, see its README.txt for
details.

OPTIONS

[-wget]         : Use wget to download archive. Script chooses by default
                  with preference for curl

[-curl]         : Use curl to download archive. Script chooses by default
                  with preference for curl

[-lite_version] : Download a version of the Demo in which the EPIs
                  have been truncated to 75 points each.  This makes
                  the download size less than half of the original,
                  and means the afni_proc.py processing will run
                  faster.  Note that the processing outputs will look
                  a bit different-- but that might be fine for quicker
                  learning purposes.

EOF

   goto END

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