#! /bin/bash
#
# Copyright 2012, 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

set -e
set -u

usage() {
  echo "usage: $0 <source> <target>"
  echo
  echo "Update a minimalistic mirror for buildd archives."
  exit ${1:-0}
}

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

source="${1}"
dest="${2}"

if [ ! -d "${source}/dists" -o ! -d "${source}/pool" ]; then
  echo "${source}: does not look like a Debian archive" >&2
  exit 1
fi

if [ ! -d "${dest}" ]; then
  echo "${dest}: destination does not exist or is not a directory" >&2
  exit 1
fi

# Make sure ${dest}/pool exists
if [ ! -e "${dest}/pool" ]; then
  # Files are only removed from the build queues once they are no longer
  # referenced. Having a symlink should thus not cause problems.
  ln -s "${source}/pool" "${dest}/pool"
fi

for subdir in dists zzz-dists project/external-signatures; do
  if [ ! -d "${source}/${subdir}" ]; then
    continue
  fi

  # Make sure ${dest}/${subdir} exists to avoid a special case later
  if [ ! -d "${dest}/${subdir}" ]; then
    mkdir -p "${dest}/${subdir}"
  fi
  for olddir in ${subdir}.new ${subdir}.old; do
    if [ -e "${dest}/${olddir}" ]; then
      echo "Removing old ${olddir}..."
      rm -r "${dest}/${olddir}"
    fi
  done

  # Finally copy ${subdir}/ to ${subdir}.new/, rename it and remove old version
  cp -a "${source}/${subdir}" "${dest}/${subdir}.new"
  mv "${dest}/${subdir}" "${dest}/${subdir}.old"
  mv "${dest}/${subdir}.new" "${dest}/${subdir}"
  rm -r "${dest}/${subdir}.old"
done
