#!/bin/tcsh -f

set stat = 0
set sdir = $PWD

set demo = Suma_TSrestMovieDemo


goto PARSE
RET_PARSE:

if ( -d ./$demo/SUMA_Recordings) then
echo ""
echo "ERROR:"
echo "   Directory ./$demo/SUMA_Recordings exists already"
echo "   If you want to recreate movie frames, remove it with:"
echo "\rm -rf ./$demo/SUMA_Recordings"
echo "   then run `basename $0` $argv[*] again."
echo ""
echo "Otherwise to just view the images run:"
echo "   aiv $demo/SUMA_Recordings/*jpg &"
echo "Press 'v' in the image window to start the movie, space bar to stop."
echo ""
goto END
   endif

which curl
if ($status) 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 "********************************************************"
   echo "Script @CreateMovie will now be used to automatically "
   echo "generate a movie of the time series at 30 frames per "
   echo "second. You should see SUMA working on your screen,"
   echo "Images will be saved to the disk. Check for further"
   echo "comments once SUMA stops. "
   echo "See also README file in ./$demo"
   echo "---------------------------------------------------------"
   tcsh ./@CreateMovie >& ./cm.log
   echo "---------------------------------------------------------"
   echo "JPEGs are all under $demo/SUMA_Recordings"
   echo "To view all the frames you can run:"
   echo "   aiv $demo/SUMA_Recordings/*jpg & "
   echo "Press 'v' in the image window to start the movie, space bar to stop."
   echo "********************************************************"
cd -
goto END

PARSE:
   set Narg = $#
   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]" == "-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 runs demo script for making a resting-state movie"
   echo "demo with @CreateMovie."
   echo "After the archive $demo is downloaded and unpacked,"
   echo "see its README.txt and @CreateMovie script."
   goto END

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