#!/usr/bin/env bash
# Description: Pull AOSS Rooftop AERI AERIoe data from whizdum for a specific day

ENV=/opt/metobs/sparchive
LOGDIR=$ENV/log
SCRIPT_NAME=$(basename $0)
SCRIPT_NAME=${SCRIPT_NAME/.sh/}

DATE=$1
if [ -z "$DATE" ]; then
   DATE=`date +%Y%m%d`
fi
LOCK="${ENV}/lock/${SCRIPT_NAME}.lock"
logfile="${LOGDIR}/${SCRIPT_NAME}.log"

SSH_OPTIONS="-o ConnectTimeout=5"
KEY="/home/metobs/.ssh/maint_key_id_dsa"

# Get environment variables and common functions
source $ENV/scripts/sparchive_env.sh
AERIOE_REMOTE_DIR="/data/aerioe/sparc/sparcaerioe1turnC1.c1"
AERIOE_INCOMING_DIR="${SPARCHIVE_INCOMING_ROOT}/current/aerioe"

(
    flock -x -n 200 || log_info "Script is already running, will not run again."

    if [ ! -d $AERIOE_INCOMING_DIR ]; then
        log_info "Creating incoming directory $AERIOE_INCOMING_DIR..."
        mkdir -p $AERIOE_INCOMING_DIR
    fi

    log_info "$(date +%Y-%m-%dT%H:%M:%S): Running rsync jobs for ${DATE}" >> $logfile
    log_info "Rsync Options: ${RSYNC_OPTIONS}" >> $logfile
    log_info "Rsyncing AOSS AERIoe" >> $logfile
	time rsync -e "ssh -i $KEY $SSH_OPTIONS -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -L --partial --inplace -rlpgoDt --chmod=u=rwX,go=rX -vv --timeout=30 dennyh@whizdum.ssec.wisc.edu:${AERIOE_REMOTE_DIR}/*${DATE}*.{png,cdf} $AERIOE_INCOMING_DIR/ >> $logfile 2>&1
	time rsync -e "ssh -i $KEY $SSH_OPTIONS -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" -L --partial --inplace -rlpgoDt --chmod=u=rwX,go=rX -vv --timeout=30 dennyh@whizdum.ssec.wisc.edu:${AERIOE_REMOTE_DIR}/{latest_skewt,aerioe*last_*}.png $AERIOE_INCOMING_DIR/ >> $logfile 2>&1

    log_info "Done"

) 200>$LOCK
