#!/bin/sh

# recoverid: argument 1 is the eventid of interest.
#            argument 2 is the target host (current MASTER system)

# This script is to aid in recovering events identified at a remote
# node when network communications has been broken between the node
# and the main processing center.

# Set EVENT_DIR to the directory where event messages are being
# automatically stashed away.
#
# Set OUTDIR_BASE to the base name of the directories being polled
# by various sendfileII modules for shipping recovered data to
# machines back in Menlo Park. The target directory will be 
# generated by tacking target-host (arg2) on the end of OUTDIR_BASE.

EVENT_DIR=/home/earthworm/run/arc
OUTDIR_BASE=/home/earthworm/run/send2

# Make sure all arguments are valid!
# If either arg is NULL or 
# if arg2 not a valid taget-host, give usage hints:

if (test -z "$1") || (test -z "$2") ||
   ((test "$2" != "ew03") && (test "$2" != "ew04" ))
then
   echo "  Usage: recoverid <eventid> <target-host>"
   echo
   echo "  where: <eventid>     = id of the event to recover"
   echo "         <target-host> = ew03 or ew04 (current MASTER system)"
   echo " "
   echo "Example: recoverid 6110543 ew04"
   exit
fi

# Go to event directory and work from there:

cd $EVENT_DIR
FILE_LIST=`grep -l -s "$1" *`

echo "Events in the following data file(s) will be sent to $2:"
echo "$FILE_LIST"
echo "Continue? y/n"
read answer

if test "$answer" != "y" 
then 
  echo "Aborting on request..."
  exit
fi

# Make a copy of each data file in temporary workspace,
# then move it into the target directory.
# The extra step is required to ensure that sendfileII gets a 
# complete file to ship to getfileII.

TARGET=$OUTDIR_BASE$2

for FILE in $FILE_LIST; do
  cp $FILE /tmp/$FILE
  mv /tmp/$FILE $TARGET
done

echo "File copy complete!"


