#!/bin/tcsh -f

set stat = 0
set sdir = $PWD


goto PARSE
RET_PARSE:

if ( -d ./Suma_TSrestMovieDemo/SUMA_Recordings) then
echo ""
echo "ERROR:"
echo "   Directory ./Suma_TSrestMovieDemo/SUMA_Recordings exists already"
echo "   If you want to recreate movie frames, remove it with:"
echo "\rm -rf ./Suma_TSrestMovieDemo/SUMA_Recordings"
echo "   then run `basename $0` $argv[*] again."
echo ""
echo "Otherwise to just view the images run:"
echo "   aiv Suma_TSrestMovieDemo/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 http://afni.nimh.nih.gov/pub/dist/tgz/Suma_TSrestMovieDemo.tgz
else
   curl -O http://afni.nimh.nih.gov/pub/dist/tgz/Suma_TSrestMovieDemo.tgz
endif
tar xvzf SUMA_TSrestMovieDemo.tgz
cd Suma_TSrestMovieDemo
   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 ./Suma_TSrestMovieDemo"
   echo "---------------------------------------------------------"
   tcsh ./@CreateMovie >& ./cm.log
   echo "---------------------------------------------------------"
   echo "JPEGs are all under Suma_TSrestMovieDemo/SUMA_Recordings"
   echo "To view all the frames you can run:"
   echo "   aiv Suma_TSrestMovieDemo/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 is downloaded and unpacked, see its README.txt"
   echo "and @CreateMovie script."
   goto END

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