#!/bin/tcsh -fe

set stat = 0
set sdir = "$PWD"

# [PT: Apr 05, 2024] create
set demo = RAT_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
touch $demo


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:

cat <<EOF 

This script installs the demo archive for AFNI's rat FMRI analysis
demo. The download size is currently approximately 245MB.

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

EOF


goto END

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