#!/bin/bash

# Source conf file with hack to remove sections
# (section is used by python AGI xivocc-determinate... using ConfigParser)
source <(grep -vE '\[.*\]' /etc/vinci-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 '*.mp3')
do
    realfile=`basename $file`
    [[ $realfile =~ $REGEX ]]
    folder="${BASH_REMATCH[1]}"
    export RSYNC_PASSWORD=$PASSWORD
    rsync --remove-source-files  $FAILED_DIR/$realfile /var/tmp/recording_test
    if [ $? -ne 0 ]
    then
        log "Error copying the file $realfile from $FAILED_DIR"
    fi
done

