#! /bin/sh

# Copyright 2008-2014 Cumulus Systems Incorporated.
# All Rights Reserved.

# Clean up files already sent.
# Delete files present in data directory with file name prefix new.* and which are not modified in last 600 minutes.
# Call rt_send script to send files which are need to resend.

export PATH=".:$DATATOP/bin:/usr/bin:/bin:$PATH";

# Installation directory path.
DATATOP=$1

# Check is installation directory exist. If not then quit.
test -d $DATATOP || exit 0

# Change directory to installation directory.
cd $DATATOP || exit 0

# Delete all the files from data/sent directory which are not modified in last 60 minutes.
if test -d ./data/sent ; then
  find ./data/sent -type f -mmin +60 -exec rm -f {} \;
fi


# Delete all the files with prefix new.* and which are not modified in last 600 minutes.
if test -d ./data ; then
  find ./data -maxdepth 1 -type f -name 'new.*' -mmin +600 -exec rm -f {} \;
fi

# Moves files which are not modified in last 60 minutes and names not started with 'new or lck', to retry/ directory for retries..
mkdir -p ./data/retry
if test -d ./data ; then
  # find ./data -maxdepth 1 -type f ! -name 'new.*|lck.rt_send' -mmin +60 -exec mv -f {} ./data/retry/. \;
  for i in `find ./data -maxdepth 1 -type f | grep -v "^./data/new.*$" | grep -v "^./data/lck.*$"`; do mv -f $i ./data/retry/. ; done
fi

# Run the retry server for sending files present under directory retry.
./bin/sendRawData.pl -y -d $DATATOP -q ./data/retry > ./logs/tmp/linuxProbe.dump
