#!/bin/sh
exec 1>&2
env | grep RSYNC | sort | tr '\n' '\t'; echo

set -x

case $RSYNC_MODULE_NAME in
	(*.push) ;;
	(*) exit 1;;
esac

for m in $RSYNC_MODULE_PATH/.snapshot.[0-9]*; do
	test -f "$m" || continue
	test -n "$mark" && exit 1
	mark=$m
done

test -f "$mark" || exit 1

snap_dir=${RSYNC_MODULE_PATH%/*}

if test -f "$snap_dir/${mark##*/}"; then
	# already have this snapshot
	rm -f "$mark"
	exit $?
fi

ts=${mark##*.}
rsync -aA --exclude=/.snapshot.\* --link-dest="$RSYNC_MODULE_PATH/" "$RSYNC_MODULE_PATH/" "$snap_dir/$ts/" || exit $?
touch "$snap_dir/$ts/${mark##*/}" || exit $?
mv "$mark" "$snap_dir/" || exit $?