#!/bin/tcsh -f

@global_parse `basename $0` "$*" ; if ($status) exit 0

if ( -f __._thb_tmp_stdin.rst) \rm -f __._thb_tmp_stdin.rst

goto PARSE
RETURN_PARSE:

if ( ! -d ${bdir} ) mkdir ${bdir}

set fo = ()
foreach f ($ff)
   set fr = $f:r
   sphinx-build -b html -d _build/doctrees/ . ${bdir}/html $f
   set fo = ($fo ${bdir}/html/$fr.html)
end

echo ""
echo "Build results"
set cnt = 1
foreach f ($ff)
   if ( -f $fo[$cnt] ) then
      echo "$ff -> $fo[$cnt]"
      if ( $auto_open ) afni_open -b $fo[$cnt]
   else
      echo "$ff -> :("
   endif
   @ cnt ++
end
echo ""

goto END

PARSE:
	set Narg = $#
	set cnt = 1
   set fromstd = 0
   set bdir = _test_build
   set auto_open = 1
   set ff = ()
   while ($cnt <= $Narg)
      if ("$argv[$cnt]" == "-d" || "$argv[$cnt]" == "-echo") then
         set verb = 1
         set echo
         goto CONTINUE
      endif
     
      if ("$argv[$cnt]" == '-h' || "$argv[$cnt]" == '-help') then
         goto HELP
      endif
            
      if ("$argv[$cnt]" == '-f') then
         if ($cnt >= $Narg) then
            echo "Need file after -f"
            goto BEND
         endif
         @ cnt ++
         set ff = ($ff $argv[$cnt])
         goto CONTINUE
      endif
            
      if ("$argv[$cnt]" == '-bd') then
         if ($cnt >= $Narg) then
            echo "Need directory name after -bd"
            goto BEND
         endif
         @ cnt ++
         set bdir = ($argv[$cnt])
         goto CONTINUE
      endif
      
      if ("$argv[$cnt]" == "-no_open") then
         set auto_open = 0
         goto CONTINUE
      endif      

      if ("$argv[$cnt]" == "-") then
         set fromstd = 1
         goto CONTINUE
      endif      
      
      echo "Error: Option or parameter '$argv[$cnt]' not understood"
      apsearch -popt `basename $0` -word $argv[$cnt]
      goto BEND
      
      CONTINUE:		
		@ cnt ++
	end
   
   if ($#ff == 0 && $fromstd == 0) then
      echo "No files to process"
      goto BEND
   endif
   if ($fromstd == 1) then
      set noglob
      set nblank = 0
      while ($nblank < 50)
         set mm = ($< )
         if ("$mm" == "") then
            @ nblank ++
         else 
            set nblank = 0
         endif
         echo "$mm" >> __._thb_tmp_stdin.rst
      end
      unset noglob
      set ff = ($ff __._thb_tmp_stdin.rst)
   endif
   goto RETURN_PARSE

HELP:
   echo ""
   echo "A script to build one or more .rst files. This is mostly"
   echo "for testing automatically generated output from help strings."
   echo ""
   echo "For example, to test whether the output of 3dToyProg -h_aspx"
   echo "is OK, you can do:"
   echo "   3dToyProg -h_aspx > test.rst"
   echo "   tcsh @test_html_build -f test.rst"
   echo "   afni_open -b _build/html/test.html"
   echo ""
   
   echo "Otherwise, to build everything you just need to run:"
   echo "" 
   echo "      make html"
   echo "or"
   echo "      make clean html"
   echo ""
   echo "To regenerate all the auto-docs in addition to a rebuild,"
   echo "see @gen_all"
   echo ""
   echo "Options:"
   echo "-h: This message"
   echo "-echo: Yap yap"
   echo "-no_open: Don't open browser after build"
   echo "-bd BUILD_DIR: Set the build directory to BUILD_DIR"
   echo "               Default is: $bdir"
   echo "- : Expect input from stdin. Stop reading when 50 blanks"
   echo "    are encountered in a row."
   echo ""
   goto END

BEND:
   echo "Berror!"
   goto END

END:
   if ( -f __._thb_tmp_stdin.rst) \rm -f __._thb_tmp_stdin.rst
