#!/bin/bash
# $Id: vzrpm,v 1.9 2005/10/26 11:34:56 kir Exp $
#
# vzrpm - a wrapper for rpm to use rpm for OpenVZ VPS.
#
# Copyright (C) 2005, SWsoft. Licensed under QPL.
# By Kir Kolyshkin.

TOOLDIR=/usr/share/vzpkg
#DEBUG_LEVEL=4

. ${TOOLDIR}/functions

log4 Started $0 $*

set -e

function usage() {
	cat << USAGE_EOF 1>&2
Usage: $PROGNAME <VPSID> <arguments>
  where <arguments> are yum arguments.
USAGE_EOF
	exit $1
}

if test $# -lt 2; then
        log2 "Wrong number of parameters specified"
        usage
fi

get_veid $1
shift
USER_ARGS=$*
# Sanity checks
STATUS=`$VZCTL status $VEID` || abort "Can't get status for VPS $VEID: " \
	"vzctl status failed with code $?"
echo $STATUS | grep -qw "exist" || abort "VPS $VEID not exist!"
echo $STATUS | grep -qw "running" || abort "VPS $VEID not running; " \
	"you should start it first"
RPM_ARGS="--root $VE_ROOT --veid $VEID"
# Find out which RPM binary to use
get_ve_os_template $VEID || abort "Can't get OSTEMPLATE for VPS $VEID"
TEMPLATE=`get_vz_var TEMPLATE`
OST=`ost2full $OSTEMPLATE` || abort "No such OS template: $OSTEMPLATE"
set $OST
OSNAME=$1
OSVER=$2
OSSET=$3
OSARCH=$4
TDIR=$5
check_ost_exists $OSNAME $OSVER $OSSET $OSARCH || exit 1
RPM=`get_rpm $TDIR`
# Run it
log3 exec $RPM $RPM_ARGS $USER_ARGS
exec $RPM $RPM_ARGS $USER_ARGS
