#!/bin/sh

print_usage()
{
    [ "$1" = 0 ] || ($@ >&2; echo "")
    cat <<EOF
Usage: vsr-chroot <workdir> <command>

Use vsr-chroot to execute <command> under fake chroot into your vserver
		     				     
Report bugs to <shalupov@altlinux.ru>
EOF
    exit 1
}

WORKDIR=$1
shift

[ -z "$WORKDIR" ] && print_usage echo "ERROR: WORKDIR not specified"
[ -z "$1" ] && print_usage echo "ERROR: You must specify command"

test -d "$WORKDIR" || print_usage echo "ERROR: WORKDIR not exists"
WORKDIR=`realpath $WORKDIR`

TMPDIR="$WORKDIR/tmp"
ROOT="$WORKDIR/root"
test -f $TMPDIR/permiss || print_usage echo "ERROR: You should create WORKDIR by vsr-build first"

cd /
fakeroot -s $TMPDIR/permiss -i $TMPDIR/permiss \
    -l `ls /usr/lib/libfakechroot/libfakechroot.so* | head -n 1` \
    /usr/sbin/chroot $ROOT $@
