#!/bin/bash
#
# rsync_cips_to_hampton_driver.sh
#
# Bill Barrett June 2019
#
# A script to use rsync to push AIM CIPS data to Hampton University

# The directory where the log files will be written
LOG_FILE_DIR=/aim/logs/cips/rsync

# The individuals who will receive the email that this script sends out
EMAIL_RECIPIENTS="adrian.gehr@lasp.colorado.edu,james.craft@lasp.colorado.edu"
#EMAIL_RECIPIENTS="adrian.gehr@lasp.colorado.edu"

# Create (or update) a log file using the current date
date_string=`date "+%Y_%m_%d"`
mkdir -p $LOG_FILE_DIR
logfile_name="${LOG_FILE_DIR}/hampton_rsync-${date_string}.log"
touch $logfile_name

# Is the script already running? During large data transfers cron may try to start a new job
#  before the previous one has finished.
this_program=`echo "$0" | awk -F "/" '{print $NF}'`
script_count=`ps a | grep "$this_program" | grep -v grep | wc -l`
# Anything more than this instance is too much.
# Although bash frequently reports two instances when there are one
#if [[ ! $script_count =~ 0|1|2 ]]; then
#  date_string=`date "+%Y-%m-%d %H:%M"`
#  echo "$date_string Exiting because $script_count copies of $this_program are already running" | \
#    tee -ia $logfile_name
#  echo "$date_string $script_count" | tee -ia $logfile_name
#  exit 0
#fi

# Do the actual data transfer
export AIMPI_HOME=/export/home/aimpi
export PYTHONPATH=/aim/sds/cips/scripts:/aim/sds/common/scripts
export PYTHONPATH=$PYTHONPATH:$AIMPI_HOME/src/sqlite_utils:$AIMPI_HOME/src/utils
/aim/sds/common/scripts/rsync_cips_to_hampton.py 2>&1 | tee -ia $logfile_name

# Annoyingly rsync from cron sometimes copies files from the cron directory
# Delete the artifacts
ssh aim_cips@aim.hamptonu.edu \
  'find /mnt/home/aim_cips/interim_archive/cips/data/ -name "*_backup*" -exec rm -v {} +'

# Send out an email when the script has completed
# The grep pulls out the lines from the logfile that tell when rync starts for a given directory,
#   what the byte transfer count is, and what errors have occurred. Because some of these lines
#   are double printed, 'uniq' is used leave only one copy of each line.
grep -P "(bytes|error|rsync|continuing|failed)" ${logfile_name} | uniq | \
  mail -s "rsync_cips_to_hampton.sh complete"  $EMAIL_RECIPIENTS
exit 0

