#! /bin/bash

# Copyright (C) 2016, Ansgar Burchardt <ansgar@debian.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

set -e
set -u
set -E

export LANG=C
export LC_ALL=C

export SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
. $SCRIPTVARS

usage() {
  echo "usage: $0 <suite>"
  echo
  echo "sync dists/<suite> to a location accessible by the release team"
  echo "excluding irrelevant bits (such as installer images)"
  exit ${1:-0}
}

if [ $# -ne 1 ]; then
  usage
fi

suite="${1}"
archive_path=$(psql -qAtc "SELECT path FROM archive WHERE id = (SELECT archive_id FROM suite WHERE suite_name='${suite}')")
target="sync-release:/srv/ftp-master.debian.org/tmp-release/"

rsync -avH --progress -B8192 \
  --relative \
  --exclude "/dists/${suite}/main/installer-*" \
  --timeout 3600 \
  -e 'ssh -o ConnectTimeout=30 -o SetupTimeout=30' \
  "${archive_path:?}/./dists/${suite}/" "${archive_path:?}/./zzz-dists/${suite}/" "${target}"

# command for the remote side:
# rsync --server -lHogDtprRe.iLsfx -B8192 --timeout=3600 . /srv/ftp-master.debian.org/tmp-release/
