#!/bin/sh
# $NetBSD: upload,v 1.4 2014/02/22 00:35:41 jperkin Exp $
#
# Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>.
# All rights reserved.
#
# This code was developed as part of Google's Summer of Code 2007 program.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

. ${PBULK_CONF:-/opt/tools/etc/pbulk.conf}

set -e

if [ "${config_version}" != "0.68" ]; then
	echo "Your configuration has version ${config_version}."
	echo "This version of pbulk expects version 0.68."
	exit 1
fi

#
# Run optional check script.  This allows ensuring certain criteria are met
# before continuing with the upload process, for example to avoid publishing
# a new package set with critical packages missing.  PBULK_CONF is passed in
# the environment so that the script can source it for useful variables.
#
if [ -x "${script_phase_pre_upload}" ]; then
	echo "Running pre-upload check script..."
	PBULK_CONF=${PBULK_CONF:-/opt/tools/etc/pbulk.conf} ${script_phase_pre_upload}
fi

echo "Uploading packages..."
cd ${packages}
{
	[ "${checksum_packages}" != "no" ] && \
	    [ "${checksum_packages}" != "NO" ] && \
	    echo "+ SHA512.bz2"
	echo "+ All/"
	echo "+ All/pkg_files.bz2"
	echo "+ All/pkg_files.gz"
	echo "+ All/pkg_files.xz"
	echo "+ All/pkg_summary.bz2"
	echo "+ All/pkg_summary.gz"
	echo "+ All/pkg_summary.xz"
	${packages_script} ${loc} ${pkg_sufx}
	echo "- *"
} | LC_ALL=C sort >/tmp/rsync-exclude-$$

# Joyent specific.  Additional pbulk.conf variables provide host + directory so
# that we can insert the rsync --link-dest argument and ensure atomic directory
# updates.  Requires pkg_rsync_target_linkdest and pkg_rsync_target_tmpdest to
# reside on the same file system for hardlinks.
if [ -z "${pkg_rsync_target_host}" -o \
     -z "${pkg_rsync_target_linkdest}" -o \
     -z "${pkg_rsync_target_tmpdest}" ]; then
	# Legacy sync, just performed in-place directly to target.
	${rsync} --exclude-from=/tmp/rsync-exclude-$$ ${pkg_rsync_args} \
	    --partial-dir=.rsync-partial . ${pkg_rsync_target}
else
	# Use link-dest to create/update to a shadow directory.  The tmpdest
	# argument requires the usual parent directories to exist first.  If
	# link-dest doesn't exist (e.g. an initial sync) it is not an error.
	${rsync} --exclude-from=/tmp/rsync-exclude-$$ ${pkg_rsync_args} \
	    --partial-dir=.rsync-partial --link-dest=${pkg_rsync_target_linkdest} \
	    . ${pkg_rsync_target_host}:${pkg_rsync_target_tmpdest}

	# Move any existing linkdest out of the way, promote the new tmpdest,
	# and remove the previous linkdest.  We move it to a directory based
	# on tmpdest as that is usually a hidden directory, avoiding directory
	# listings showing the old directory while it is being deleted.
	ssh ${pkg_rsync_target_host} "
		if [ -f ${pkg_rsync_target_tmpdest}/All/pkg_summary.gz ]; then
			if [ -d ${pkg_rsync_target_linkdest} ]; then
				mv ${pkg_rsync_target_linkdest} ${pkg_rsync_target_tmpdest}-old
			else
				mkdir -p $(dirname ${pkg_rsync_target_linkdest})
			fi
			mv ${pkg_rsync_target_tmpdest} ${pkg_rsync_target_linkdest}
			rm -rf ${pkg_rsync_target_tmpdest}-old
		fi
	"
fi

rm -f /tmp/rsync-exclude-$$
