#!/bin/csh
#$Id$
#
#  This script adds a new run to the repository
#  and checks in all *.in files as well as material
#  from the data and src directories.
#
#  if this procedure is called with an argument,
#  then first go into this directory and continue from there
#
#  When invoked as tar_run, a corresponding tarball is created
#  as <working directory>.tar in the parent directory.
#  Tested for only one argument.
#
if ( $#argv >= 1 ) then
  set dirlist=($argv)
else
  set dirlist=$cwd
endif

if ( $0:t == 'tar_run' ) then
  set cvsci=0
  set add_cmd="tar -f $cwd.tar --ignore-failed-read --append"
  set add_data_opt=(--transform 's,^,data/,')
  set add_cmd_bin="$add_cmd"
  tar cf $cwd.tar run.in
  tar --delete --file=$cwd.tar run.in
else
  set cvsci=1
  set add_cmd="cvs add"
  set add_cmd_bin="$add_cmd -kb"
  set add_data_opt=""
endif

foreach pdir ($dirlist)
  cd $pdir
#
#  set directory
#
  set CWD=$cwd
  set dir=$cwd:t
#
#  go one up and add our run directory to cvs
#
if ( $cvsci ) then
  cd ..
  $add_cmd $dir
  cd $dir
endif
#
#  add directories and files to cvs
#  also get a copy of their time stamps before checking in
#
  ls -l *.in *.dat *.pro src/*.local src/*.txt pc_commands.log > .log

  $add_cmd *.in alpha_in.dat k.dat k_double.dat kvect.dat higgsless.dat a_vs_eta.dat power_profile.dat pulsar.dat parameters.pro testfield_info.dat chemistry*.dat chem.inp tran.dat air.dat stratification.dat nnu2.dat chiral_list*.dat Iv*.dat correl*.dat particles_initial.dat particles_initial_header.pro pc_commands.log .log job.sh >& /dev/null

#
  if (-e data) then

      if ( $0:t == 'cvsci_run' ) then
        $add_cmd data
      endif

      cd data
      $add_cmd $add_data_opt index.pro legend.dat params.log *.nml cvsid.dat varname.dat runtime.dat pc_constants.pro jobid.dat svnid.dat pdim.dat pt_positions.dat tstalk.dat particles_stalker_header.dat >& /dev/null
      if ( -e dim.dat ) then
          $add_cmd $add_data_opt dim.dat
      endif
      if ( -e time_series.dat ) then
          $add_cmd $add_data_opt time_series.dat
      endif
      if ( -e time_series.h5 ) then
          $add_cmd_bin $add_data_opt time_series.h5
      endif
      if ( -e grid.h5 ) then
          $add_cmd_bin $add_data_opt grid.h5
      endif
  else
      echo "no data directory found"
  endif
#
#  add alpeta.sav, if it exists
#
  if ( -e alpeta.sav ) then
    $add_cmd $add_data_opt alpeta.sav
  endif
#
#  add power spectra, if they exist
#  (for the time being, test existence only on a few such files)
#
  ls | egrep 'power.*\.dat' >/dev/null
  if ( ! $status ) then
    $add_cmd $add_data_opt *power*.dat
  endif
#
#  add Saffman invariants, if they exist
#  (for the time being, test existence only on a few such files)
#
  ls | egrep 'Iv.*\.dat' >/dev/null
  if ( ! $status ) then
    $add_cmd $add_data_opt Iv*.dat
  endif
#
#  add structure functions, if they exist
#  (for the time being, test existence only on a few such files)
#
  ls | egrep 'sf.*\.dat' >/dev/null
  if ( ! $status ) then
    $add_cmd $add_data_opt sf*.dat
  endif
#
#  add testfield_info.dat file if it exists
#
  if ( -e testfield_info.dat ) then
    $add_cmd $add_data_opt testfield_info.dat
  endif
#
#  add info about directory history
#
  if (-e new_from.dir) then
      $add_cmd $add_data_opt new_from.dir
  endif
#
  if (-e new_to.dir) then
      $add_cmd $add_data_opt new_to.dir
  endif
#
  if (-e remeshed_from.dir) then
      $add_cmd $add_data_opt remeshed_from.dir
  endif
#
#  check in everything
#  need to take full path name, because we are still in data,
#  which may only be a link.
#
  cd $CWD
  
  if ( $cvsci ) then
    cvs ci -l -m "added new run: `uname -n -s`" . data
#
#  add src stuff only if it is not a link
#  Do this at the very end, in case this step fails.
#
    test -h src
    if ( $status ) then
      $add_cmd src
      cd src
      $add_cmd *.local *.txt .build-history .moduleinfo
      cd ..
      if ( $0:t == 'cvsci_run' ) then
        cvs ci -m "added new run: `uname -n -s`" src
      endif
    endif
  else
    $add_cmd -h src/*.local src/*.txt src/.build-history >& /dev/null
  endif

  cd ..
end