#!/bin/bash
#
# vserver-new - Create a new virtual server
# Copyright (C) 2005 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

trap "exit 1" INT

APPNAME="${0##*/}"
VERSION="0.1"

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}
}

updateworld() {
# $1 - name of the vserver
	if [ ${pretend} -eq 0 ]; then
		ebegin "Running 'emerge world' inside of vserver '${1}'"
		/usr/sbin/vserver ${1} exec /usr/bin/emerge world -Du --newuse --nospinner >&9
		eend $? || die
	else
		einfo "Pretending 'emerge world' inside of vserver '${1}'"
		/usr/sbin/vserver ${1} exec /usr/bin/emerge world -Dupq --newuse --nospinner | egrep '^\[ebuild' >&9
		echo
	fi
	
}

usage() {
	echo "Usage: vupdateworld <opts> [<name>]"
	echo
	echo "<name>       Name of the vserver (required if --all not used)"
	echo "<emergeopts> Additional arguments passed to the emerge command"
	echo
	echo "Options:"
	echo " -h, --help                 This help message"
	echo " -V, --version              Show the version"
	echo " -q, --quiet                Don't show output of emerge"
	echo " -a, --all                  Sync all running vservers"
	echo " -p, --pretend              Display  what *would* have been installed"
	echo
}

# Parsing opts
opts=$(POSIXLY_CORRECT=1 getopt -o hVqap --longoptions help,version,quiet,all,pretend -n $0 -- "$@")

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

eval set -- "$opts"

quiet=0
all=0
pretend=0

while true; do
	case "${1}" in
		--help|-h)
			usage
			exit 0
			;;
		--version|-V)
			echo "${APPNAME} version ${VERSION}"
			exit 0
			;;
		--quiet|-q)
			quiet=1
			shift
			;;
		--all|-a)
			all=1
			shift
			;;
		--pretend|-p)
			pretend=1
			shift
			;;
		--)
			shift
			break
			;;
		*)
			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 [ -z "$1" ] && [ ${all} -eq 0 ]; then
	die "Missing argument <name>"
else
	name=$1
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
	updateworld ${vserver}
done

# vim:ai:noet:ts=4:nowrap
