#!/bin/bash
#
# vesync - Sync metadata and overlays in vservers
# Copyright (C) 2005 Christian Heim <phreak@gentoo.org>
#                    Benedikt Boehm <hollow@gentoo.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
#
# v0.1 - initial release

trap "exit 1" INT

if [ -r /sbin/functions.sh ]; then
	source /sbin/functions.sh
else
	echo "/sbin/functions.sh missing. Are you running Gentoo?"
fi

die() {
	eerror ${1:-<no error message>}
	einfo "Type ${APP} --help for details"
	
	exit ${2:-1}
}

syncportage() {
# $1 - name of the vserver
	ebegin "Running 'emerge --metadata' inside of vserver '${1}'"
	/usr/sbin/vserver ${1} exec /usr/bin/emerge --metadata >&9
	eend $? || die
}

syncoverlay() {
# $1 - name of the vserver
# $2 - overlayhost (e.g. rsync://rsync.de.gentoo.org/gentoo-overlay)
# $3 - overlay directory (e.g. /usr/local/portage)
	ebegin "Syncing overlay '${3}' inside of vserver '${1}'"
	/usr/sbin/vserver ${1} exec /usr/bin/rsync -rtW --progress --delete \
		--delete-after ${2}/ ${3}/ >&9
	eend $? || die
}

usage() {
	echo "Usage: vesync <opts> [<name>]"
	echo
	echo "<name>  Name of the vserver (required if --all not used)"
	echo
	echo "Options:"
	echo " -h, --help                 This help message"
	echo " -q, --quiet                Don't show output of emerge"
	echo " -a, --all                  Sync all running vservers"
	echo "     --overlay              Directory of a portage overlay"
	echo "     --overlay-host         Rsync host of the overlay"
	echo "     --only-overlay         Only sync the overlay"
	echo
}

# Parsing opts
opts=$(POSIXLY_CORRECT=1 getopt -o hqa --longoptions help,quiet,all,overlay,overlay-host,overlay-only -n $0 -- "$@")

[ "$?" != "0" ] && die "Wrong number of options"

eval set -- "$opts"

quiet=0
all=0
overlay=
overlay_host=
overlay_only=0

while true; do
	case "${1}" in
		--help|-h)
			usage
			exit 0
			;;
		--quiet|-q)
			quiet=1
			shift
			;;
		--all|-a)
			all=1
			shift
			;;
		--overlay)
			overlay="${2%/}"
			shift 2
			;;
		--overlay-host)
			overlay_host="${2%/}"
			shift 2
			;;
		--overlay-only)
			overlay_only=1
			shift
			;;
		*)
			die "Unknown argument '${1}'"
			;;
	esac
done

# checking quiet option
if [ ${quiet} -eq 0 ]; then
	exec 9>&1
else
	exec 9>/dev/null 2>&1
fi

# checking vserver name
name=
if [ ${all} -eq 0 ]; then
	if [ -z "${1}" ]; then
		die "Missing argument <name>"
	else
		name=$1
	fi
fi
shift

# get the list of vservers
if [ ${all} -eq 0 ]; then
	vservers=${name}
else
	running="$(find /proc/virtual/ -mindepth 1 -maxdepth 1 -type d)"
	for xid in ${running}; do
		vservers="${vservers} $(/usr/sbin/vserver-stat | /bin/grep "^${xid##*/}" | /bin/awk '{ print $8 }')"
	done
fi

# finally, sync it!
for vserver in ${vservers}; do
	[ ${overlay_only} -eq 0 ] && syncportage ${vserver}
	
	if [ -n "${overlay_host}" ] && [ -n "${overlay}" ]; then
		syncoverlay ${vserver} ${overlay_host} ${overlay}
	fi
done
