#!/bin/bash
# vim: ts=4 sw=4 ai noet
# $Id$
#
# Copyright (C) 2008, Robert Nelson. Licensed under GPLv2.
# By Robert Nelson.
#

test -n "$DEBUG_EXEC" && set -x

TOOLDIR=/usr/share/vzpkg2

. ${TOOLDIR}/apt-functions

log4 Started $0 $*

test -z `get_ve_var IP_ADDRESS` && NEED_IPADDR=1

read_vzpkg_conf

if test -n "$NEED_IPADDR"; then
	test -n $VZPKG_IPADDR || abort "VZPKG_IPADDR missing from vzpkg.conf"
	test -n $VZPKG_DNS || abort "VZPKG_DNS missing from vzpkg.conf"

	test -n "$VZPKG_DNS" && DNS_ARGS=`echo "$VZPKG_DNS" | sed -e 's/[[:space:]]/ /g' -e 's/,/ --nameserver /g' -e 's/^/--nameserver /'`
	test -n "$VZPKG_SEARCH" && SEARCH_ARGS=`echo "$VZPKG_SEARCH" | sed -e 's/[[:space:]]/ /g' -e 's/,/ --searchdomain /g' -e 's/^/--searchdomain /'`

	$VZCTL set $VEID --ipadd $VZPKG_IPADDR $DNS_ARGS $SEARCH_ARGS --save
fi

$VZCTL exec $VEID apt-get -y update
$VZCTL exec2 $VEID apt-get -y upgrade
rc=$?

if test -n "$NEED_IPADDR"; then
	$VZCTL set $VEID --ipdel $VZPKG_IPADDR --save

	rm -f $VE_ROOT/etc/resolv.conf
fi

exit $rc
