#! /bin/csh -f
#
# run the python generator "nbigen"
# this regenerates shared NUBEAM data modules.
#
# this script takes 1 optional argument:
#   "test" or "debug" -- write Python script output to $TMPDIR_TR/nbigen
#   "difftest" -- show what generator output files are changed, but do
#                 not install the changes.
#
# the default is to run the generator, and, if it exits with a normal
# status codes, copy modified sources to $CODESYSDIR/source/nubeam.
#
  if ( -d $TMPDIR_TR/nbigen ) then
    rm -r $TMPDIR_TR/nbigen
  endif
  mkdir $TMPDIR_TR/nbigen
  cd $TMPDIR_TR/nbigen

  cp $CODESYSDIR/source/nubeam/nbspec.dat .
  cp $CODESYSDIR/source/nubeam/nbi_state_user.f90 .
  cp $CODESYSDIR/source/nubeam/*.py .

# lists of codes with both hand-maintained and Python-generated sections...

  cp $CODESYSDIR/source/doc/nubeam.hlp ./nubeam_hlp.tmp

  python3 nbigen.py
  if ( $status != 0 ) then
    echo " python error -- work directory is $TMPDIR_TR/nbigen "
    exit 1
  endif

  set difftest = "FALSE"
  if ( $#argv > 0 ) then
    if ( $1 == "test" || $1 == "debug" ) then
      echo " test complete in $TMPDIR_TR/nbigen "
      exit 1
    endif
    if ( $1 == "difftest" ) then
      echo " nbigen.csh difftest -- generated files not to be copied."
      set difftest = "TRUE"
    else
      echo " nbigen.csh -- unrecognized argument(s): $argv "
      exit 1
    endif
  endif

  rm -f *.pyc
  rm -f nbspec.dat *.py
  rm -f *.tmp

  @ icount = 0
  @ ichange = 0

  foreach file ( `ls` )
    @ icount++
    if ( -f $CODESYSDIR/source/doc/$file ) then
      cmp -s $file $CODESYSDIR/source/doc/$file
      if ( $status != 0 ) then
        @ ichange++
        if ( $difftest == "FALSE" ) then
          echo " nbigen.csh:  updated:  $file"
          cp $file $CODESYSDIR/source/doc/$file
        else
	  echo " nbigen.csh difftest: changes detected:  $file"
	endif
      endif
    else
      cmp -s $file $CODESYSDIR/source/nubeam/$file
      if ( $status != 0 ) then
        @ ichange++
        if ( $difftest == "FALSE" ) then
          echo " nbigen.csh:  updated:  $file"
          cp $file $CODESYSDIR/source/nubeam/$file
        else
	  echo " nbigen.csh difftest: changes detected:  $file"
	endif
      endif
    endif
  end

  echo " nbigen.csh: modifications in $ichange elements out of $icount possible."
