#!/bin/bash
#  Copyright (C) 2000-2006 SWsoft. All rights reserved.
#
#  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# This script should create VE private area
# For usage info see vz-create_prvt(5) man page.
#
# Parameters are passed in environment variables.
# Required parameters:
#   VE_PRVT		- path to root of VE private areas
#   PRIVATE_TEMPLATE	- path to private template used as a source for copying

. /usr/lib/vzctl/scripts/vps-functions

vzcheckvar VE_PRVT PRIVATE_TEMPLATE

function create_prvt()
{
	if test ! -d ${VE_PRVT}; then
		vzerror "Destination dir doesn't exists: ${VE_PRVT}" $VZ_FS_NEW_VE_PRVT 
	fi

	if test ! -f $PRIVATE_TEMPLATE; then
		vzerror "Tarball does not exist: ${PRIVATE_TEMPLATE}" $VZ_FS_NEW_VE_PRVT
	fi
	TMP=`df -P ${VE_PRVT}`
	AVAIL=`echo "${TMP}" | awk 'END{print $4}'`
	NEEDED=`gzip -l  ${PRIVATE_TEMPLATE} | awk 'END{print int($2/1024)}'`
	if test ${AVAIL} -lt ${NEEDED}; then
		vzerror "Insufficient disk space in ${VE_PRVT} available: ${AVAIL} needed: ${NEEDED} <${TMP}>" ${VZ_FS_NO_DISK_SPACE}
	fi
	tar -C ${VE_PRVT} ${TAR_OPT} -xzf ${PRIVATE_TEMPLATE}
	if [ $? -ne 0 ]; then
		vzerror "Error in tar xzf $PRIVATE_TEMPLATE ${exclude}" $VZ_FS_NEW_VE_PRVT
	fi
}

create_prvt
exit 0
