#!/bin/bash

export LC_ALL=C 
CONFIG='/etc/repopusher/repopusher.conf'

if [ -r "$CONFIG" ]; then
    . "$CONFIG"
else
	echo "ERROR: $CONFIG not found or not readable" >&2
	exit 1
fi

cd "$arrived" || { echo "ERROR: Could not chdir to $arrived"; exit 1; }

for j in ${servers}*.conf; do
        server="${j##*/}"
		server="$(basename $server .conf)";
        for i in $(ls -tr | grep -v '^home:') $(ls -tr | grep '^home:'); do
                if ! [ -d "$repodata${i//:/:/}" ]; then
                        echo "skipping for non-existing dir $repodata${i//:/:/}"
                        echo "removing arrived/$i"
                        rm -v $i
                        continue
                fi
                push_file="${pushed}$i.$server"
                if [ "$i" -nt "$push_file" ]; then
                        echo $server: push needed for $i
                fi
        done
done
