#!/usr/bin/env bash
#/ Usage: ghe-rsync
#/ Run rsync with silenced vanished file warnings (non-critical).
#
# Based on the rsync-no-vanished support script included with rsync:
# https://bugzilla.samba.org/show_bug.cgi?id=10356

set -o pipefail

# Filter vanished file warnings from both stdout (rsync versions < 3.x) and
# stderr (rsync versions >= 3.x). The complex redirections are necessary to
# filter stderr while also keeping stdout and stderr separated.
IGNOREOUT='^(file has vanished: |rsync warning: some files vanished before they could be transferred)'
(rsync "${@}" 3>&1 1>&2 2>&3 3>&- |
  (egrep -v "$IGNOREOUT" || true)) 3>&1 1>&2 2>&3 3>&- |
  (egrep -v "$IGNOREOUT" || true)
res=$?

# rsync exits with 24 when vanished files are detected.
if [ $res = 24 ]; then
    res=0
fi

exit $res
