#!/bin/bash

# Source conf file with hack to remove sections
# (section is used by python AGI xivocc-determinate... using ConfigParser)
source <(grep -vE '\[.*\]' /etc/xivocc-recording.conf)

PATH=$PATH:/usr/bin

function log {
    date=`date +'%Y-%m-%d %H:%M:%S'`
    echo "$date $1" >> $LOG_FILE
}

REGEX="([^-]+)-.+"

log "Copying files from $FAILED_DIR to the recording server"

for file in $(find $FAILED_DIR -name '*.wav')
do
    realfile=`basename $file`
    [[ $realfile =~ $REGEX ]]
    folder="${BASH_REMATCH[1]}"
    export RSYNC_PASSWORD=$PASSWORD
    rsync --bwlimit=200 --remove-source-files --timeout=3 -z $FAILED_DIR/$realfile rsync://$USERNAME@$RECORDING_SERVER/recording/$folder/
    if [ $? -ne 0 ]
    then
        log "Error copying the file $realfile from $FAILED_DIR"
    fi
done

