#!/bin/bash

DIRECTORY="/data0/FotoboxSync/0-CURRENT/"

cd "$(dirname "$0")" || exit 1

while true; do

    list="$(ls images/0-CURRENT/upload/* 2>/dev/null)"
    
    if [ "$list" != "" ]; then
        ssh media@10.0.0.50 mkdir -p $DIRECTORY
        
        count=$(echo "$list" | wc -l)
        echo "found $count files"

        sync
        sleep 3
        sync
    
        echo "$list" | while read file; do
        
            echo "Uploading:"
            echo "$file"
            
            scp "$file" media@10.0.0.50:"$DIRECTORY"
            success="$?"
            
            if [ "$success" == "0" ]; then
                echo "OK"
                rm -f "$file"
            else
                echo "FAIL"
            fi
            
            echo ""
        
            sleep 1
        
        done
        
    fi
    
    sleep 60
    
done
