#!/bin/bash
# $Id: tagcopy.sh,v 1.11 2003/11/24 15:45:42 hoeth Exp $
#

# I want to run over all the tagfile I find.
echo "Searching in $RERECO_RESULT_DIRS ."
for tagfile in `find $RERECO_RESULT_DIRS -name "$RERECO_TAGFILE_PATTERN"`
do \
  echo "Processing tagfile: $tagfile"
  tagbase=`basename $tagfile .tagfile`
  #tagrunnumber=`echo $tagbase | cut -d _ -f 4`
  failed=0
    
  # Test if we already processed this file
  cd `dirname $tagfile`
  if test -f $RERECO_SUCCESS_TAGFILE_DIR/$tagbase.was.fully.copied
  then
    echo "tagfile $tagfile was already copied"
    failed=1
  fi

  # Test if the right number of arguments is in the tagfile
  argcount=`cat $tagfile | wc -w`
  if test $argcount -lt 6 
  then \
    echo "Wrong number of arguments in $tagfile"
    failed=2
  fi
  if [ $failed -eq 0 ] ; then
    # Test if the files specified in the tagfile exist.
    for ((i=1 ; $i<=6 ; i++))
    do \
      sourcename=`cut -d " " -f $i $tagfile`
      if test -f $sourcename
      then
         echo "source $sourcename exists. That's good"
      else
        echo "source $sourcename does not exist. That's BAD!"
        failed=3
      fi
    done
  fi

  if [ $failed -eq 0 ] ; then
    # Can I write the status-file?
    touch $RERECO_SUCCESS_TAGFILE_DIR/$tagbase.is.being.copied.now || failed=4
  fi
  # Do the copy.
  if [ $failed -eq 0 ] ; then
    source $1
    if [ $failed -eq 0 ] ; then
      mv $RERECO_SUCCESS_TAGFILE_DIR/$tagbase.is.being.copied.now $RERECO_SUCCESS_TAGFILE_DIR/$tagbase.was.fully.copied
    else
      rm $RERECO_SUCCESS_TAGFILE_DIR/$tagbase.is.being.copied.now
    fi
  else
    if [ $failed -gt 1 ] ; then
      echo "ERROR: Skipping $tagbase !!!!"
    fi
  fi
done

