#! /bin/csh -f
#
#  set NPTCLS and NPTCLF in NUBEAM input namelists
#  set NDEP0 in NUBEAM input namelists
#
#     if 2 arguments, first is the desired number of particles <NNN>;
#          2nd is number of deposition tracks <MMM>
#          (one of these must be >0);  ### = NNN if NNN > 0 else ### = MMM
#
#     the script converts nubeam_init_example.dat -> nubeam_init_<###>.dat
#                     and nubeam_step_example.dat -> nubeam_step_<###>.dat
#
#     e.g. nubeam_init_100000.dat and nubeam_step_100000.dat,
#     if the argument value $1 is 100000.
#
#     if 4 arguments: <NNN> <MMM> <template-filename> <output-filename>
#     convert the named template namelist to the named output namelist

  @ ier = 0 
  if ( $#argv != 2 && $#argv != 4 ) then
    @ ier++
    @ nptcls = 1
    @ ndep0  = 1
  else
    echo 'The message "@: Expression Syntax" means argument is not an integer.'
    @ nptcls = $1
    @ ndep0  = $2
  endif

  set inum = 0
  if ( $nptcls > 0 ) then
    @ inum = $nptcls
  else
    if ( $ndep0 > 0 ) then
      @ inum = $ndep0
    endif
  endif

  if ( $inum == 0 ) then
    @ ier++
    echo " ?? set_nptcls.csh: at least one positive numeric argument expected."
  endif

  if ( $#argv == 4 ) then
    set input_files = ( $3 )
    set output_files = ( $4 )
  else if ( $ier == 0 ) then
    if (-e ../nubeam_init_user.dat) then
       set input_files = ( ../nubeam_init_user.dat nubeam_step_example.dat )
    else
       set input_files = ( nubeam_init_example.dat nubeam_step_example.dat )
    endif
    set output_files = ( nubeam_init_${inum}.dat nubeam_step_${inum}.dat )
  endif

  if ( $ier > 0 ) then
    echo " ?? set_nptcls.csh: error with input arguments.  Expected syntax:"
    echo " ./set_nptcls.csh <nptcls> <ndep0>   ...or... "
    echo " ./set_nptcls.csh <nptcls> <ndep0> <input-namelist-file> <output-namelist-file> "
    echo "   and max(<nptcls>,<ndep0>) > 0 is required."
    exit 1
  endif

  @ j = 0
  while ( $j < $#input_files )
    @ j++
    if ( ! -f $input_files[$j] ) then
      echo " file not found: $input_files[$j]"
      @ ier++
    endif
  end

  if ( $ier > 0 ) then
    echo " ?? set_nptcls.csh: error with input arguments.  Expected syntax:"
    echo " ./set_nptcls.csh <nptcls> <ndep0>   ...or... "
    echo " ./set_nptcls.csh <nptcls> <ndep0> <input-namelist-file> <output-namelist-file> "
    echo "   and max(<nptcls>,<ndep0>) > 0 is required."
    exit 1
  endif

  @ j = 0
  while ( $j < $#input_files )
    @ j++

    set ifile = $input_files[$j]
    set ofile = $output_files[$j]

    if ( $ndep0 <= 0 && $nptcls > 0 ) then
      cat $ifile | \
	  sed "s# nptcls =.*# nptcls = $nptcls#" | \
	  sed "s# nptclf =.*# nptclf = $nptcls#"       > $ofile
    else if ( $nptcls <= 0 && $ndep0 > 0 ) then
      cat $ifile | \
	  sed "s# ndep0 =.*# ndep0 = $ndep0#"       > $ofile
    else
      cat $ifile | \
	  sed "s# nptcls =.*# nptcls = $nptcls#" | \
	  sed "s# nptclf =.*# nptclf = $nptcls#" | \
	  sed "s# ndep0 =.*# ndep0 = $ndep0#"       > $ofile
    endif

  end

  echo " wrote: $output_files "
  echo " set_nptcls.csh: normal exit "
  exit 0


    
