#!/bin/sh
: ${SNAP_SRCS:=.}

usage() {
	printf >&2 "Usage: %s: [-S SNAP_SRCS] [-D SNAP_DST] [-] [RSYNC_ARGS]\n" $(basename "$0")
	exit 2
}

die() {
	printf "%s\n" "$*"
	exit 1
}

while getopts S:D: opt; do
	case $opt in
		(S) SNAP_SRCS=$OPTARG;;
		(D) SNAP_DST=$OPTARG;;
		(?) usage;;
		(h) usage;;
	esac
done
shift $(($OPTIND - 1))

case $1 in
	(-) shift;;
	(--) shift;;
esac

if test -z "$SNAP_DST"; then
	echo >&2 "SNAP_DST is required either as env var or argument"
	usage
fi

SNAP_SRCS=${SNAP_SRCS%%/}
SNAP_DST=${SNAP_DST%%/}

export SNAP_DST

snap.missing "$SNAP_SRCS/" "${SNAP_DST%.push}/" | while read ts; do
	snap.push.single -S "$SNAP_SRCS/$ts" - "$@"
done